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 with lambda expression #1014

Closed
novsirion opened this issue Apr 1, 2018 · 1 comment · Fixed by #3510
Closed

[java] LawOfDemeter: False positive with lambda expression #1014

novsirion opened this issue Apr 1, 2018 · 1 comment · Fixed by #3510
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@novsirion
Copy link

Affects PMD Version: 6.1.0

Rule: LawOfDemeter

Description: Since java 1.8 a lambda expression like this System.out::println can be used.
A "Potential violation of Law of Demeter (method chain calls)" is reported when a lambda expression is used as a parameter for a method.

Result files:

<?xml version="1.0" encoding="UTF-8"?>
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd"
    version="6.1.0" timestamp="2018-04-01T15:08:32.567">
<file name="F:\Project\Java\demeter\src\main\java\SimpleClass.java">
<violation beginline="5" endline="5" begincolumn="9" endcolumn="52" rule="LawOfDemeter" ruleset="Design" class="SimpleClass" method="simpleMethod" externalInfoUrl="https://pmd.github.io/pmd-6.1.0/pmd_rules_java_design.html#lawofdemeter" priority="3">
Potential violation of Law of Demeter (method chain calls)
</violation>
</file>
</pmd>

Code Sample demonstrating the issue:

import java.util.function.Consumer;

public class SimpleClass{
    public void simpleMethod() {
        anotherSimpleMethod(System.out::println, 10);
    }

    public <T> void anotherSimpleMethod(Consumer<T> consumer, T value) {
        consumer.accept(value);
    }
}

Running PMD through: Gradle

  • build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'pmd'

repositories {
    mavenLocal()
    mavenCentral()
}

pmd{
    toolVersion = "6.1.0"
    ruleSetFiles = files("config/pmd/defaultRuleSet.xml")
    ruleSets = []
}
  • defaultRuleSet.xml
<ruleset 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Android Application Rules" 
    xmlns="http://pmd.sf.net/ruleset/1.0.0" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0                         http://pmd.sf.net/ruleset_xml_schema.xsd">
    <description>Standard rule set</description>
    <rule ref="category/java/design.xml/LawOfDemeter"/>
</ruleset>
@jsotuyod jsotuyod added the a:false-positive PMD flags a piece of code that is not problematic label Apr 2, 2018
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 changed the title [java] LawOfDemeter - False positive with lambda expression [java] LawOfDemeter: False positive with lambda expression Feb 25, 2022
@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