{"record":{"id":"e36084cf81c010b3","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-e36084","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/ConvertUtils.java","lineNumber":48,"sourceCode":"\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/13\n *     desc  : utils about convert\n * </pre>\n */\npublic final class ConvertUtils {\n\n    private static final int    BUFFER_SIZE      = 8192;\n    private static final char[] HEX_DIGITS_UPPER =\n            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};\n    private static final char[] HEX_DIGITS_LOWER =\n            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};\n\n    private ConvertUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Int to hex string.\n     *\n     * @param num The int number.\n     * @return the hex string\n     */\n    public static String int2HexString(int num) {\n        return Integer.toHexString(num);\n    }\n\n    /**\n     * Hex string to int.\n     *\n     * @param hexString The hex string.\n     * @return the int\n     */","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ConvertUtils.java#L30-L66","documentation":"ConvertUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). Every conversion routine (hex, base64, byte/string, memory size, etc.) is static; the constructor exists only to enforce the static-only usage contract shared by all *Utils classes in the library.","triggerScenarios":"Reflection-based construction via getDeclaredConstructor().newInstance() (with setAccessible), or handing the Class object to a reflective factory. A plain `new ConvertUtils()` is a compile error, so this is purely a runtime reflection artifact.","commonSituations":"Coverage/mutation-testing tools that instantiate every loaded class; DI frameworks that auto-discover and new-up classes; a developer who assumes the conversion helpers are instance methods; generic test utilities that reflect over a package.","solutions":["Use the static API directly: ConvertUtils.bytes2HexString(...), ConvertUtils.byte2FitMemorySize(...), etc.","Exclude final utility classes from any tooling that reflectively instantiates classes.","If writing a no-instance-contract unit test, assert UnsupportedOperationException is thrown rather than consuming the instance."],"exampleFix":"// before (reflection)\nConvertUtils cu = ConvertUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nString hex = ConvertUtils.bytes2HexString(new byte[]{1, 2});","handlingStrategy":"validation","validationCode":"// Never construct ConvertUtils; it is static-only.\nif (clazz == ConvertUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use static API\n}","typeGuard":"if (clazz.getName().endsWith(\"Utils\") && Modifier.isFinal(clazz.getModifiers())) {\n    // static-only; do not newInstance()\n}","tryCatchPattern":"try {\n    Constructor<?> c = ConvertUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; call ConvertUtils.xxx() instead\n}","preventionTips":["Every *Utils class here is static-only — do not instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tooling that instantiates everything.","Static API only: ConvertUtils.bytes2HexString / byte2FitMemorySize / etc."],"tags":["utility-class","instantiation","design-pattern","reflection"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}