0

I have in my method the snippet below:

JSONParser jsonParser = new JSONParser();
try {
    Object obj = jsonParser.parse(new InputStreamReader(is));
    JSONObject jsonObj = (JSONObject) obj;
    JSONArray jsonArray = (JSONArray) jsonObj.get("JSON_NODE");
    String jsonStr = jsonArray.toJSONString();
    return (JSONObject)jsonParser.parse(jsonStr);     
}

In SonarQube I am getting an issue "Object not created locally" on

String jsonStr = jsonArray.toJSONString();

I am trying to understand why I am getting this. Any help?

TT.
  • 15,774
  • 6
  • 47
  • 88
Sam
  • 1,333
  • 5
  • 23
  • 36
  • See this answer http://stackoverflow.com/a/26021695/1796579 for an explanation and why there is nothing wrong here. – Henry Jan 21 '16 at 09:35

1 Answers1

1

Why violation?

This violation relates to the fact that method is called on the object which is not created within the method and rather, retrieved as a return object as a result of method invocation on one of the local objects.

Your jsonArray Object satisfies above condition.

How to fix ,

See one sample listed here , your code can be fixed on similar lines.

According to Law of Demeter, a method M of object O should only call following types of methods :

1.Methods of Object O itself
2.Methods of Object passed as an argument
3.Method of object, which is held in instance variable
4.Any Object which is created locally in method M
Sabir Khan
  • 9,826
  • 7
  • 45
  • 98