Oops, I had forgotten to add this test

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3882 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2005-10-03 13:37:35 +00:00
parent dd5514a1f7
commit 9e5c2a82b7
5 changed files with 128 additions and 6 deletions

View File

@ -0,0 +1,43 @@
package test.net.sourceforge.pmd;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import test.net.sourceforge.pmd.testframework.RuleTst;
public class SuppressWarningsTest extends RuleTst {
private static class FooRule extends AbstractRule {
public Object visit(ASTClassOrInterfaceDeclaration c, Object ctx) {
if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx,c);
return super.visit(c, ctx);
}
public Object visit(ASTVariableDeclaratorId c, Object ctx) {
if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx,c);
return super.visit(c, ctx);
}
}
public void testClassLevelSuppressWarningsAnnotation() throws Throwable {
Report rpt = new Report();
runTestFromString15(TEST1, new FooRule(), rpt);
assertEquals(0, rpt.size());
runTestFromString15(TEST2, new FooRule(), rpt);
assertEquals(0, rpt.size());
}
private static final String TEST1 =
"@SuppressWarnings()" + PMD.EOL +
"public class Foo {}";
private static final String TEST2 =
"@SuppressWarnings()" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -142,13 +142,18 @@ public class Report {
return;
}
// Annotation excluder
/*
SimpleNode node = violation.getNode();
if (node.jjtGetParent() != null && node.jjtGetParent() instanceof ASTTypeDeclaration) {
ASTTypeDeclaration t = (ASTTypeDeclaration)node.jjtGetParent();
if (t.jjtGetChild(0) instanceof ASTAnnotation)
List parentTypes = node.getParentsOfType(ASTTypeDeclaration.class);
if (node instanceof ASTTypeDeclaration) {
parentTypes.add(node);
}
*/
for (Iterator i = parentTypes.iterator(); i.hasNext();) {
ASTTypeDeclaration t = (ASTTypeDeclaration)i.next();
if (t.hasSuppressWarningsAnnotationFor(violation.getRule())) {
return;
}
}
violations.add(violation);
violationTree.addRuleViolation(violation);

View File

@ -2,6 +2,8 @@
package net.sourceforge.pmd.ast;
import net.sourceforge.pmd.Rule;
public class ASTAnnotation extends SimpleNode {
public ASTAnnotation(int id) {
super(id);
@ -12,6 +14,59 @@ public class ASTAnnotation extends SimpleNode {
}
public boolean suppresses(Rule rule) {
/* Check for "suppress all warnings" case
@SuppressWarnings()
TypeDeclaration
Annotation
NormalAnnotation
Name:SuppressWarnings
*/
if (jjtGetChild(0) instanceof ASTNormalAnnotation) {
ASTNormalAnnotation n = (ASTNormalAnnotation)jjtGetChild(0);
if (n.jjtGetChild(0) instanceof ASTName && ((ASTName)n.jjtGetChild(0)).getImage().equals("SuppressWarnings")) {
return true;
}
return false;
}
/* Check for "suppress some warnings" case
@SuppressWarnings({"hi","hey"})
TypeDeclaration
Annotation
SingleMemberAnnotation
Name:SuppressWarnings
MemberValue
MemberValueArrayInitializer
MemberValue
PrimaryExpression
PrimaryPrefix
Literal:"hi"
MemberValue
PrimaryExpression
PrimaryPrefix
Literal:"hey"
*/
/*
if (!(jjtGetChild(0) instanceof ASTName)) {
return false;
}
ASTName n = (ASTName)jjtGetChild(0);
if (n.getImage() == null || n.getImage().equals("SuppressWarnings")) {
return false;
}
//List values = findChildrenOfType()
*/
return false;
}
/** Accept the visitor. **/
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
return visitor.visit(this, data);

View File

@ -2,6 +2,11 @@
package net.sourceforge.pmd.ast;
import net.sourceforge.pmd.Rule;
import java.util.Set;
import java.util.List;
public class ASTSingleMemberAnnotation extends SimpleNode {
public ASTSingleMemberAnnotation(int id) {
super(id);
@ -11,7 +16,6 @@ public class ASTSingleMemberAnnotation extends SimpleNode {
super(p, id);
}
/** Accept the visitor. **/
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
return visitor.visit(this, data);

View File

@ -2,6 +2,10 @@
package net.sourceforge.pmd.ast;
import net.sourceforge.pmd.Rule;
import java.util.Set;
public class ASTTypeDeclaration extends SimpleNode {
public ASTTypeDeclaration(int id) {
super(id);
@ -12,6 +16,17 @@ public class ASTTypeDeclaration extends SimpleNode {
}
public boolean hasSuppressWarningsAnnotationFor(Rule rule) {
for (int i=0;i<jjtGetNumChildren(); i++) {
if (jjtGetChild(i) instanceof ASTAnnotation) {
ASTAnnotation a = (ASTAnnotation)jjtGetChild(i);
if (a.suppresses(rule)) {
return true;
}
}
}
return false;
}
/**
* Accept the visitor. *
*/