Skip to content

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

@novsirion

Description

@novsirion

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions