diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java b/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java index e74075405..3f752d99a 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java @@ -250,19 +250,20 @@ public final class ScopeUtils { * @return whether aAST is a local variable definition. */ public static boolean isLocalVariableDef(DetailAST aAST) { + boolean localVariableDef = false; // variable declaration? if (aAST.getType() == TokenTypes.VARIABLE_DEF) { final DetailAST parent = aAST.getParent(); final int type = parent.getType(); - return type == TokenTypes.SLIST + localVariableDef = type == TokenTypes.SLIST || type == TokenTypes.FOR_INIT || type == TokenTypes.FOR_EACH_CLAUSE; } // catch parameter? if (aAST.getType() == TokenTypes.PARAMETER_DEF) { final DetailAST parent = aAST.getParent(); - return parent.getType() == TokenTypes.LITERAL_CATCH; + localVariableDef = parent.getType() == TokenTypes.LITERAL_CATCH; } - return false; + return localVariableDef; } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java index 74410e83c..49b602920 100755 --- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -574,13 +574,12 @@ public final class TreeWalker * @return true if position of ast1 is greater than position of ast2. */ private static boolean isPositionGreater(DetailAST ast1, DetailAST ast2) { - if (ast1.getLineNo() > ast2.getLineNo()) { - return true; + if (ast1.getLineNo() == ast2.getLineNo()) { + return ast1.getColumnNo() > ast2.getColumnNo(); } - if (ast1.getLineNo() < ast2.getLineNo()) { - return false; + else { + return ast1.getLineNo() > ast2.getLineNo(); } - return ast1.getColumnNo() > ast2.getColumnNo(); } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java index d28fd3640..98673ed24 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/LocalizedMessage.java @@ -243,27 +243,26 @@ public final class LocalizedMessage /** @return the translated message **/ public String getMessage() { + String message = getCustomMessage(); - final String message = getCustomMessage(); - if (message != null) { - return message; - } - - try { - // Important to use the default class loader, and not the one in - // the GlobalProperties object. This is because the class loader in - // the GlobalProperties is specified by the user for resolving - // custom classes. - final ResourceBundle resourceBundle = getBundle(this.bundle); - final String pattern = resourceBundle.getString(key); - return MessageFormat.format(pattern, args); - } - catch (final MissingResourceException ignored) { - // If the Check author didn't provide i18n resource bundles - // and logs error messages directly, this will return - // the author's original message - return MessageFormat.format(key, args); + if (message == null) { + try { + // Important to use the default class loader, and not the one in + // the GlobalProperties object. This is because the class loader in + // the GlobalProperties is specified by the user for resolving + // custom classes. + final ResourceBundle resourceBundle = getBundle(this.bundle); + final String pattern = resourceBundle.getString(key); + message = MessageFormat.format(pattern, args); + } + catch (final MissingResourceException ignored) { + // If the Check author didn't provide i18n resource bundles + // and logs error messages directly, this will return + // the author's original message + message = MessageFormat.format(key, args); + } } + return message; } /** @@ -351,13 +350,17 @@ public final class LocalizedMessage @Override public int compareTo(LocalizedMessage other) { + int result = Integer.compare(getLineNo(), other.getLineNo()); + if (getLineNo() == other.getLineNo()) { if (getColumnNo() == other.getColumnNo()) { - return getMessage().compareTo(other.getMessage()); + result = getMessage().compareTo(other.getMessage()); + } + else { + result = Integer.compare(getColumnNo(), other.getColumnNo()); } - return Integer.compare(getColumnNo(), other.getColumnNo()); } - return Integer.compare(getLineNo(), other.getLineNo()); + return result; } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractDeclarationCollector.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractDeclarationCollector.java index 57735b84a..792d88929 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractDeclarationCollector.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/AbstractDeclarationCollector.java @@ -264,15 +264,15 @@ public abstract class AbstractDeclarationCollector extends Check { * @return whether it was found */ LexicalFrame getIfContains(String nameToFind) { + LexicalFrame frame = null; + if (contains(nameToFind)) { - return this; + frame = this; } else if (parent != null) { - return parent.getIfContains(nameToFind); - } - else { - return null; + frame = parent.getIfContains(nameToFind); } + return frame; } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java index b987dd432..02d20648d 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/ClassResolver.java @@ -190,17 +190,20 @@ public class ClassResolver { * @return Class object for the given name or null. */ private Class resolveQualifiedName(final String name) { + Class classObj = null; try { if (isLoadable(name)) { - return safeLoad(name); + classObj = safeLoad(name); } - //Perhaps it's fully-qualified inner class - final int dot = name.lastIndexOf('.'); - if (dot != -1) { - final String innerName = - name.substring(0, dot) + "$" + name.substring(dot + 1); - if (isLoadable(innerName)) { - return safeLoad(innerName); + else { + //Perhaps it's fully-qualified inner class + final int dot = name.lastIndexOf('.'); + if (dot != -1) { + final String innerName = + name.substring(0, dot) + "$" + name.substring(dot + 1); + if (isLoadable(innerName)) { + classObj = safeLoad(innerName); + } } } } @@ -209,7 +212,6 @@ public class ClassResolver { // so this is unexpected runtime exception throw new IllegalStateException(ex); } - - return null; + return classObj; } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java index 897b08577..73ef8f1d1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolder.java @@ -377,19 +377,24 @@ public class SuppressWarningsHolder private static String getStringExpr(DetailAST ast) { if (ast != null && ast.getType() == TokenTypes.EXPR) { final DetailAST firstChild = ast.getFirstChild(); + String expr = ""; + switch (firstChild.getType()) { case TokenTypes.STRING_LITERAL: // NOTE: escaped characters are not unescaped final String quotedText = firstChild.getText(); - return quotedText.substring(1, quotedText.length() - 1); + expr = quotedText.substring(1, quotedText.length() - 1); + break; case TokenTypes.IDENT: - return firstChild.getText(); + expr = firstChild.getText(); + break; case TokenTypes.DOT: - return firstChild.getLastChild().getText(); + expr = firstChild.getLastChild().getText(); + break; default: // annotations with complex expressions cannot suppress warnings - return ""; } + return expr; } throw new IllegalArgumentException("Expression AST expected: " + ast); } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java index 67b84835f..ed0571c6b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java @@ -186,34 +186,22 @@ public class DeclarationOrderCheck extends Check { case TokenTypes.OBJBLOCK: scopeStates.push(new ScopeState()); break; - case TokenTypes.CTOR_DEF: - if (parentType != TokenTypes.OBJBLOCK) { - return; + if (parentType == TokenTypes.OBJBLOCK) { + processConstructor(ast); } - - processConstructor(ast); break; - case TokenTypes.METHOD_DEF: - - if (parentType != TokenTypes.OBJBLOCK) { - return; + if (parentType == TokenTypes.OBJBLOCK) { + processMethod(ast); } - - processMethod(ast); break; - case TokenTypes.MODIFIERS: - if (parentType != TokenTypes.VARIABLE_DEF - || ast.getParent().getParent().getType() - != TokenTypes.OBJBLOCK) { - return; + if (parentType == TokenTypes.VARIABLE_DEF + && ast.getParent().getParent().getType() == TokenTypes.OBJBLOCK) { + processModifiers(ast); } - - processModifiers(ast); break; - default: break; } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java index 51114ea57..e5cfb54a3 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.java @@ -321,19 +321,24 @@ public class HiddenFieldCheck */ private static boolean inStatic(DetailAST ast) { DetailAST parent = ast.getParent(); + boolean inStatic = false; + while (parent != null) { - switch (parent.getType()) { - case TokenTypes.STATIC_INIT: - return true; - case TokenTypes.METHOD_DEF: - final DetailAST mods = - parent.findFirstToken(TokenTypes.MODIFIERS); - return mods.branchContains(TokenTypes.LITERAL_STATIC); - default: - parent = parent.getParent(); + if (parent.getType() == TokenTypes.STATIC_INIT) { + inStatic = true; + break; + } + else if (parent.getType() == TokenTypes.METHOD_DEF) { + final DetailAST mods = + parent.findFirstToken(TokenTypes.MODIFIERS); + inStatic = mods.branchContains(TokenTypes.LITERAL_STATIC); + break; + } + else { + parent = parent.getParent(); } } - return false; + return inStatic; } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java index 57498efd8..6aec0b2b7 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java @@ -133,19 +133,11 @@ public class InnerAssignmentCheck @Override public void visitToken(DetailAST ast) { - if (isInContext(ast, ALLOWED_ASSIGMENT_CONTEXT)) { - return; + if (!isInContext(ast, ALLOWED_ASSIGMENT_CONTEXT) + && !isInNoBraceControlStatement(ast) + && !isInWhileIdiom(ast)) { + log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY); } - - if (isInNoBraceControlStatement(ast)) { - return; - } - - if (isInWhileIdiom(ast)) { - return; - } - - log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY); } /**