GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] ProperCloneImplementation not valid for final class · Issue #2410 · pmd/pmd

[java] ProperCloneImplementation not valid for final class #2410

New issue
Copy link
New issue
Copy link
Closed
#2699
Closed
[java] ProperCloneImplementation not valid for final class#2410
#2699
Copy link
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.27.0

Description

@numeralnathan
numeralnathan
opened on Apr 10, 2020
Issue body actions

Affects PMD Version:

6.21.0

Rule:

ProperCloneImplementation

Description:

The rule ProperCloneImplementation flags a problem on clone(). However, it is my opinion that this is perfectly valid since MyClass is declared final. Is my opinion correct or does it need corrected?

The documentation should be enhanced to state that declaring the class as final is sufficient.

Code Sample demonstrating the issue:

public final class MyClass implements Cloneable
{
   public MyClass clone()
   {
      return new MyClass();
   }

Expected outcome:

PMD is showing a false-positive

Running PMD through:

Eclipse

Activity

  1. numeralnathan
    added
    a:bugPMD crashes or fails to analyse a file.
    on Apr 10, 2020
  2. adangel commented on Aug 21, 2020

    @adangel
    adangel
    on Aug 21, 2020
    Member
    More actions

    I think, you are right. This is described e.g. in Effective Java, Item 11 (available e.g. here: http://jtechies.blogspot.com/2012/07/item-11-override-clone-judiciously.html)

    The clone method must return a instance of the class being cloned. That means in your example:

    MyClass mc = new MyClass();
    Object clone = mc.clone();
    assert mc.getClass() == clone.getClass(); // this is true

    Since MyClass is final and therefore cannot have sub classes, this is fine. The returned type is always correct.

    If MyClass would not be final then it could have a subclass (e.g. MySubClass). The implementation would be wrong:

    In practice, programmers assume that if they extend a class and invoke super.clone from the subclass, the returned object will be an instance of the subclass.

    class MySubClass extends MyClass implements Cloneable {
        public MySubClass clone() {
            return (MySubClass) super.clone(); // runtime error
        }
    }

    The only way to get the correct class while cloning when being a "intermediate" class (like the non-final MyClass example) is to call super.clone() - which eventually calls Object's clone() - which creates a new instance of the correct subtype. Otherwise the intermediate class would need to know about all the subtypes (which is not possible as long as the class it not sealed).

  3. adangel
    added
    a:false-positivePMD flags a piece of code that is not problematic
    and removed
    a:bugPMD crashes or fails to analyse a file.
    on Aug 21, 2020
  4. adangel
    added this to the 6.27.0 milestone on Aug 21, 2020
  5. adangel
    closed this as completedin #2699on Aug 21, 2020
  6. adangel
    added a commit that references this issue on Aug 21, 2020

    [doc] Update release notes, fixes #2410, refs #2699

    This issue will close once commit 18abedc is merged into the 'main' branch.
    18abedc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.27.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.