Skip to content

[java] LawOfDemeter: False positive with static property access - should treat class-level property as global object, not dot-accessed property #2179

@linusjf

Description

@linusjf

#2160 (comment)
Affects PMD Version:
6.20.0
Rule:
LawOfDemeter.
Description:
Static property access warning must not be flagged for Law of Demeter when it can be mitigated by static import. The access of a static property is double-counted; the dot accessor count must start from the static property, not the class in which it exists. A very special case for this would be the static property at the class level 'class'. This should be treated as a global variable as well.

Code Sample demonstrating the issue:

package pmdtests;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.SECONDS;

public enum Test {
  ;

  public static void main(String[] args) {
    try {
     
        TimeUnit.MILLISECONDS.sleep(300);
SECONDS.sleep(2);
       
  
    } catch (InterruptedException ie) {
      System.err.println(ie);
    }
  }
}

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