From 85e493d55f95bfc69fc25119b219dc6a61301046 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sun, 16 Dec 2012 23:10:17 +0100 Subject: [PATCH] pmd: fixed #1026 PMD doesn't handle 'value =' in SuppressWarnings annotation --- pmd/etc/changelog.txt | 1 + .../pmd/lang/java/ast/ASTAnnotation.java | 32 +++++--- .../sourceforge/pmd/SuppressWarningsTest.java | 77 +++++++++++++++++++ 3 files changed, 98 insertions(+), 12 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 33e2d570b0..cd2cd2c480 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotation.java b/pmd/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotation.java index a0f69aee0c..8919b746f5 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotation.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAnnotation.java @@ -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 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 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; } } } diff --git a/pmd/src/test/java/net/sourceforge/pmd/SuppressWarningsTest.java b/pmd/src/test/java/net/sourceforge/pmd/SuppressWarningsTest.java index c47c00d923..178f744ef1 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/SuppressWarningsTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/SuppressWarningsTest.java @@ -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 +