{"record":{"id":"383c5832fa7a77bd","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-383c58","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/MapUtils.java","lineNumber":24,"sourceCode":"import java.util.Comparator;\nimport java.util.HashMap;\nimport java.util.Hashtable;\nimport java.util.LinkedHashMap;\nimport java.util.Map;\nimport java.util.TreeMap;\n\n/**\n * <pre>\n *     author: blankj\n *     blog  : http://blankj.com\n *     time  : 2019/08/12\n *     desc  : utils about map\n * </pre>\n */\npublic class MapUtils {\n\n    private MapUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Returns a new read-only map with the specified contents, given as a list of pairs\n     * where the first value is the key and the second is the value.\n     *\n     * @param pairs a list of pairs\n     * @return a new read-only map with the specified contents\n     */\n    @SafeVarargs\n    public static <K, V> Map<K, V> newUnmodifiableMap(final Pair<K, V>... pairs) {\n        return Collections.unmodifiableMap(newHashMap(pairs));\n    }\n\n    @SafeVarargs\n    public static <K, V> HashMap<K, V> newHashMap(final Pair<K, V>... pairs) {\n        HashMap<K, V> map = new HashMap<>();\n        if (pairs == null || pairs.length == 0) {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/MapUtils.java#L6-L42","documentation":"MapUtils is a utility class providing static factory methods for creating and populating maps (newHashMap, newUnmodifiableMap, newTreeMap, etc.) from pairs. Its private constructor throws UnsupportedOperationException to enforce static-only access.","triggerScenarios":"Direct or reflective instantiation by DI frameworks (Dagger, Guice, Spring), mocking libraries (Mockito, PowerMock), serialization frameworks (Gson, Jackson), or manual reflection.","commonSituations":"Component scanning in a DI framework traverses the utility package and attempts construction; a test suite instantiates all classes for coverage analysis; a serialization library targets the class.","solutions":["Use static factory methods: MapUtils.newHashMap(pairs), MapUtils.newUnmodifiableMap(pairs). Never instantiate.","Exclude utility classes from DI scanning and serialization configs.","Mock static methods with mockito-inline for testing."],"exampleFix":"// before\nMapUtils mu = new MapUtils();\nMap<String, Integer> map = mu.newHashMap(Pair.create(\"a\", 1));\n\n// after\nMap<String, Integer> map = MapUtils.newHashMap(Pair.create(\"a\", 1));","handlingStrategy":"type-guard","validationCode":"// MapUtils is static-only — use factory methods directly\nMap<String, Integer> map = MapUtils.newHashMap(\n    Pair.create(\"a\", 1), Pair.create(\"b\", 2));","typeGuard":"static boolean isUtilityClass(Class<?> c) {\n    return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith(\"Utils\");\n}","tryCatchPattern":"try {\n    MapUtils.class.getDeclaredConstructor().setAccessible(true);\n    MapUtils.class.getDeclaredConstructor().newInstance();\n} catch (UnsupportedOperationException e) {\n    // Use static factory methods instead\n}","preventionTips":["Never instantiate classes ending in 'Utils'.","Exclude util packages from DI and serialization scanning.","Use static factory methods: MapUtils.newHashMap(), newTreeMap(), etc."],"tags":["java","android","utility-class","instantiation","map"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}