What I understood about law of demeter is :
Your method can call other methods in its class directly
Your method can call methods on its own fields directly (but not on the fields' fields)
When your method takes parameters, your method can call methods on those parameters directly.
When your method creates local objects, that method can call methods on the local objects.
but
One should not call methods on a global object (but it can be passed as a parameter ?)
One should not have a chain of messages a.getB().getC().doSomething() in some class other than a's class.
What I'm doing in one of my methods is this :
final ServiceStatusBean serviceStatusBean = new ServiceStatusBean();
serviceStatusBean.setName("someName");
serviceStatusBean.setApiVersion("someVersion");
My serviceStatusBean instance is created locally within the method and I'm calling setters on it. As per my understanding, Its fine with law of demeter. But according to PMD, its violating the law of demeter at the lines calling the setters.
Warning- "Potential violation of Law of Demeter (object not created locally)"
I'm not able to understand the reasoning behind these PMD warnings. Any explanations ??
PMD Details :
Plugin version- 4.0.5.v20141105-1906
PMD version- 5.2.1