diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 5364b057d4..544814ed9e 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,6 +1,8 @@ ???? - 4.2.5: Enhanced logging in the ClassTypeResolver to provide more detailed messaging. +Fixed bug 2338341 - ArrayIndexOutOfBoundsException in cpd on rails project +Fixed bug 2315623 - @SuppressWarnings("PMD.UseSingleton") has no effect October 12, 2008 - 4.2.4: diff --git a/pmd/regress/test/net/sourceforge/pmd/SuppressWarningsTest.java b/pmd/regress/test/net/sourceforge/pmd/SuppressWarningsTest.java index 961ce1339a..8906c10060 100644 --- a/pmd/regress/test/net/sourceforge/pmd/SuppressWarningsTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/SuppressWarningsTest.java @@ -6,6 +6,7 @@ import net.sourceforge.pmd.AbstractRule; import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.Report; import net.sourceforge.pmd.SourceType; +import net.sourceforge.pmd.ast.ASTCompilationUnit; import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; @@ -32,6 +33,24 @@ import junit.framework.JUnit4TestAdapter; } } + private static class BarRule extends AbstractRule { + @Override + public Object visit(ASTCompilationUnit cu, Object ctx) { + // Convoluted rule to make sure the violation is reported for the ASTCompilationUnit node + for (ASTClassOrInterfaceDeclaration c : cu.findChildrenOfType(ASTClassOrInterfaceDeclaration.class)) { + if (c.getImage().equalsIgnoreCase("bar")) { + addViolation(ctx, cu); + } + } + return super.visit(cu, ctx); + } + + @Override + public String getName() { + return "NoBar"; + } + } + @Test public void testClassLevelSuppression() throws Throwable { Report rpt = new Report(); @@ -111,6 +130,13 @@ import junit.framework.JUnit4TestAdapter; assertEquals(0, rpt.size()); } + @Test + public void testSpecificSuppressionAtTopLevel() throws Throwable { + Report rpt = new Report(); + runTestFromString(TEST13, new BarRule(), rpt, SourceType.JAVA_15); + assertEquals(0, rpt.size()); + } + private static final String TEST1 = "@SuppressWarnings(\"PMD\")" + PMD.EOL + "public class Foo {}"; @@ -201,6 +227,11 @@ import junit.framework.JUnit4TestAdapter; " @SuppressWarnings(\"all\") int foo;" + PMD.EOL + "}"; + private static final String TEST13 = + "@SuppressWarnings(\"PMD.NoBar\")" + PMD.EOL + + "public class Bar {" + PMD.EOL + + "}"; + public static junit.framework.Test suite() { return new JUnit4TestAdapter(SuppressWarningsTest.class); } diff --git a/pmd/src/net/sourceforge/pmd/RuleViolation.java b/pmd/src/net/sourceforge/pmd/RuleViolation.java index b2fcb692a6..fc270ff62c 100644 --- a/pmd/src/net/sourceforge/pmd/RuleViolation.java +++ b/pmd/src/net/sourceforge/pmd/RuleViolation.java @@ -9,6 +9,7 @@ import java.util.List; import net.sourceforge.pmd.ast.ASTClassOrInterfaceBodyDeclaration; import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration; +import net.sourceforge.pmd.ast.ASTCompilationUnit; import net.sourceforge.pmd.ast.ASTFieldDeclaration; import net.sourceforge.pmd.ast.ASTFormalParameter; import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; @@ -41,7 +42,7 @@ public class RuleViolation implements IRuleViolation { if (r1.getBeginLine() == r2.getBeginLine()) { return 1; } - + // line number diff maps nicely to compare() return r1.getBeginLine() - r2.getBeginLine(); } @@ -123,6 +124,14 @@ public class RuleViolation implements IRuleViolation { if (node instanceof ASTLocalVariableDeclaration) { parentTypes.add(node); } + if (node instanceof ASTCompilationUnit) { + for (int i = 0; i < node.jjtGetNumChildren(); i++) { + SimpleNode n = (SimpleNode) node.jjtGetChild(i); + if (n instanceof ASTTypeDeclaration) { + parentTypes.add(n); + } + } + } for (SimpleNode parentType : parentTypes) { CanSuppressWarnings t = (CanSuppressWarnings) parentType; if (t.hasSuppressWarningsAnnotationFor(getRule())) { @@ -138,14 +147,14 @@ public class RuleViolation implements IRuleViolation { } private void setVariableNameIfExists(SimpleNode node) { - variableName = (node.getClass().equals(ASTFieldDeclaration.class)) + variableName = node.getClass().equals(ASTFieldDeclaration.class) ? ((ASTFieldDeclaration) node).getVariableName() : ""; if ("".equals(variableName)) { - variableName = (node.getClass().equals(ASTLocalVariableDeclaration.class)) + variableName = node.getClass().equals(ASTLocalVariableDeclaration.class) ? ((ASTLocalVariableDeclaration) node).getVariableName() : ""; } if ("".equals(variableName)) { - variableName = (node.getClass().equals(ASTVariableDeclaratorId.class)) + variableName = node.getClass().equals(ASTVariableDeclaratorId.class) ? node.getImage() : ""; } } @@ -198,6 +207,7 @@ public class RuleViolation implements IRuleViolation { return variableName; } + @Override public String toString() { return getFilename() + ":" + getRule() + ":" + getDescription() + ":" + beginLine; }