Skip to content

[java] AccessorClassGeneration false-negative: subclass calls private constructor #1998

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

Closed
Vampire opened this issue Sep 2, 2019 · 2 comments
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Milestone

Comments

@Vampire
Copy link
Contributor

Vampire commented Sep 2, 2019

Rule: AccessorClassGeneration

Possibly a false-negative.

Having a sub-class that calls a private constructor is bad practice.
It requires the compiler adding another package-private synthetic constructor with an additional synthetic anonymous inner class parameter that forwards to the private constructor so that the subclass has something non-private to call.

Examples for this would be for example

class Foo {
    private Foo() { }
    public Foo(String foo) { }
    private static class Bar extends Foo { }
}

where the default constructor of Bar calls the private no-arg constructor of Foo or

class Foo {
    private Foo() { }
    private Foo(String foo) { }
    public Foo(String foo, String bar) { }
    private static class Bar extends Foo {
        public Bar() { super(null); }
    }
}

where the call to the private constructor is explicit.

This adds unnecessarily additional constructors and also can cause problems if you use the class from Groovy code, as in the first example new Foo(null) and in the second example new Foo(null, null) can not be executed as there is a disambiguity between the constructor you meant to call and the synthetic added constructor.

It is usually better practice to simply make that constructor package-private direclty, eliminating the need for any synthetic constructors.

@jsotuyod
Copy link
Member

jsotuyod commented Sep 4, 2019

@Vampire this should be already covered by java-bestpractices/AccessorClassGeneration.

It's possible however that the rule misses these scenarios, as there is no explicit call to the private constructor / it's through a call to super, in which case this should be flagged as a false negative. Could you please confirm this and update the report accordingly?

@adangel adangel changed the title [java] New Rule: subclass calls private constructor [java] AccessorClassGeneration false-negative: subclass calls private constructor Oct 29, 2019
@adangel adangel added the a:false-negative PMD doesn't flag a problematic piece of code label Oct 29, 2019
oowekyala added a commit to oowekyala/pmd that referenced this issue Nov 5, 2020
@oowekyala oowekyala added this to the 7.0.0 milestone Nov 10, 2020
@oowekyala oowekyala linked a pull request Nov 10, 2020 that will close this issue
5 tasks
@oowekyala oowekyala removed a link to a pull request Nov 10, 2020
5 tasks
@adangel
Copy link
Member

adangel commented Dec 11, 2020

Fixed via #2899 for PMD 7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-negative PMD doesn't flag a problematic piece of code
Projects
None yet
Development

No branches or pull requests

4 participants