HHH-8159 - Apply fixups indicated by analysis tools

This commit is contained in:
Steve Ebersole 2013-04-26 11:04:40 -05:00
parent eaef21076d
commit e09d6855a2
6 changed files with 125 additions and 40 deletions

View File

@ -102,19 +102,38 @@ protected final String getOriginalMessage() {
} }
/** /**
* Wraps this exception with another, of same kind, with the specified queryString. If this exception already
* has a queryString defined, the same exception ({@code this}) is returned. Otherwise the protected
* {@link #generateQueryException(String)} is called, to allow subclasses to properly create the correct
* subclass for return.
* *
* @param queryString * @param queryString The query string that led to the QueryException
* @return *
* @return {@code this}, if {@code this} has {@code null} for {@link #getQueryString()}; otherwise a new
* QueryException (or subclass) is returned.
*/ */
public final QueryException wrapWithQueryString(String queryString) { public final QueryException wrapWithQueryString(String queryString) {
if ( this.getQueryString() != null ) { if ( this.getQueryString() != null ) {
return this; return this;
} }
return doWrapWithQueryString( queryString ); return generateQueryException( queryString );
} }
protected QueryException doWrapWithQueryString(String queryString) { /**
* Called from {@link #wrapWithQueryString(String)} when we really need to generate a new QueryException
* (or subclass).
* <p/>
* NOTE : implementors should take care to use {@link #getOriginalMessage()} for the message, not
* {@link #getMessage()}
*
* @param queryString The query string
*
* @return The generated QueryException (or subclass)
*
* @see #getOriginalMessage()
*/
protected QueryException generateQueryException(String queryString) {
return new QueryException( getOriginalMessage(), queryString, this ); return new QueryException( getOriginalMessage(), queryString, this );
} }
} }

View File

@ -50,7 +50,7 @@ public QueryParameterException(String message, String queryString, Exception cau
} }
@Override @Override
protected QueryException doWrapWithQueryString(String queryString) { protected QueryException generateQueryException(String queryString) {
return new QueryParameterException( super.getOriginalMessage(), queryString, this ); return new QueryParameterException( super.getOriginalMessage(), queryString, this );
} }
} }

View File

@ -37,47 +37,76 @@
* An error handler that counts parsing errors and warnings. * An error handler that counts parsing errors and warnings.
*/ */
public class ErrorCounter implements ParseErrorHandler { public class ErrorCounter implements ParseErrorHandler {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
ErrorCounter.class.getName()
);
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, ErrorCounter.class.getName()); private final String hql;
private List<String> errorList = new ArrayList<String>(); private List<String> errorList = new ArrayList<String>();
private List<String> warningList = new ArrayList<String>();
private List<RecognitionException> recognitionExceptions = new ArrayList<RecognitionException>(); private List<RecognitionException> recognitionExceptions = new ArrayList<RecognitionException>();
/**
* Constructs an ErrorCounter without knowledge of the HQL, meaning that generated QueryException
* instances *will not* contain the HQL (and will need to be wrapped at a higher level in another
* QueryException).
*/
public ErrorCounter() {
this( null );
}
/**
* Constructs an ErrorCounter with knowledge of the HQL, meaning that generated QueryException
* instances *will* contain the HQL.
*/
public ErrorCounter(String hql) {
this.hql = hql;
}
@Override
public void reportError(RecognitionException e) { public void reportError(RecognitionException e) {
reportError( e.toString() ); reportError( e.toString() );
recognitionExceptions.add( e ); recognitionExceptions.add( e );
LOG.error( e.toString(), e ); LOG.error( e.toString(), e );
} }
@Override
public void reportError(String message) { public void reportError(String message) {
LOG.error( message ); LOG.error( message );
errorList.add( message ); errorList.add( message );
} }
@Override
public int getErrorCount() { public int getErrorCount() {
return errorList.size(); return errorList.size();
} }
@Override
public void reportWarning(String message) { public void reportWarning(String message) {
LOG.debug( message ); LOG.debug( message );
warningList.add( message );
} }
private String getErrorString() { private String getErrorString() {
StringBuilder buf = new StringBuilder(); final StringBuilder buf = new StringBuilder();
for ( Iterator<String> iterator = errorList.iterator(); iterator.hasNext(); ) { final Iterator<String> iterator = errorList.iterator();
while ( iterator.hasNext() ) {
buf.append( iterator.next() ); buf.append( iterator.next() );
if ( iterator.hasNext() ) buf.append( "\n" ); if ( iterator.hasNext() ) {
buf.append( "\n" );
}
} }
return buf.toString(); return buf.toString();
} }
@Override
public void throwQueryException() throws QueryException { public void throwQueryException() throws QueryException {
if ( getErrorCount() > 0 ) { if ( getErrorCount() > 0 ) {
if (recognitionExceptions.size() > 0) throw QuerySyntaxException.convert(recognitionExceptions.get(0)); if ( recognitionExceptions.size() > 0 ) {
throw new QueryException(getErrorString()); throw QuerySyntaxException.convert( recognitionExceptions.get( 0 ), hql );
}
throw new QueryException( getErrorString(), hql );
} }
LOG.debug( "throwQueryException() : no errors" ); LOG.debug( "throwQueryException() : no errors" );
} }

View File

