Skip to content

[java] LawOfDemeter: False positive for chained methods with generic method call #2175

@linusjf

Description

@linusjf

#2160 (comment)

Affects PMD Version:
6.20.0

Rule:
LawOfDemeter

Description:
The rule double counts dot accessor for method that uses angled brackets to specify return type in generic method.

Code Sample demonstrating the issue:

package pmdtests;

import java.util.Arrays;
import java.util.List;

public enum Containment {
  ;

  public static void main(String... args) {
    Object obj = "one";
    List<Object> objs = Arrays.asList("one", 2, Math.PI, 4);
    List<Integer> ints = Arrays.asList(2, 4);
    assert objs.contains(obj);     // wrong violation: object not created locally
    assert objs.containsAll(ints); // wrong violation: object not created locally
    assert !ints.contains(obj);    // wrong violation: object not created locally
    assert !ints.containsAll(objs);// wrong violation: object not created locally

    obj = 1;
    objs = Arrays.<Object>asList(1, 3); // wrong violation: method chain calls
    ints = Arrays.asList(1, 2, 3, 4);
    assert ints.contains(obj);          // wrong violation: object not created locally
    assert ints.containsAll(objs);      // wrong violation: object not created locally
  }
}

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    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