forked from phoedos/pmd
pmd: fix #985 Suppressed methods shouldn't affect avg CyclomaticComplexity
This commit is contained in:
parent
cece698b04
commit
30af4f6381
@ -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
|
||||
|
@ -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<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
|
||||
for (ASTAnnotation a : annotations) {
|
||||
ASTName name = a.getFirstDescendantOfType(ASTName.class);
|
||||
if ("SuppressWarnings".equals(name.getImage())) {
|
||||
List<ASTLiteral> 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;
|
||||
}
|
||||
}
|
||||
|
@ -144,4 +144,23 @@ public class Test {
|
||||
<expected-problems>0</expected-problems>
|
||||
<code-ref id="constructor-violation"/>
|
||||
</test-code>
|
||||
<test-code reinitializeRule="true">
|
||||
<description>#985 Suppressed methods shouldn't affect avg CyclomaticComplexity</description>
|
||||
<rule-property name="reportLevel">2</rule-property>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Test {
|
||||
@SuppressWarnings("PMD.CyclomaticComplexity")
|
||||
public Test() {
|
||||
if (a == 1) {
|
||||
if (b == 2) {
|
||||
System.out.println("b");
|
||||
} else if (b == 1) {
|
||||
}
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
Loading…
x
Reference in New Issue
Block a user