Skip to content

[java] MissingStaticMethodInNonInstantiatableClass should consider Lombok's @UtilityClass #4224

@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 @UtilityClass generates a private constructor private Test() which throws an exception.So the class only has a private constructor and does not have any static methods or fields which cannot be used. But PMD considers the class doesn't have a constructor and does not report MissingStaticMethodInNonInstantiatableClass at line 4 which causes false negatives.

Lombok documentation: https://projectlombok.org/features/experimental/UtilityClass

Code Sample demonstrating the issue:

Original code:

import lombok.experimental.UtilityClass;

@UtilityClass
public final class Test {
    private int x;
}

Changed:

public final class Test {
    private static int x;

    private Test() {
        throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
    }
}

Expected outcome:

PMD should report a violation at line 4, but not. That's a false negative.

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    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