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
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
@UtilityClassgenerates a private constructorprivate 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 reportMissingStaticMethodInNonInstantiatableClassat line 4 which causes false negatives.Lombok documentation: https://projectlombok.org/features/experimental/UtilityClass
Code Sample demonstrating the issue:
Original code:
Changed:
Expected outcome:
PMD should report a violation at line 4, but not. That's a false negative.
Running PMD through: CLI