From 5a3f05ec8038daa674666bbaa82d59127aa74f92 Mon Sep 17 00:00:00 2001 From: Ruslan Diachenko Date: Thu, 27 Aug 2015 23:42:16 +0100 Subject: [PATCH] Issue #1566: ReturnCount violation fixed for AbstractSuperCheck --- .../checks/coding/AbstractSuperCheck.java | 70 ++++++++++++------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java index 23765d968..848fa9138 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java @@ -89,36 +89,50 @@ public abstract class AbstractSuperCheck } /** - * Determines whether a 'super' literal is a call to the super method + * Determines whether a 'super' literal is a call to the super method * for this check. * @param literalSuperAst the AST node of a 'super' literal. * @return true if ast is a call to the super method * for this check. */ private boolean isSuperCall(DetailAST literalSuperAst) { - if (literalSuperAst.getType() != TokenTypes.LITERAL_SUPER) { - return false; - } - // dot operator? - DetailAST dotAst = literalSuperAst.getParent(); - if (dotAst.getType() != TokenTypes.DOT - || isSameNameMethod(literalSuperAst) - || hasArguments(dotAst)) { - return false; - } + boolean superCall = false; + + if (literalSuperAst.getType() == TokenTypes.LITERAL_SUPER) { + // dot operator? + final DetailAST dotAst = literalSuperAst.getParent(); + + if (dotAst.getType() == TokenTypes.DOT + && !isSameNameMethod(literalSuperAst) + && !hasArguments(dotAst)) { + superCall = isSuperCallInOverridingMethod(dotAst); + } + } + return superCall; + } + + /** + * Determines whether a super call in overriding method. + * + * @param ast The AST node of a 'dot operator' in 'super' call. + * @return true if super call in overriding method. + */ + private boolean isSuperCallInOverridingMethod(DetailAST ast) { + boolean inOverridingMethod = false; + DetailAST dotAst = ast; - // in an overriding method for this check? while (dotAst != null) { if (dotAst.getType() == TokenTypes.METHOD_DEF) { - return isOverridingMethod(dotAst); + inOverridingMethod = isOverridingMethod(dotAst); + break; } if (dotAst.getType() == TokenTypes.CTOR_DEF || dotAst.getType() == TokenTypes.INSTANCE_INIT) { - return false; + break; } dotAst = dotAst.getParent(); } - return false; + return inOverridingMethod; } /** @@ -173,19 +187,21 @@ public abstract class AbstractSuperCheck * @return true if the method of ast is a method for this check. */ private boolean isOverridingMethod(DetailAST ast) { - if (ast.getType() != TokenTypes.METHOD_DEF - || ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) { - return false; + boolean overridingMethod = false; + + if (ast.getType() == TokenTypes.METHOD_DEF + && !ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) { + final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); + final String name = nameAST.getText(); + final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS); + + if (getMethodName().equals(name) + && !modifiersAST.branchContains(TokenTypes.LITERAL_NATIVE)) { + final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); + overridingMethod = params.getChildCount() == 0; + } } - final DetailAST nameAST = ast.findFirstToken(TokenTypes.IDENT); - final String name = nameAST.getText(); - final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS); - if (!getMethodName().equals(name) - || modifiersAST.branchContains(TokenTypes.LITERAL_NATIVE)) { - return false; - } - final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); - return params.getChildCount() == 0; + return overridingMethod; } /**