Skip to content

[java] LawOfDemeter: False positive with fields assigned to local vars #2188

@linusjf

Description

@linusjf

#2160 (comment)
Affects PMD Version:
6.20.0.

Rule:
LawOfDemeter.

Description:
Assigning an instance or local variables (created locally) should not be flagged as an object created elsewhere by this rule.

Code Sample demonstrating the issue:

package pmdtests;

public class AddressUsingCopyOnWrite {
  private volatile AddressValue addressValue;

 public AddressUsingCopyOnWrite(String       street, String city, String phone) {
   this.addressValue = new                    AddressValue(street, city, phone);
   }
  

 // thread-safe method
 @Override
  public String toString() {
    AddressValue local = addressValue;
    return "street=" + local.getStreet() + ",city=" + local.getCity()
        + ",phoneNumber=" + local.getPhoneNumber();
  }

 
}

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