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] False negative in ClassWithOnlyPrivateConstructorsShouldBeFinal #5072

Open
Chiky777 opened this issue Jun 18, 2024 · 1 comment
Open
Labels
a:false-negative PMD doesn't flag a problematic piece of code

Comments

@Chiky777
Copy link

Affects PMD Version: 7.2.0

Rule: ClassWithOnlyPrivateConstructorsShouldBeFinal
https://docs.pmd-code.org/latest/pmd_rules_java_design.html#classwithonlyprivateconstructorsshouldbefinal

Description:
I found a regresson in the rule ClassWithOnlyPrivateConstructorsShouldBeFinal as @AllArgsConstructor annotation was added to the filter that assumes the annotation always generates a public constructor at ClassWithOnlyPrivateConstructorsShouldBeFinal.java. However, PMD does not consider the parameters of the annotation. When using the staticName="build" , the class does not generate a public constructor, leading to a false negative. Case2 is equivalent to Case1, and PMD reported a warning.

Code Sample demonstrating the issue:

Case1

import lombok.AllArgsConstructor;
@AllArgsConstructor(staticName = "build")
public class Person {    //report no warnings
    private String name;
    private  int age;
    private Person(){}
}

Case2

public class Person {
    private String name;
    private int age;

    private Person() {    //report a warning
    }

    @java.lang.SuppressWarnings("all")
    private Person(final String name, final int age) {
        this.name = name;
        this.age = age;
    }

    @java.lang.SuppressWarnings("all")
    public static Person build(final String name, final int age) {
        return new Person(name, age);
    }
}

Expected outcome:

PMD does not report a violation at line 3 in the first case, but that's wrong. That's a false negative.

Running PMD through: [CLI]

@Chiky777 Chiky777 added the a:false-negative PMD doesn't flag a problematic piece of code label Jun 18, 2024
@Chiky777
Copy link
Author

@RequiredArgsConstructor(staticName="build") and @NoArgsConstructor(staticName="build") will also cause this issue

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

1 participant