Affects PMD Version: 7.2
Rule:
Please provide the rule name and a link to the rule documentation:
https://docs.pmd-code.org/pmd-doc-7.2.0/pmd_rules_java_bestpractices.html#unusedprivatemethod
Description:
The below getter says
<violation beginline="59" endline="59" begincolumn="42" endcolumn="50" rule="UnusedPrivateMethod" ruleset="Best Practices" class="Utils" method="getValue" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.2.0/pmd_rules_java_bestpractices.html#unusedprivatemethod" priority="3">
Avoid unused private methods such as 'getValue(T, Function<T, Long>)'.
</violation>
However the method is being used and hence it is a false positive. Note that if I use lombok @Getter
instead of explicit id
method then pmd error is not reported.
Code Sample demonstrating the issue:
import java.util.function.Function; public class Main { public static void main(String[] args) { new Utils().printValue(); } public enum ConsentStatus { NOT_APPLICABLE(0L), NO_CONSENT(1L), CONSENT(2L), UNKNOWN_STATUS(3L); private final long id; ConsentStatus(long id) { this.id = id; } public long id() { return this.id; } } public enum OptOutStatus { NOT_APPLICABLE(0L), OPTED_OUT(1L), DID_NOT_OPT_OUT(2L), UNKNOWN_STATUS(3L); private final long id; OptOutStatus(long id) { this.id = id; } public long id() { return this.id; } } static class Utils { public void printValue() { System.out.println(getValue(ConsentStatus.CONSENT)); System.out.println(getValue(ConsentStatus.NO_CONSENT)); System.out.println(getValue(OptOutStatus.DID_NOT_OPT_OUT)); System.out.println(getValue(OptOutStatus.OPTED_OUT)); } private Long getValue(ConsentStatus val) { return getValue(val, ConsentStatus::id); } private Long getValue(OptOutStatus val) { return getValue(val, OptOutStatus::id); } private <T extends Enum<T>> Long getValue(T enumValue, Function<T, Long> fn) { if (enumValue == null) { return null; } return fn.apply(enumValue); } } }
Expected outcome:
PMD error at line 59 but it should not be sicne the method is being used.
Running PMD through: Maven
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4