From 64012cc0b4008439f09b73197814ccb1c8899938 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Wed, 16 Sep 2015 21:37:17 +0200 Subject: [PATCH] verified #1405 UnusedPrivateMethod false positive? --- .../unusedcode/xml/UnusedPrivateMethod.xml | 17 +++++++++++ .../pmd/testframework/RuleTst.java | 29 +++++++++++++++---- .../pmd/testframework/TestDescriptor.java | 9 ++++++ src/site/markdown/overview/changelog.md | 1 + 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml index 83c72e23d9..11ad31a37c 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml @@ -1484,6 +1484,23 @@ public class Something { private static Map mapOf2(final Function keyMapper, final V... values) { return Stream.of(values).collect(Collectors.toMap(keyMapper, Function.identity())); } +} + ]]> + + + + #1405 UnusedPrivateMethod false positive? + 0 + getProductImageUrls(final ApparelStyleVariantProductModel product, final String format) { + return getImageUrlsListForVariant(product, format); + } + private List getImageUrlsListForVariant(final VariantProductModel variant, final String format) { + final SortedMap imageUrls = getImageUrlsMapForVariant(variant, format); + return new ArrayList(imageUrls.values()); + } } ]]> diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java index 8827c02ed3..023bbc6d2e 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/RuleTst.java @@ -94,7 +94,7 @@ public abstract class RuleTst { } } - report = processUsingStringReader(test.getCode(), rule, test.getLanguageVersion()); + report = processUsingStringReader(test, rule); res = report.size(); } catch (Throwable t) { t.printStackTrace(); @@ -190,10 +190,9 @@ public abstract class RuleTst { System.out.println("--------------------------------------------------------------"); } - private Report processUsingStringReader(String code, Rule rule, - LanguageVersion languageVersion) throws PMDException { + private Report processUsingStringReader(TestDescriptor test, Rule rule) throws PMDException { Report report = new Report(); - runTestFromString(code, rule, report, languageVersion); + runTestFromString(test, rule, report); return report; } @@ -201,10 +200,16 @@ public abstract class RuleTst { * Run the rule on the given code and put the violations in the report. */ public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion) { + runTestFromString(code, rule, report, languageVersion, true); + } + + public void runTestFromString(String code, Rule rule, Report report, LanguageVersion languageVersion, boolean isUseAuxClasspath) { try { PMD p = new PMD(); p.getConfiguration().setDefaultLanguageVersion(languageVersion); - p.getConfiguration().prependClasspath("."); // configure the "auxclasspath" option for unit testing + if (isUseAuxClasspath) { + p.getConfiguration().prependClasspath("."); // configure the "auxclasspath" option for unit testing + } RuleContext ctx = new RuleContext(); ctx.setReport(report); ctx.setSourceCodeFilename("n/a"); @@ -218,6 +223,10 @@ public abstract class RuleTst { } } + public void runTestFromString(TestDescriptor test, Rule rule, Report report) { + runTestFromString(test.getCode(), rule, report, test.getLanguageVersion(), test.isUseAuxClasspath()); + } + /** * getResourceAsStream tries to find the XML file in weird locations if the * ruleName includes the package, so we strip it here. @@ -307,6 +316,15 @@ public abstract class RuleTst { } } + boolean isUseAuxClasspath = true; + Node useAuxClasspathAttribute = testCode.getAttributes().getNamedItem("useAuxClasspath"); + if (useAuxClasspathAttribute != null) { + String useAuxClasspathValue = useAuxClasspathAttribute.getNodeValue(); + if ("false".equalsIgnoreCase(useAuxClasspathValue)) { + isUseAuxClasspath = false; + } + } + NodeList ruleProperties = testCode.getElementsByTagName("rule-property"); Properties properties = new Properties(); for (int j = 0; j < ruleProperties.getLength(); j++) { @@ -373,6 +391,7 @@ public abstract class RuleTst { } tests[i].setReinitializeRule(reinitializeRule); tests[i].setRegressionTest(isRegressionTest); + tests[i].setUseAuxClasspath(isUseAuxClasspath); tests[i].setExpectedMessages(messages); tests[i].setExpectedLineNumbers(expectedLineNumbers); tests[i].setProperties(properties); diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java index 83a91b5152..15fe1f767c 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java @@ -27,6 +27,7 @@ public class TestDescriptor { private LanguageVersion languageVersion; private boolean reinitializeRule = true; //default, avoids unintentional mixing of state between test cases private boolean isRegressionTest = true; + private boolean useAuxClasspath = true; private int numberInDocument = -1; // Empty descriptor added to please mvn surefire plugin @@ -135,4 +136,12 @@ public class TestDescriptor { public void setRegressionTest(boolean isRegressionTest) { this.isRegressionTest = isRegressionTest; } + + public void setUseAuxClasspath(boolean useAuxClasspath) { + this.useAuxClasspath = useAuxClasspath; + } + + public boolean isUseAuxClasspath() { + return useAuxClasspath; + } } diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 2d089767b8..b06a315afb 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -28,5 +28,6 @@ * [#1401](https://sourceforge.net/p/pmd/bugs/1401/): False positive for StringBuilder.append called with constructor * [#1402](https://sourceforge.net/p/pmd/bugs/1402/): Windows-Only: File exclusions are not case insensitive * [#1403](https://sourceforge.net/p/pmd/bugs/1403/): False positive UnusedPrivateMethod with JAVA8 +* [#1405](https://sourceforge.net/p/pmd/bugs/1405/): UnusedPrivateMethod false positive? **API Changes:**