fb-contrib icon indicating copy to clipboard operation
fb-contrib copied to clipboard

fb-contrib:PMB_POSSIBLE_MEMORY_BLOAT should not appear where there is no option to mutate the collection

Open mistriel opened this issue 6 months ago • 1 comments

my case has two issues related to this check,

  1. The docs appears to refer to collection or StringBuffer but not to a Map
  2. The warning should not appear where there is no option to bloat.

In the following scenario if putUnique was public there were an option to bloat the Map, yet since putUnique is private and roleMapping is private then roleMapping field is immutable.


public class Example{

    private static final Map<String, String> roleMapping = new HashMap<>();

    static {
        putUnique("Jhon", "User");
        putUnique("Kate", "Manager");
        putUnique("Gorge", "Supervisor");
       
    }

    public String getRole(String first) {
     
        return roleMapping.get(first);
    }

    
    private static void putUnique(String first, String last ) {
        String previous = roleMapping.put(first, last);
        if (previous != null) {
            throw new IllegalArgumentException("Duplicate key");
        }
    }
}

mistriel avatar Aug 14 '24 14:08 mistriel