@ -52,25 +52,35 @@
* @author Joshua Davis (pgmjsd@sourceforge.net) * @author Joshua Davis (pgmjsd@sourceforge.net)
*/ */
public final class HqlParser extends HqlBaseParser { public final class HqlParser extends HqlBaseParser {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
HqlParser.class.getName()
);
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, HqlParser.class.getName()); private final ParseErrorHandler parseErrorHandler;
private final ASTPrinter printer = getASTPrinter();
private ParseErrorHandler parseErrorHandler;
private ASTPrinter printer = getASTPrinter();
private static ASTPrinter getASTPrinter() { private static ASTPrinter getASTPrinter() {
return new ASTPrinter( org.hibernate.hql.internal.antlr.HqlTokenTypes.class ); return new ASTPrinter( org.hibernate.hql.internal.antlr.HqlTokenTypes.class );
} }
/**
* Get a HqlParser instance for the given HQL string.
*
* @param hql The HQL query string
*
* @return The parser.
*/
public static HqlParser getInstance(String hql) { public static HqlParser getInstance(String hql) {
// [jsd] The fix for HHH-558... return new HqlParser( hql );
HqlLexer lexer = new HqlLexer( new StringReader( hql ) );
return new HqlParser( lexer );
} }
private HqlParser(TokenStream lexer) { private HqlParser(String hql) {
super( lexer ); // The fix for HHH-558...
initialize(); super( new HqlLexer( new StringReader( hql ) ) );
parseErrorHandler = new ErrorCounter( hql );
// Create nodes that track line and column number.
setASTFactory( new HqlASTFactory() );
} }
@ -326,12 +336,6 @@ private void showAst(AST ast, PrintWriter pw) {
printer.showAst( ast, pw ); printer.showAst( ast, pw );
} }
private void initialize() {
// Initialize the error handling delegate.
parseErrorHandler = new ErrorCounter();
setASTFactory(new HqlASTFactory()); // Create nodes that track line and column number.
}
@Override @Override
public void weakKeywords() throws TokenStreamException { public void weakKeywords() throws TokenStreamException {

View File

@ -170,7 +170,7 @@ public HqlSqlWalker(
String collectionRole) { String collectionRole) {
setASTFactory( new SqlASTFactory( this ) ); setASTFactory( new SqlASTFactory( this ) );
// Initialize the error handling delegate. // Initialize the error handling delegate.
this.parseErrorHandler = new ErrorCounter(); this.parseErrorHandler = new ErrorCounter( qti.getQueryString() );
this.queryTranslatorImpl = qti; this.queryTranslatorImpl = qti;
this.sessionFactoryHelper = new SessionFactoryHelper( sfi ); this.sessionFactoryHelper = new SessionFactoryHelper( sfi );
this.literalProcessor = new LiteralProcessor( this ); this.literalProcessor = new LiteralProcessor( this );

View File

@ -1,10 +1,10 @@
/* /*
* Hibernate, Relational Persistence for Idiomatic Java * Hibernate, Relational Persistence for Idiomatic Java
* *
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as * Copyright (c) 2008, 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution * indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are * statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC. * distributed under license by Red Hat Inc.
* *
* This copyrighted material is made available to anyone wishing to use, modify, * This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU * copy, or redistribute it subject to the terms and conditions of the GNU
@ -20,7 +20,6 @@
* Free Software Foundation, Inc. * Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*
*/ */
package org.hibernate.hql.internal.ast; package org.hibernate.hql.internal.ast;
import antlr.RecognitionException; import antlr.RecognitionException;
@ -33,23 +32,57 @@
* @author josh * @author josh
*/ */
public class QuerySyntaxException extends QueryException { public class QuerySyntaxException extends QueryException {
/**
* Constructs a QuerySyntaxException
*
* @param message Message explaining the condition that led to the exception
*/
public QuerySyntaxException(String message) { public QuerySyntaxException(String message) {
super( message ); super( message );
} }
/**
* Constructs a QuerySyntaxException
*
* @param message Message explaining the condition that led to the exception
* @param hql The hql query that was being parsed/analyzed
*/
public QuerySyntaxException(String message, String hql) { public QuerySyntaxException(String message, String hql) {
super( message, hql ); super( message, hql );
} }
/**
* Intended for use solely from {@link #generateQueryException(String)}
*
* @param message Message explaining the condition that led to the exception
* @param queryString The hql query that was being parsed/analyzed
* @param cause The cause, generally another QuerySyntaxException
*/
protected QuerySyntaxException(String message, String queryString, Exception cause) { protected QuerySyntaxException(String message, String queryString, Exception cause) {
super( message, queryString, cause ); super( message, queryString, cause );
} }
/**
* Converts the given ANTLR RecognitionException into a QuerySyntaxException. The RecognitionException
* does not become the cause because ANTLR exceptions are not serializable.
*
* @param e The ANTLR exception
*
* @return The QuerySyntaxException
*/
public static QuerySyntaxException convert(RecognitionException e) { public static QuerySyntaxException convert(RecognitionException e) {
return convert( e, null ); return convert( e, null );
} }
/**
* Converts the given ANTLR RecognitionException into a QuerySyntaxException. The RecognitionException
* does not become the cause because ANTLR exceptions are not serializable.
*
* @param e The ANTLR exception
* @param hql The query string
*
* @return The QuerySyntaxException
*/
public static QuerySyntaxException convert(RecognitionException e, String hql) { public static QuerySyntaxException convert(RecognitionException e, String hql) {
String positionInfo = e.getLine() > 0 && e.getColumn() > 0 String positionInfo = e.getLine() > 0 && e.getColumn() > 0
? " near line " + e.getLine() + ", column " + e.getColumn() ? " near line " + e.getLine() + ", column " + e.getColumn()
@ -58,7 +91,7 @@ public static QuerySyntaxException convert(RecognitionException e, String hql) {
} }
@Override @Override
protected QueryException doWrapWithQueryString(String queryString) { protected QueryException generateQueryException(String queryString) {
return new QuerySyntaxException( getOriginalMessage(), queryString, this ); return new QuerySyntaxException( getOriginalMessage(), queryString, this );
} }
} }