Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
linusjf opened this issue Dec 20, 2019 · 1 comment · Fixed by #3510
Closed

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

linusjf opened this issue Dec 20, 2019 · 1 comment · Fixed by #3510
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@linusjf
Copy link

linusjf commented Dec 20, 2019

#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]

@linusjf linusjf changed the title [Java] False positive for chained methods in rule LawOfDemeter. [Java] False positive for chained methods in rule LawOfDemeter with generic method call. Dec 20, 2019
@adangel adangel changed the title [Java] False positive for chained methods in rule LawOfDemeter with generic method call. [Java] LawOfDemeter: False positive for chained methods with generic method call Dec 20, 2019
@adangel adangel changed the title [Java] LawOfDemeter: False positive for chained methods with generic method call [java] LawOfDemeter: False positive for chained methods with generic method call Dec 20, 2019
@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Dec 20, 2019
oowekyala added a commit to oowekyala/pmd that referenced this issue Feb 16, 2022
- Fixes pmd#2175
- Fixes pmd#2179
- Fixes pmd#1605: same fix as pmd#2179, since enum constants are static fields they are trusted.
- Fixes pmd#2180: the fix is not to special-case Thread, but to consider all static methods as trusted (consistent with the treatment of static fields in pmd#2179)
- Fixes pmd#2182: the fix is not to allow package-private access, but to allow a class to access fields of instances of the same class.
- Fixes pmd#1014
- Fixes pmd#2188
@adangel adangel added this to the 7.0.0 milestone Feb 25, 2022
@adangel adangel linked a pull request Feb 25, 2022 that will close this issue
5 tasks
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@jsotuyod
Copy link
Member

This is fixed in PMD 7.0.0-rc1. It won't be backported to PMD 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants