diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 8842e22a3a..6c3d38aaa7 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/AvoidDuplicateLiterals.xml b/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/AvoidDuplicateLiterals.xml index c26d6d3d20..36ef0aef16 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/AvoidDuplicateLiterals.xml +++ b/pmd/regress/test/net/sourceforge/pmd/rules/strings/xml/AvoidDuplicateLiterals.xml @@ -37,6 +37,43 @@ duplicate literals in field decl + + + + 1 + + + + + 0 + true + diff --git a/pmd/src/net/sourceforge/pmd/rules/strings/AvoidDuplicateLiteralsRule.java b/pmd/src/net/sourceforge/pmd/rules/strings/AvoidDuplicateLiteralsRule.java index ad16b09321..257376b814 100644 --- a/pmd/src/net/sourceforge/pmd/rules/strings/AvoidDuplicateLiteralsRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/strings/AvoidDuplicateLiteralsRule.java @@ -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 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 occurrences = literals.get(node.getImage()); occurrences.add(node); @@ -133,5 +146,10 @@ public class AvoidDuplicateLiteralsRule extends AbstractRule { return data; } + + @Override + protected Map propertiesByName() { + return PROPERTY_DESCRIPTORS_BY_NAME; + } }