Search code examples
javafindbugsspotbugs

FindBugs detects the SIC_INNER_SHOULD_BE_STATIC_ANON warning for the following code


FindBugs detects the SIC_INNER_SHOULD_BE_STATIC_ANON warning for the following code. Besides ignoring warnings in SuppressFBWarnings How can I avoid 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;

    }
}

Solution

  • As suggested by the message, you can create a named static inner 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();
        ...
    }
    

    BTW the FindBugs is not updated for years so it is better to replace it with its successor or other solutions such as PMD, Google Error prone, etc.