有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对象不是本地创建的

我的方法中包含以下代码片段:

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);     
}

在SonarQube中,我在上遇到一个问题“对象不是本地创建的”

String jsonStr = jsonArray.toJSONString();

我试图理解为什么我会得到这个。有什么帮助吗


共 (1) 个答案

  1. # 1 楼答案

    为什么违反

    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.

    您的jsonArray对象满足上述条件

    如何修复

    请参阅列出的一个示例here,您的代码可以固定在类似的行上

    根据德米特定律,对象O的方法M只能调用以下类型的方法:

    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