You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.m().n() breaks the law where a.m() does not.
According to LawOfDemeter described in Wikipedia, the chain call should be warned. However, PMD failed to warn the chain call in get_nowarning() at point 1 (line 12).
As per #3840 (comment) below, the call chain is not detected, which is expected. However, moving the argument to allOf into a local variable produces a false positive, as shown at point 2 (line 19).
I don't think those are false negatives. Here you create a CompletableFuture using allOf, which produces a local object. If you can't call methods on local objects, then you can't do anything with them, and so you basically can't use the language. To me, your "true positive" is a false positive and should be removed.
More context: in PMD 7 the rule has been reimplemented and rationalized differently. Formulating LawOfDemeter as "use only one dot" is way too simplistic, as the rule is then so noisy that it is practically useless. The new rule instead says that you can only call methods on "trusted values", where eg, a formal parameter is trusted, fields of this are trusted, and an object created locally is trusted. Calling a getter on a "trusted value" produces an "untrusted value". The new rule only reports getter calls on untrusted values. This is closer to how Wikipedia defines the law of Demeter as "only talk to your friends" (here, trusted values are your friends), and significantly less noisy than "use only one dot".
In your example, the field futures is trusted. futures.toArray() is trusted as well, as it doesn't breach a boundary of abstraction, just converts futures into another format. The result of CompletableFuture.allOf is trusted, as it is an object created locally (allOf is a static construction method). That means you can call join on it without violation, and your example shouldn't report value code.
oowekyala
changed the title
[java] PMD failed to detect LawOfDemeter violation
[java] LawOfDemeter disallows method call on locally created object
Mar 11, 2022
Affects PMD Version: 6.43.0
Rule: LawOfDemeter
Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_design.html#lawofdemeter
Description:
According to LawOfDemeter described in Wikipedia, the chain call should be warned. However, PMD failed to warn the chain call inget_nowarning()
at point 1 (line 12).As per #3840 (comment) below, the call chain is not detected, which is expected. However, moving the argument to
allOf
into a local variable produces a false positive, as shown at point 2 (line 19).Code Sample demonstrating the issue:
Expected outcome:
PMD should report no violation at point 2 (line 19), but does. This is a false-positive.
Running PMD through: [CLI]
The text was updated successfully, but these errors were encountered: