From 799f7d6002aa4ad246242b66904a4fa7ef42c335 Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Fri, 23 Sep 2011 15:40:34 +0000 Subject: [PATCH] apply patch 3175832 from Cd-MaN: Add options to to the CPD command line task. While applying the patch, I discover that properties where never really used and not passed down to the language implementation. In order to fix that and keep the loose coupling we currently have, I used the system properties. Also did isolate that in a separate function and did a (little) code cleaning. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7310 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/bin/cpd.sh | 3 ++- pmd/etc/changelog.txt | 1 + pmd/src/net/sourceforge/pmd/cpd/CPD.java | 22 ++++++++++++++++++- .../net/sourceforge/pmd/cpd/JavaLanguage.java | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/pmd/bin/cpd.sh b/pmd/bin/cpd.sh index eec53e3a06..422388f935 100755 --- a/pmd/bin/cpd.sh +++ b/pmd/bin/cpd.sh @@ -19,6 +19,7 @@ if [ -z "$1" ]; then echo " $script " exit 1 fi +shift # OS specific support. $var _must_ be set to either true or false. cygwin=false; @@ -82,4 +83,4 @@ if $cygwin; then DIRECTORY=`cygpath --windows "$DIRECTORY"` fi -java $HEAPSIZE -cp $classpath net.sourceforge.pmd.cpd.CPD --minimum-tokens $MINIMUM_TOKENS --files $DIRECTORY --language $LANGUAGE +java $HEAPSIZE -cp $classpath net.sourceforge.pmd.cpd.CPD --minimum-tokens $MINIMUM_TOKENS --files $DIRECTORY --language $LANGUAGE ${@} diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 71d8325bdc..36005f9488 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -321,6 +321,7 @@ Rules can now use property values in messages, for example ${propertyName} will Rules can now use violation specific values in messages, specifically ${variableName}, ${methodName}, ${className}, ${packageName}. New XPath function 'getCommentOn' can be used to search for strings in comments - Thanks to Andy Throgmorton Add .hxx and .hpp as valid file extension for CPD - Thanks to Ryan Pavlik +Add options to to the CPD command line task - Thanks to Cd-Man Other changes: Rule property API upgrades: diff --git a/pmd/src/net/sourceforge/pmd/cpd/CPD.java b/pmd/src/net/sourceforge/pmd/cpd/CPD.java index b8e9fc1d53..dcc51e3f4c 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/CPD.java +++ b/pmd/src/net/sourceforge/pmd/cpd/CPD.java @@ -10,6 +10,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.TreeMap; @@ -154,22 +155,41 @@ public class CPD { return defaultValue; } + private static void setSystemProperties(String[] args) { + boolean ignoreLiterals = findBooleanSwitch(args, "--ignore-literals"), + ignoreIdentifiers = findBooleanSwitch(args, "--ignore-identifiers"); + Properties properties = System.getProperties(); + if (ignoreLiterals) { + properties.setProperty(JavaTokenizer.IGNORE_LITERALS, "true"); + } + if (ignoreIdentifiers) { + properties.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true"); + } + System.setProperties(properties); + } + public static void main(String[] args) { if (args.length == 0) { usage(); } try { - boolean skipDuplicateFiles = findBooleanSwitch(args, "--skip-duplicate-files"); + String languageString = findOptionalStringValue(args, "--language", "java"); String formatString = findOptionalStringValue(args, "--format", "text"); String encodingString = findOptionalStringValue(args, "--encoding", System.getProperty("file.encoding")); int minimumTokens = Integer.parseInt(findRequiredStringValue(args, "--minimum-tokens")); LanguageFactory f = new LanguageFactory(); + // Pass extra paramteters as System properties to allow language + // implementation to retrieve their associate values... + setSystemProperties(args); + Language language = f.createLanguage(languageString); Renderer renderer = CPD.getRendererFromString(formatString, encodingString); CPD cpd = new CPD(minimumTokens, language); cpd.setEncoding(encodingString); + + boolean skipDuplicateFiles = findBooleanSwitch(args, "--skip-duplicate-files"); if (skipDuplicateFiles) { cpd.skipDuplicates(); } diff --git a/pmd/src/net/sourceforge/pmd/cpd/JavaLanguage.java b/pmd/src/net/sourceforge/pmd/cpd/JavaLanguage.java index 5a016bf7e1..658b3017e2 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/JavaLanguage.java +++ b/pmd/src/net/sourceforge/pmd/cpd/JavaLanguage.java @@ -7,7 +7,7 @@ import java.util.Properties; public class JavaLanguage extends AbstractLanguage { public JavaLanguage() { - this(new Properties()); + this(System.getProperties()); } public JavaLanguage(Properties properties) {