A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/3840 below:

[java] LawOfDemeter disallows method call on locally created object · Issue #3840 · pmd/pmd · GitHub

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:

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).

Code Sample demonstrating the issue:

import java.util.List;
import java.util.concurrent.CompletableFuture;

public class ParallelHandler<T> {

    private List<CompletableFuture<T>> futures;

    //...

    // pmd failed to warn
    public List<CompletableFuture<T>> get_nowarning() {
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[] {})).join(); // point 1: true-negative
        return futures;
    }
    
    // pmd warns CompletableFuture.allOf(tempVar).join();
    public List<CompletableFuture<T>> get_fp() {
        CompletableFuture[] tempVar =  futures.toArray(new CompletableFuture[] {});
        CompletableFuture.allOf(tempVar).join(); // point 2: false positive
        return futures;
    }
}

Expected outcome:

PMD should report no violation at point 2 (line 19), but does. This is a false-positive.

Running PMD through: [CLI]


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