Issue #1566: ReturnCount violations (partial fix)

This commit is contained in:
Ruslan Diachenko 2015-08-18 23:14:51 +01:00 committed by Roman Ivanov
parent 03f4181580
commit 0da1e4d980
9 changed files with 85 additions and 90 deletions

View File

@ -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;
}
}

View File

@ -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();
}
/**

View File

@ -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;
}
/**

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}
/**

View File

@ -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);
}
/**