Fixed bug 2315623 - @SuppressWarnings("PMD.UseSingleton") has no effect

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6701 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch 2008-11-24 21:00:23 +00:00
parent 6836ae8bcd
commit 8f8afde50b
3 changed files with 47 additions and 4 deletions

View File

@ -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:

View File

@ -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);
}

View File

@ -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;
}