removing useless variables

removing useless logical conditions
extracted some methods to improve cyclomatic complexity
extracted loop into two pieces to remove extra useless variables and minimize complexity
This commit is contained in:
Guilherme Silveira 2011-10-07 01:22:25 -03:00 committed by Strong Liu
parent f951e15fea
commit 82b167e5f1

View File

@ -85,51 +85,38 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
ArrayList placeholders = new ArrayList();
ArrayList replacements = new ArrayList();
StringBuffer templateQuery = new StringBuffer( 40 );
int start = getStartingPositionFor(tokens, templateQuery);
int count = 0;
String last = null;
int nextIndex = 0;
String next = null;
boolean isSelectClause = false;
String last = tokens[start - 1].toLowerCase();
templateQuery.append( tokens[0] );
if ( "select".equals( tokens[0].toLowerCase() ) ) isSelectClause = true;
for ( int i = 1; i < tokens.length; i++ ) {
//update last non-whitespace token, if necessary
if ( !ParserHelper.isWhitespace( tokens[i - 1] ) ) last = tokens[i - 1].toLowerCase();
// select-range is terminated by declaration of "from"
if ( "from".equals( tokens[i].toLowerCase() ) ) isSelectClause = false;
for ( int i = start; i < tokens.length; i++ ) {
String token = tokens[i];
if ( !ParserHelper.isWhitespace( token ) || last == null ) {
//scan for next non-whitespace token
if ( nextIndex <= i ) {
for ( nextIndex = i + 1; nextIndex < tokens.length; nextIndex++ ) {
next = tokens[nextIndex].toLowerCase();
if ( !ParserHelper.isWhitespace( next ) ) break;
if ( ParserHelper.isWhitespace( token ) ) {
templateQuery.append( token );
continue;
}
next = nextNonWhite(tokens, i).toLowerCase();
boolean process = isJavaIdentifier( token ) &&
isPossiblyClassName( last, next );
last = token.toLowerCase();
if (process) {
String importedClassName = getImportedClass( token, factory );
if ( importedClassName != null ) {
String[] implementors = factory.getImplementors( importedClassName );
token = "$clazz" + count++ + "$";
if ( implementors != null ) {
placeholders.add( token );
replacements.add( implementors );
}
}
boolean process = !isSelectClause &&
isJavaIdentifier( token ) &&
isPossiblyClassName( last, next );
if (process) {
String importedClassName = getImportedClass( token, factory );
if ( importedClassName != null ) {
String[] implementors = factory.getImplementors( importedClassName );
String placeholder = "$clazz" + count++ + "$";
if ( implementors != null ) {
placeholders.add( placeholder );
replacements.add( implementors );
}
token = placeholder; // Note this!!
}
}
}
templateQuery.append( token );
@ -140,6 +127,25 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f
return results;
}
private static String nextNonWhite(String[] tokens, int start) {
for ( int i = start + 1; i < tokens.length; i++ ) {
if ( !ParserHelper.isWhitespace( tokens[i] ) ) return tokens[i];
}
return tokens[tokens.length - 1];
}
private static int getStartingPositionFor(String[] tokens, StringBuffer templateQuery) {
templateQuery.append( tokens[0] );
if ( !"select".equals( tokens[0].toLowerCase() ) ) return 1;
// select-range is terminated by declaration of "from"
for (int i = 1; i < tokens.length; i++ ) {
if ( "from".equals( tokens[i].toLowerCase() ) ) return i;
templateQuery.append( tokens[i] );
}
return tokens.length;
}
private static boolean isPossiblyClassName(String last, String next) {
return "class".equals( last ) || (
BEFORE_CLASS_TOKENS.contains( last ) &&