Skip to content

[java] False positives for Cloneable rules #3104

@linusjf

Description

@linusjf

Affects PMD Version:
6.31.0

Rule(s):
CloneMethodMustImplementCloneable
CloneThrowsCloneNotSupportedException
CloneReturnTypeMustMatchClassName

Description:
False positives for above three rules are reported as follows( ignore line numbers):

DSAlgos/src/main/java/ds/ITreeNode.java:36:	CloneMethodMustImplementCloneable:	clone() method should be implemented only if implementing Cloneable interface

DSAlgos/src/main/java/ds/ITreeNode.java:36:	CloneThrowsCloneNotSupportedException:	clone() method should throw CloneNotSupportedException

DSAlgos/src/main/java/ds/TreeNode.java:209:	CloneMethodReturnTypeMustMatchClassName:	The return type of the clone() method must be the class name when implements Cloneable

DSAlgos/src/main/java/ds/TreeNode.java:209:	CloneThrowsCloneNotSupportedException:	clone() method should throw CloneNotSupportedException

Code Sample demonstrating the issue:

package ds; 
interface ITreeNode<E> extends Cloneable { 
ITreeNode<E> clone(); 
}

package ds; 
public class TreeNode<T extends Comparable<T>> implements ITreeNode<T> { 
@Override public ITreeNode<T> clone() { 
try { 
return (ITreeNode<T>) super.clone();
 } catch (CloneNotSupportedException cnse) {
 throw new AssertionError("Shouldn't reach here...", cnse); }
 } 
}

Expected outcome:

  • No violations must be reported.

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

First posted here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions