Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] ConstructorCallsOverridableMethod occurs when unused overloaded method is defined #2348

Closed
mistercam opened this issue Mar 11, 2020 · 1 comment · Fixed by #4101
Closed
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@mistercam
Copy link

mistercam commented Mar 11, 2020

Affects PMD Version: 6.11.0, 6.21.0

Rule: ConstructorCallsOverridableMethod

Description:
If a constructor calls a non-overridable public method, and this method has an overridable overloaded version (which is not called from the constructor or from the non-overridable version,) then a ConstructorCallsOverridableMethod rule exception is raised.

Code Sample demonstrating the issue:

public class Scratch {
    private Long member = 0L;

    public Scratch(final Long v) {
        setMember(v);
    }

    public final void setMember(final Long v) {
        this.member = v;
    }

    // Renaming this or marking it as final prevents the
    // 'ConstructorCallsOverridableMethod' rule from being broken.
    public void setMember(final String n) {
        setMember(Long.valueOf(n));
    }
}

Running PMD through: Gradle (with version 6.11.0) and CLI (with version 6.21.0)

@jsotuyod jsotuyod added a:false-negative PMD doesn't flag a problematic piece of code in:symbol-table Affects the symbol table code labels Mar 11, 2020
@mistercam mistercam changed the title [java] ConstructorCallsOverridableMethod occurs when unused overloaded method is used [java] ConstructorCallsOverridableMethod occurs when unused overloaded method is defined Mar 11, 2020
@adangel
Copy link
Member

adangel commented Mar 12, 2020

Another example:

import java.util.Date;

public class MyTimestamp {
    private final long t;
    public MyTimestamp() {
        t = new Date().getTime(); // <-- false-positive violation here
    }
    public long getTime() {
        return t;
    }
}

@adangel adangel added a:false-positive PMD flags a piece of code that is not problematic and removed in:symbol-table Affects the symbol table code a:false-negative PMD doesn't flag a problematic piece of code labels Sep 29, 2022
@adangel adangel added this to the 6.50.0 milestone Sep 29, 2022
adangel added a commit to adangel/pmd that referenced this issue Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants