backport of: AvoidDuplicateLiteralRule now has 'skipAnnotations' boolean property

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6004 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2008-04-11 16:41:42 +00:00
parent bbad2dfbc2
commit 2f4cdbd6c3
3 changed files with 56 additions and 0 deletions

View File

@ -3,6 +3,7 @@
'41' and '42' shortcuts for rulesets added
Fixed bug 1928009 - Error using migration ruleset in PMD 4.2
Fixed bug 1932242 - EmptyMethodInAbstractClassShouldBeAbstract false +
AvoidDuplicateLiteralRule now has 'skipAnnotations' boolean property
ruleset.dtd and ruleset_xml_schema.xsd added to jar file in rulesets directory
Update RuleSetWriter to handle non-Apache TRAX implementations, add an option to not use XML Namespaces
Added file encoding option to CPD GUI, which already existed for the command line and Ant

View File

@ -37,6 +37,43 @@ duplicate literals in field decl
<code><![CDATA[
public class Foo {
String[] FOO = {"foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo", "foo"};
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
duplicate literals in annotations
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
public class Foo {
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
duplicate literals in annotations, skipped
]]></description>
<expected-problems>0</expected-problems>
<rule-property name="skipAnnotations">true</rule-property>
<code><![CDATA[
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
@SuppressWarnings("foo")
public class Foo {
}
]]></code>
</test-code>

View File

@ -3,9 +3,12 @@
*/
package net.sourceforge.pmd.rules.strings;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTAnnotation;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.ASTLiteral;
import net.sourceforge.pmd.properties.BooleanProperty;
import java.io.BufferedReader;
import java.io.File;
@ -21,6 +24,11 @@ import java.util.Set;
public class AvoidDuplicateLiteralsRule extends AbstractRule {
private static final PropertyDescriptor SKIP_ANNOTATIONS = new BooleanProperty("skipAnnotations",
"Skip literals within Annotations.", false, 1.0f);
private static final Map<String, PropertyDescriptor> PROPERTY_DESCRIPTORS_BY_NAME = asFixedMap(new PropertyDescriptor[] { SKIP_ANNOTATIONS });
public static class ExceptionParser {
private static final char ESCAPE_CHAR = '\\';
@ -122,6 +130,11 @@ public class AvoidDuplicateLiteralsRule extends AbstractRule {
return data;
}
// Skip literals in annotations
if (getBooleanProperty(SKIP_ANNOTATIONS) && node.getFirstParentOfType(ASTAnnotation.class) != null) {
return data;
}
if (literals.containsKey(node.getImage())) {
List<ASTLiteral> occurrences = literals.get(node.getImage());
occurrences.add(node);
@ -133,5 +146,10 @@ public class AvoidDuplicateLiteralsRule extends AbstractRule {
return data;
}
@Override
protected Map<String, PropertyDescriptor> propertiesByName() {
return PROPERTY_DESCRIPTORS_BY_NAME;
}
}