Skip to content

[java] MissingStaticMethodInNonInstantiatableClass should consider Lombok's @NoArgsConstructor #4225

@ghost

Description

Affects PMD Version:6.51.0

Rule: MissingStaticMethodInNonInstantiatableClass

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_errorprone.html#missingstaticmethodinnoninstantiatableclass

Description:
In the following code example, the @NoArgsConstructor generates a public constructor public C() but PMD considers the C class only has private constructor and does not have any static methods or fields cannot be used which triggers MissingStaticMethodInNonInstantiatableClass

Lombok Documentation: https://projectlombok.org/features/constructor

Note: there are more annotations, like @RequiredArgsConstructor and @AllArgsConstructor.

Code Sample demonstrating the issue:

Original code:

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class C {
    private C(int a, int b, int c, String abc, long d, double p,
                             String[] arr, int data, long in, float fl, String res) { }
}

Changed:

public class C {
    private C(int a, int b, int c, String abc, long d, double p, String[] arr, int data, long in, float fl, String res) {
    }

    public C() {
    }
}

Expected outcome:

PMD reports a violation at line 4, but that's wrong. That's a false positive.

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions