pmd: fixed #1026 PMD doesn't handle 'value =' in SuppressWarnings annotation

This commit is contained in:
Andreas Dangel 2012-12-16 23:10:17 +01:00
parent 6abbea51dd
commit 85e493d55f
3 changed files with 98 additions and 12 deletions

View File

@ -1,5 +1,6 @@
???? ??, 2012 - 5.0.2:
Fixed bug 1026: PMD doesn't handle 'value =' in SuppressWarnings annotation
Fixed bug 1043: node.getEndLine() always returns 0 (ECMAscript)
Fixed bug 1044: Unknown option: -excludemarker
Fixed bug 1047: False Positive in 'for' loops for LocalVariableCouldBeFinal in 5.0.1

View File

@ -6,6 +6,7 @@ import java.util.Arrays;
import java.util.List;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.lang.ast.Node;
public class ASTAnnotation extends AbstractJavaNode {
@ -27,20 +28,27 @@ public class ASTAnnotation extends AbstractJavaNode {
if (jjtGetChild(0) instanceof ASTSingleMemberAnnotation) {
ASTSingleMemberAnnotation n = (ASTSingleMemberAnnotation) jjtGetChild(0);
return checkAnnototation(n, ruleAnno, rule);
} else if (jjtGetChild(0) instanceof ASTNormalAnnotation) {
ASTNormalAnnotation n = (ASTNormalAnnotation) jjtGetChild(0);
return checkAnnototation(n, ruleAnno, rule);
}
return false;
}
if (n.jjtGetChild(0) instanceof ASTName) {
ASTName annName = (ASTName) n.jjtGetChild(0);
private boolean checkAnnototation(Node n, String ruleAnno, Rule rule) {
if (n.jjtGetChild(0) instanceof ASTName) {
ASTName annName = (ASTName) n.jjtGetChild(0);
if (annName.getImage().equals("SuppressWarnings")) {
List<ASTLiteral> nodes = n.findDescendantsOfType(ASTLiteral.class);
for (ASTLiteral element : nodes) {
if (element.hasImageEqualTo("\"PMD\"") || element.hasImageEqualTo(ruleAnno)
// Check for standard annotations values
|| element.hasImageEqualTo("\"all\"")
|| element.hasImageEqualTo("\"serial\"") && serialRules.contains(rule.getName())
|| element.hasImageEqualTo("\"unused\"") && unusedRules.contains(rule.getName())) {
return true;
}
if (annName.getImage().equals("SuppressWarnings")) {
List<ASTLiteral> nodes = n.findDescendantsOfType(ASTLiteral.class);
for (ASTLiteral element : nodes) {
if (element.hasImageEqualTo("\"PMD\"") || element.hasImageEqualTo(ruleAnno)
// Check for standard annotations values
|| element.hasImageEqualTo("\"all\"")
|| element.hasImageEqualTo("\"serial\"") && serialRules.contains(rule.getName())
|| element.hasImageEqualTo("\"unused\"") && unusedRules.contains(rule.getName())) {
return true;
}
}
}

View File

@ -116,6 +116,41 @@ import org.junit.Test;
assertEquals(1, rpt.size());
}
@Test
public void testSpecificSuppressionValue1() throws Throwable {
Report rpt = new Report();
runTestFromString(TEST9_VALUE1, new FooRule(), rpt, LanguageVersion.JAVA_15);
assertEquals(1, rpt.size());
}
@Test
public void testSpecificSuppressionValue2() throws Throwable {
Report rpt = new Report();
runTestFromString(TEST9_VALUE2, new FooRule(), rpt, LanguageVersion.JAVA_15);
assertEquals(1, rpt.size());
}
@Test
public void testSpecificSuppressionValue3() throws Throwable {
Report rpt = new Report();
runTestFromString(TEST9_VALUE3, new FooRule(), rpt, LanguageVersion.JAVA_15);
assertEquals(1, rpt.size());
}
@Test
public void testSpecificSuppressionMulitpleValues1() throws Throwable {
Report rpt = new Report();
runTestFromString(TEST9_MULTIPLE_VALUES_1, new FooRule(), rpt, LanguageVersion.JAVA_15);
assertEquals(0, rpt.size());
}
@Test
public void testSpecificSuppressionMulitpleValues2() throws Throwable {
Report rpt = new Report();
runTestFromString(TEST9_MULTIPLE_VALUES_2, new FooRule(), rpt, LanguageVersion.JAVA_15);
assertEquals(0, rpt.size());
}
@Test
public void testNoSuppressionBlank() throws Throwable {
Report rpt = new Report();
@ -213,6 +248,48 @@ import org.junit.Test;
" }" + PMD.EOL +
"}";
private static final String TEST9_VALUE1 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" @SuppressWarnings(value = \"PMD.NoFoo\") int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST9_VALUE2 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" @SuppressWarnings({\"PMD.NoFoo\"}) int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST9_VALUE3 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" @SuppressWarnings(value = {\"PMD.NoFoo\"}) int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST9_MULTIPLE_VALUES_1 =
"@SuppressWarnings({\"PMD.NoFoo\", \"PMD.NoBar\"})" + PMD.EOL +
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST9_MULTIPLE_VALUES_2 =
"@SuppressWarnings(value = {\"PMD.NoFoo\", \"PMD.NoBar\"})" + PMD.EOL +
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST10 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +