Modified annotation suppression to use @SuppressWarning('PMD') to suppress all warnings and @SuppressWarning('PMD.UnusedLocalVariable') to suppress a particular rule's warnings.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4863 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2006-12-07 13:56:47 +00:00
parent 81a8aea958
commit 14feb13963
6 changed files with 84 additions and 87 deletions

View File

@ -38,6 +38,7 @@ Fixed array handling in AvoidReassigningParameters and UnusedFormalParameter.
Fixed bug in UselessOverridingMethod: false + when adding synchronization.
Fixed false positives in LocalVariableCouldBeFinal.
Fixed false positives in MethodArgumentCouldBeFinal.
Modified annotation suppression to use @SuppressWarning("PMD") to suppress all warnings and @SuppressWarning("PMD.UnusedLocalVariable") to suppress a particular rule's warnings.
Rules can now call RuleContext.getSourceType() if they need to make different checks on JDK 1.4 and 1.5 code.
CloseResource rule now checks code without java.sql import.
ArrayIsStoredDirectly rule now checks Constructors

View File

@ -21,6 +21,10 @@
if (c.getImage().equalsIgnoreCase("Foo")) addViolation(ctx, c);
return super.visit(c, ctx);
}
public String getName() {
return "NoFoo";
}
}
public void testClassLevelSuppression() throws Throwable {
@ -67,12 +71,18 @@
assertEquals(1, rpt.size());
}
public void testSpecificSuppression() throws Throwable {
Report rpt = new Report();
runTestFromString(TEST9, new FooRule(), rpt, SourceType.JAVA_15);
assertEquals(1, rpt.size());
}
private static final String TEST1 =
"@SuppressWarnings(\"\")" + PMD.EOL +
"@SuppressWarnings(\"PMD\")" + PMD.EOL +
"public class Foo {}";
private static final String TEST2 =
"@SuppressWarnings(\"\")" + PMD.EOL +
"@SuppressWarnings(\"PMD\")" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
@ -81,7 +91,7 @@
private static final String TEST3 =
"public class Baz {" + PMD.EOL +
" @SuppressWarnings(\"\")" + PMD.EOL +
" @SuppressWarnings(\"PMD\")" + PMD.EOL +
" public class Bar {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
@ -91,7 +101,7 @@
private static final String TEST4 =
"public class Foo {" + PMD.EOL +
" @SuppressWarnings(\"\")" + PMD.EOL +
" @SuppressWarnings(\"PMD\")" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
" }" + PMD.EOL +
@ -99,7 +109,7 @@
private static final String TEST5 =
"public class Bar {" + PMD.EOL +
" @SuppressWarnings(\"\")" + PMD.EOL +
" @SuppressWarnings(\"PMD\")" + PMD.EOL +
" public Bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
" }" + PMD.EOL +
@ -107,7 +117,7 @@
private static final String TEST6 =
"public class Bar {" + PMD.EOL +
" @SuppressWarnings(\"\")" + PMD.EOL +
" @SuppressWarnings(\"PMD\")" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" int foo;" + PMD.EOL +
@ -117,15 +127,23 @@
private static final String TEST7 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar(@SuppressWarnings(\"\") int foo) {}" + PMD.EOL +
" void bar(@SuppressWarnings(\"PMD\") int foo) {}" + PMD.EOL +
"}";
private static final String TEST8 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" @SuppressWarnings(\"\") int foo;" + PMD.EOL +
" @SuppressWarnings(\"PMD\") int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST9 =
"public class Bar {" + PMD.EOL +
" int foo;" + PMD.EOL +
" void bar() {" + PMD.EOL +
" @SuppressWarnings(\"PMD.NoFoo\") int foo;" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -10,44 +10,35 @@ These are new rules that are still in progress.
</description>
<rule name="AvoidUsingOctalValues"
message="Do not start a literal by 0 unless it's an octal value"
class="net.sourceforge.pmd.rules.DynamicXPathRule"
type="Literal"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidUsingOctalValues">
<description>
Integers literals should not start with zero.
Zero means that the rest of literal have to be
interprated as an octal.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
<rule name="DataflowAnomalyAnalysis"
message="Found ''{0}''-anomaly for variable ''{1}'' (lines ''{2}''-''{3}'')."
class="net.sourceforge.pmd.dfa.DaaRule"
dfa="true">
<description>The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow.
From those informations there can be found various problems.
.
[(starts-with(@Image, 0))]
[not(substring(@Image, 2, 1) = 'x')]
[not(substring(@Image, 2, 1) = 'L')]
[string-length(@Image)>1]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
1. UR - Anomaly: There is a reference to a variable that was not defined before. This is a bug and leads to an error.
2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text.
3. DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.
</description>
<priority>5</priority>
<properties>
<property name="maxviolations" value="100"/>
<property name="maxpaths" value="1000"/>
</properties>
<example>
<![CDATA[
...
int i = 012; // set i with 10 not 12
int j = 010; // set j with 8 not 10
...
k = i * j; // set k with 80 not 120
..
public class Foo {
public void foo() {
int buz = 5;
buz = 6; // redefinition of buz -> dd-anomaly
foo(buz);
buz = 2;
} // buz is undefined when leaving scope -> du-anomaly
}
]]>
</example>
</rule>
</example>
</rule>
<!--
<rule name="UselessAssignment"

View File

@ -4,6 +4,9 @@ package net.sourceforge.pmd.ast;
import net.sourceforge.pmd.Rule;
import java.util.Iterator;
import java.util.List;
public class ASTAnnotation extends SimpleJavaNode {
public ASTAnnotation(int id) {
super(id);
@ -13,58 +16,32 @@ public class ASTAnnotation extends SimpleJavaNode {
super(p, id);
}
public boolean suppresses(Rule rule) {
/* Check for "suppress all warnings" case
@SuppressWarnings("")
TypeDeclaration
Annotation
NormalAnnotation
Name:SuppressWarnings
*/
final String ruleAnno = "\"PMD." + rule.getName() + "\"";
if (jjtGetChild(0) instanceof ASTSingleMemberAnnotation) {
ASTSingleMemberAnnotation n = (ASTSingleMemberAnnotation) jjtGetChild(0);
if (n.jjtGetChild(0) instanceof ASTName && ((ASTName) n.jjtGetChild(0)).hasImageEqualTo("SuppressWarnings")) {
return true;
if (n.jjtGetChild(0) instanceof ASTName) {
ASTName annName = ((ASTName) n.jjtGetChild(0));
if (annName.getImage().equals("SuppressWarnings")) {
List nodes = n.findChildrenOfType(ASTLiteral.class);
for (Iterator iter = nodes.iterator(); iter.hasNext();) {
ASTLiteral element = (ASTLiteral) iter.next();
if (element.hasImageEqualTo("\"PMD\"")
|| element.hasImageEqualTo(ruleAnno)) {
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.hasImageEqualTo("SuppressWarnings")) {
return false;
}
//List values = findChildrenOfType()
*/
return false;
}
/**
* Accept the visitor. *
* Accept the visitor.
*/
public Object jjtAccept(JavaParserVisitor visitor, Object data) {
return visitor.visit(this, data);

View File

@ -54,6 +54,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Jason Bennett - Rewrite of annotation-based warning suppression to allow for rule-specific suppression, noticed useless line in XSLT scripts, fix for UnnecessaryLocalBeforeReturn, wrote NPathComplexity rule, patches to improve CyclomaticComplexity rule, Implemented: UseCollectionIsEmpty, NcssTypeCount, NcssMethodCount, NcssConstructor</li>
<li>Brent Fisher - Fixed report backslash bug, SummaryHTML report improvements</li>
<li>Thomas Leplus - Rewrote UselessStringValueOf</li>
<li>Larry Brigman - Reported symlink bug in CPD</li>
@ -61,7 +62,6 @@
<li>classens - Noted missing varargs setting in ASTFormalParameter</li>
<li>piair - Implemented StringBufferInstantiationWithChar, AvoidUsingOctalValues</li>
<li>Christopher Eagan - Reported bug in VariableNamingConventions</li>
<li>Jason Bennett - Noticed useless line in XSLT scripts, fix for UnnecessaryLocalBeforeReturn, wrote NPathComplexity rule, patches to improve CyclomaticComplexity rule, Implemented: UseCollectionIsEmpty, NcssTypeCount, NcssMethodCount, NcssConstructor</li>
<li><a href="http://www.livejournal.com/users/insac/">Fabio Insaccanebbia</a> - Improvement for UseArraysAsList, UnusedNullCheckInEquals, MisplacedNullCheck, UselessOperationOnImmutable, AvoidArrayLoops, UseArraysAsList, AvoidConstantsInterface, AvoidDecimalLiteralsInBigDecimalConstructor, ClassCastExceptionWithToArray, BigIntegerInstantiation</li>
<li>Ryan Gustafson - Patch to fix bug in AvoidDecimalLiteralsInBigDecimalConstructor, patch to add "ref" overrides to RuleSetFactory, patch to fix JDK 1.3 incompatibilities in PMD 2.0</li>
<li>Stefan Seidel - Reported JDK 1.5 parsing bug</li>

View File

@ -12,7 +12,17 @@
<p>You can use a JDK 1.5 annotation to suppress PMD warnings, like this:</p>
<source>
// This will suppress all the PMD warnings in this class
@SuppressWarnings("")
@SuppressWarnings("PMD")
public class Bar {
void bar() {
int foo;
}
}
</source>
<p>Or you can suppress one rule with an annotation like this:</p>
<source>
// This will suppress UnusedLocalVariable warnings in this class
@SuppressWarnings("PMD.UnusedLocalVariable")
public class Bar {
void bar() {
int foo;