Menu

#1550 CloneMethodMustImplementCloneable: Warning 'clone() method should be implemented' Though Interface Extends Cloneable

New Tickets
closed
PMD
3-Major
Bug
5.5.2
java-typeresolution
2016-12-07
2016-11-23
No

This is a follow up to #1532

@Andreas: Thanks for taking care of my entry #1532. I got the chance to try out your fix with PMD 5.5.2 and unfortunately I still get a warning 'clone() method should be implemented only if implementing Cloneable interface.' for the following line in the TestInterface implementing class CloneableClass from my initial example:

public CloneableClass clone() {

Actually I had a look at your test case '#1532 [java] CloneMethodMustImplementCloneable: Implemented Interface extends Cloneable' from https://github.com/pmd/pmd/commit/a9d0de9450f61aee3a533dceecb77b4a376faa83 which should exactly cover this case.

So I am worried if I am doing something wrong.
This is the configuration I am using to force the use of the typeresolution rule:

    <rule name="CloneMethodMustImplementCloneable"
        since="3.9"
        message="clone() method should be implemented only if implementing Cloneable interface"
        class="net.sourceforge.pmd.lang.java.typeresolution.rules.CloneMethodMustImplementCloneable"
        typeResolution="true"
        externalInfoUrl="https://pmd.github.io/pmd-5.5.2/pmd-java/rules/java/typeresolution.html#CloneMethodMustImplementCloneable">
        <description>
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. This version uses PMD's type resolution facilities,
and can detect if the class implements or extends a Cloneable class.
        </description>
        <priority>3</priority>
        <example>
        <![CDATA[
        public class MyClass {
            public Object clone() throws CloneNotSupportedException {
                return foo;
            }
        }
        ]]>
        </example>
    </rule>

By the way, the warning for this is gone:

interface TestInterface extends Cloneable {
    TestInterface clone();
}

Discussion

  • Andreas Dangel

    Andreas Dangel - 2016-12-03
    • status: open --> more-info-needed
    • Milestone: PMD-5.5.3 --> New Tickets
     
  • Andreas Dangel

    Andreas Dangel - 2016-12-03

    Hi Tobias,

    how do you execute PMD exactly? As the bugfix for #1532 depends on typeresolution, you need to make sure, to have all the compiled classes on the "auxclasspath" for PMD.
    If you run PMD via command line, it's the -auxclasspath option (see Running PMD). Ant also has this option via the auxclasspath nested element (see Ant task usage). Maven and Gradle usually enable this option by default (however, it can be disabled...).

    Regards,
    Andreas

     
  • Tobias Wörenkämper

    We use it via Ant. And we indeed missed to set the auxclasspath. Thank you very much for your great help!

     
  • Juan Sotuyo

    Juan Sotuyo - 2016-12-07
    • status: more-info-needed --> closed
     

Log in to post a comment.

MongoDB Logo MongoDB