FindBugs 检测到以下代码的 SIC_INNER_SHOULD_BE_STATIC_ANON 警告

FindBugs detects the SIC_INNER_SHOULD_BE_STATIC_ANON warning for the following code

FindBugs 检测到以下代码的 SIC_INNER_SHOULD_BE_STATIC_ANON 警告。 除了忽略 SuppressFBWarnings 中的警告 我怎样才能避免SIC_INNER_SHOULD_BE_STATIC_ANON?

Class MyClass $ 1 may be able to be refactored into a named static inner class.
Local variable name reference

public class MyClass() {

    public Map<String, String> fromJSON(String json) throws Exception {
            TypeReference<HashMap<String, String>> reference = new TypeReference<HashMap<String, String>>() {};
            Map<String, String> map = null;
            ObjectMapper mapper = new ObjectMapper();
            map = mapper.readValue(json, reference);

        return map;

    }
}

根据消息的建议,您可以创建一个命名静态内部 class:

private static final class MyTypeReference extends TypeReference<HashMap<String, String>> {}

public Map<String, String> fromJSON(String json) throws Exception {
    TypeReference<HashMap<String, String>> reference = new MyTypeReference();
    ...
}

顺便说一句 FindBugs is not updated for years so it is better to replace it with its successor 或其他解决方案,例如 PMD、Google 容易出错等