#1532 [java] CloneMethodMustImplementCloneable: Implemented Interface extends Cloneable

PMD-5.3.8
closed
PMD
3-Major
Bug
5.5.1
java-typeresolution
2016-11-23
2016-10-28
No

If my class implements an interface that is extending the Cloneable interface I get the warning clone() method should be implemented only if implementing Cloneable interface. which is actually not true in this case.

Compare to https://sourceforge.net/p/pmd/bugs/681/ and https://sourceforge.net/p/pmd/bugs/863/ which note the same problem for super classes directly implementing Cloneable.

  interface TestInterface extends Cloneable {
    TestInterface clone();
  }

  class CloneableClass implements TestInterface {
    @Override // creates a warning though CloneableClass is actually implementing Cloneable
    public CloneableClass clone() {
      // clone implementation
    }
  }

Discussion

  • Andreas Dangel

    Andreas Dangel - 2016-11-04
    • labels: cloneable --> cloneable, java, java-typeresolution
    • summary: CloneMethodMustImplementClonable - Implemented Interface extends Cloneable --> [java] CloneMethodMustImplementClonable - Implemented Interface extends Cloneable
    • status: open --> in-progress
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.8
    • Ruleset / Rule: net.sourceforge.pmd.lang.java.typeresolution.rules.CloneMethodMustImplementCloneable --> java-typeresolution
     
  • Andreas Dangel

    Andreas Dangel - 2016-11-04

    Please note, that this bug will only be fixed for the implementation, that uses typeresolution (from the java-typeresolution ruleset).

     
  • Andreas Dangel

    Andreas Dangel - 2016-11-04
    • summary: [java] CloneMethodMustImplementClonable - Implemented Interface extends Cloneable --> [java] CloneMethodMustImplementCloneable: Implemented Interface extends Cloneable
     
  • Andreas Dangel

    Andreas Dangel - 2016-11-04
    • status: in-progress --> closed
     
  • Tobias Wörenkämper

    @Andreas: Thanks for taking care of my entry. 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();
    }
    
     
  • Tobias Wörenkämper

    Opened a new ticket: #1550

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks