From 30af4f63817dab2bc0bcead14cb12aeba9707faf Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 16 Mar 2013 18:09:25 +0100 Subject: [PATCH] pmd: fix #985 Suppressed methods shouldn't affect avg CyclomaticComplexity --- pmd/etc/changelog.txt | 1 + .../codesize/CyclomaticComplexityRule.java | 33 +++++++++++++++++++ .../codesize/xml/CyclomaticComplexity.xml | 19 +++++++++++ 3 files changed, 53 insertions(+) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index c8ca8089c9..51371b2633 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,6 +1,7 @@ ????? ??, 2013 - 5.0.3: Fixed bug 984: Cyclomatic complexity should treat constructors like methods +Fixed bug 985: Suppressed methods shouldn't affect avg CyclomaticComplexity Fixed bug 992: Class java.beans.Statement triggered in CloseResource rule Fixed bug 997: Rule NonThreadSafeSingleton gives analysis problem Fixed bug 1002: False +: FinalFieldCouldBeStatic on inner class diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java index b3880c8205..26c1a55725 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java @@ -3,11 +3,14 @@ */ package net.sourceforge.pmd.lang.java.rule.codesize; +import java.util.List; import java.util.Stack; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; import net.sourceforge.pmd.lang.java.ast.ASTBlockStatement; import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement; +import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression; @@ -17,8 +20,10 @@ import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTExpression; import net.sourceforge.pmd.lang.java.ast.ASTForStatement; import net.sourceforge.pmd.lang.java.ast.ASTIfStatement; +import net.sourceforge.pmd.lang.java.ast.ASTLiteral; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; +import net.sourceforge.pmd.lang.java.ast.ASTName; import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel; import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement; import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement; @@ -205,6 +210,7 @@ public Object visit(ASTMethodDeclaration node, Object data) { entryStack.push( new Entry( node ) ); super.visit( node, data ); Entry methodEntry = entryStack.pop(); + if (!isSuppressed(node)) { int methodDecisionPoints = methodEntry.decisionPoints; Entry classEntry = entryStack.peek(); classEntry.methodCount++; @@ -228,6 +234,7 @@ public Object visit(ASTMethodDeclaration node, Object data) { methodDeclarator == null ? "" : methodDeclarator.getImage(), String.valueOf( methodEntry.decisionPoints ) } ); } + } return data; } @@ -252,6 +259,7 @@ public Object visit(ASTConstructorDeclaration node, Object data) { entryStack.push( new Entry( node ) ); super.visit( node, data ); Entry constructorEntry = entryStack.pop(); + if (!isSuppressed(node)) { int constructorDecisionPointCount = constructorEntry.decisionPoints; Entry classEntry = entryStack.peek(); classEntry.methodCount++; @@ -264,6 +272,31 @@ public Object visit(ASTConstructorDeclaration node, Object data) { classEntry.node.getImage(), String.valueOf( constructorDecisionPointCount ) } ); } + } return data; } + + private boolean isSuppressed(Node node) { + boolean result = false; + + ASTClassOrInterfaceBodyDeclaration parent = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class); + List annotations = parent.findChildrenOfType(ASTAnnotation.class); + for (ASTAnnotation a : annotations) { + ASTName name = a.getFirstDescendantOfType(ASTName.class); + if ("SuppressWarnings".equals(name.getImage())) { + List literals = a.findDescendantsOfType(ASTLiteral.class); + for (ASTLiteral l : literals) { + if (l.isStringLiteral() && "\"PMD.CyclomaticComplexity\"".equals(l.getImage())) { + result = true; + break; + } + } + } + if (result) { + break; + } + } + + return result; + } } diff --git a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml index d428e1e2f3..000c9c480c 100644 --- a/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml +++ b/pmd/src/test/resources/net/sourceforge/pmd/lang/java/rule/codesize/xml/CyclomaticComplexity.xml @@ -144,4 +144,23 @@ public class Test { 0 + + #985 Suppressed methods shouldn't affect avg CyclomaticComplexity + 2 + 0 + +