{"record":{"id":"b6248fd7759ed911","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-b6248f","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"info","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/CollectionUtils.java","lineNumber":31,"sourceCode":"import java.util.LinkedList;\nimport java.util.List;\nimport java.util.ListIterator;\nimport java.util.Map;\nimport java.util.Set;\nimport java.util.TreeSet;\n\n/**\n * <pre>\n *     author: blankj\n *     blog  : http://blankj.com\n *     time  : 2019/07/26\n *     desc  : utils about collection\n * </pre>\n */\npublic final class CollectionUtils {\n\n    private CollectionUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    ///////////////////////////////////////////////////////////////////////////\n    // listOf\n    ///////////////////////////////////////////////////////////////////////////\n\n    /**\n     * Returns a new read-only list of given elements.\n     *\n     * @param array The array.\n     * @return a new read-only list of given elements\n     */\n    @SafeVarargs\n    public static <E> List<E> newUnmodifiableList(E... array) {\n        return Collections.unmodifiableList(newArrayList(array));\n    }\n\n    /**","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/CollectionUtils.java#L13-L49","documentation":"CollectionUtils is a final utility class with static collection helpers; its private constructor throws UnsupportedOperationException('u can't instantiate me...') to forbid construction. The throw is an intentional guard reachable only via reflection, because the class is final and the constructor is private.","triggerScenarios":"Reflectively invoking the private constructor (setAccessible(true) + newInstance()); coverage or generic reflection tooling that instantiates every class; frameworks that auto-construct helper classes.","commonSituations":"Code-coverage tools (JaCoCo/Cobertura) covering private constructors; blanket reflection tests across a package; DI/serialization frameworks attempting default construction.","solutions":["Call static methods directly (e.g., CollectionUtils.get(col, i)) and never instantiate.","Exclude the constructor from coverage or rely on the coverage helper.","If testing the guard, assert UnsupportedOperationException rather than trying to succeed.","Remove reflective newInstance() calls against CollectionUtils.class."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Never instantiate; call static methods directly.\nObject v = CollectionUtils.get(col, i);","typeGuard":null,"tryCatchPattern":"try {\n    Constructor<CollectionUtils> c = CollectionUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException | InvocationTargetException e) {\n    // expected: instantiation is forbidden by design\n}","preventionTips":["Call CollectionUtils static methods directly; never reflectively instantiate it.","Exclude private constructors from coverage tooling that auto-instantiates.","For guard tests, assert UnsupportedOperationException instead of trying to succeed."],"tags":["utility-class","private-constructor","reflection","collectionutils","android","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}