From a6a8d7e14a6524e3eeabf3c420566597bf50480b Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Wed, 13 Sep 2006 03:24:24 +0000 Subject: [PATCH] Applied patch from Xavier Le Vourch to reduce false postives from CloneMethodMustImplementCloneable. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4505 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 1 + .../clone/CloneMethodMustImplementCloneableTest.java | 5 +++++ pmd/rulesets/clone.xml | 11 +++++++++-- pmd/xdocs/credits.xml | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index f60b37f277..98f0f182a6 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -34,6 +34,7 @@ Added new XSLT stylesheet for CPD XML->HTML from Max Tardiveau. Refactored UseIndexOfChar to extract common functionality into AbstractPoorMethodCall. Improved CPD GUI and Designer look/functionality; thanks to Brian Remedios for the changes! Applied patch from Jason Bennett to enhance CyclomaticComplexity rule to account for conditional or/and nodes, do stmts, and catch blocks. +Applied patch from Xavier Le Vourch to reduce false postives from CloneMethodMustImplementCloneable. Performance Refactoring, XPath rules re-written as Java: BooleanInstantiation UselessOperationOnImmutable diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/clone/CloneMethodMustImplementCloneableTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/clone/CloneMethodMustImplementCloneableTest.java index 6e3321f2a7..96c9b65e2f 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/clone/CloneMethodMustImplementCloneableTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/clone/CloneMethodMustImplementCloneableTest.java @@ -19,6 +19,7 @@ public class CloneMethodMustImplementCloneableTest extends SimpleAggregatorTst { new TestDescriptor(TEST1, "ok, implements Cloneable", 0, rule), new TestDescriptor(TEST2, "bad, doesn't implement Cloneable", 1, rule), new TestDescriptor(TEST3, "ok, not Object.clone since method has a param", 0, rule), + new TestDescriptor(TEST4, "ok, doesn't implement Cloneable but only throw CloneNotSupportedException", 0, rule), }); } @@ -36,4 +37,8 @@ public class CloneMethodMustImplementCloneableTest extends SimpleAggregatorTst { "public class Foo {" + PMD.EOL + " void clone(int x) {}" + PMD.EOL + "}"; + private static final String TEST4 = + "public class Foo {" + PMD.EOL + + " final Object clone() { throw new CloneNotSupportedException(); }" + PMD.EOL + + "}"; } diff --git a/pmd/rulesets/clone.xml b/pmd/rulesets/clone.xml index 7eb0a7115a..c533ce5fd6 100644 --- a/pmd/rulesets/clone.xml +++ b/pmd/rulesets/clone.xml @@ -89,7 +89,7 @@ and count(NameList/Name[contains class="net.sourceforge.pmd.rules.XPathRule" externalInfoUrl="http://pmd.sourceforge.net/rules/clone.html#CloneMethodMustImplementCloneable"> -The method clone() should only be implemented if the class implements the Cloneable interface +The method clone() should only be implemented if the class implements the Cloneable interface with the exception of a final method that only throws CloneNotSupportedException @@ -98,8 +98,15 @@ The method clone() should only be implemented if the class implements the Clonea //ClassOrInterfaceDeclaration [not(./ImplementsList/ClassOrInterfaceType [@Image='Cloneable'])] -[.//MethodDeclaration/MethodDeclarator[@Image +[.//MethodDeclaration +[MethodDeclarator[@Image = 'clone' and count(FormalParameters/*) = 0]] +[not((../MethodDeclaration[@Final = 'true']) +and Block[count(BlockStatement)=1] +/BlockStatement/Statement/ThrowStatement/Expression +/PrimaryExpression/PrimaryPrefix/AllocationExpression +/ClassOrInterfaceType[@Image = 'CloneNotSupportedException'])]] + ]]> diff --git a/pmd/xdocs/credits.xml b/pmd/xdocs/credits.xml index 732353b3be..1b1afee013 100644 --- a/pmd/xdocs/credits.xml +++ b/pmd/xdocs/credits.xml @@ -48,6 +48,7 @@
    +
  • Xavier Le Vourch - Patch to improve CloneMethodMustImplementCloneable, suggestions on cleaning up casts in grammar, SimplifyBooleanAssertion, patch to fix problem with TestClassWithoutTestCases, patch to fix rule name bugs in migration rulesets
  • Jason Bennett - Patch to improve CyclomaticComplexity rule
  • Wouter Zelle - Wrote BrokenNullCheck rule, fixed a false positive in InefficientStringBuffering, fixed a false positive in NonThreadSafeSingleton, a nice patch to clean up some of the Ant task properties and fix a TextRenderer bug, rewrote PositionLiteralsFirstInComparisons in XPath, Renderer improvement suggestions, wrote NonThreadSafeSingleton rule, wrote DefaultPackage rule, worked thru ASTMethodDeclaration.isSyntacticallyX design, reported docs bug 1292689 for UnnecessaryLocalBeforeReturn, reported leftover ExceptionTypeChecking source file, rewrote UselessOverridingMethod in Java, UselessOverridingMethod rule, ProperLogger rule, nifty code to allow variables in XPath rules, externalInfoUrl data for all rules in basic and unusedcode rulesets, some very nifty XSLT, improvements to UseCorrectExceptionLogging, designed and implemented the "externalInfoUrl" feature in the rule definitions, fixed a devious bug in RuleSetFactory, AvoidPrintStackTrace, initial implementation of SimplifyConditional
  • Sven Jacob - Some fixes for the DFA layer.
  • @@ -77,7 +78,6 @@
  • David Karr - reported stale XPath documentation
  • Dawid Weiss - Reported bug in UnusedPrivateMethod
  • Shao Lo - Reported bug in CPD
  • -
  • Xavier Le Vourch - Suggestions on cleaning up casts in grammar, SimplifyBooleanAssertion, patch to fix problem with TestClassWithoutTestCases, patch to fix rule name bugs in migration rulesets
  • Mathieu Champlon - Added language support to the CPD Ant task
  • Uroshnor - Reported bug in UseNotifyAllInsteadOfNotify
  • Jan Koops - Noted missing data in MemberValuePair nodes, bug report for JBuilder plugin