From 82b167e5f1f7357b281ab572d4a3db44c815d82a Mon Sep 17 00:00:00 2001 From: Guilherme Silveira Date: Fri, 7 Oct 2011 01:22:25 -0300 Subject: [PATCH] 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 --- .../hibernate/hql/internal/QuerySplitter.java | 80 ++++++++++--------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/QuerySplitter.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/QuerySplitter.java index fa35676060..0416bc21a6 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/QuerySplitter.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/QuerySplitter.java @@ -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 ); @@ -139,6 +126,25 @@ public static String[] concreteQueries(String query, SessionFactoryImplementor f if (results.length == 0) LOG.noPersistentClassesFound(query); 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 ) || (