{"record":{"id":"d4f0d266fc6585c4","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-d4f0d2","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/JsonUtils.java","lineNumber":26,"sourceCode":" * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2019/01/07\n *     desc  : utils about json\n * </pre>\n */\npublic final class JsonUtils {\n\n    private static final byte TYPE_BOOLEAN     = 0x00;\n    private static final byte TYPE_INT         = 0x01;\n    private static final byte TYPE_LONG        = 0x02;\n    private static final byte TYPE_DOUBLE      = 0x03;\n    private static final byte TYPE_STRING      = 0x04;\n    private static final byte TYPE_JSON_OBJECT = 0x05;\n    private static final byte TYPE_JSON_ARRAY  = 0x06;\n\n    private JsonUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    \n    /**\n     * Checks if a given input is a JSONObject.\n     *\n     * @param input Anything.\n     * @return true if it is a JSONObject.\n     */\n    public static <T> boolean isJSONObject(final T input) {\n        return input instanceof JSONObject;\n    }\n\n    /**\n     * Checks if a given input is a JSONArray\n     *\n     * @param input Anything.\n     * @return true if it is a JSONArray.","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/JsonUtils.java#L8-L44","documentation":"JsonUtils is a final utility class providing static methods for JSON inspection (isJSONObject, isJSONArray). Its private constructor throws UnsupportedOperationException to enforce that the class is never instantiated — all functionality is accessed via static calls. This is a standard Java utility-class idiom.","triggerScenarios":"Instantiating via reflection (Constructor.newInstance or Class.getDeclaredConstructor().newInstance()), or via frameworks that auto-instantiate classes — Gson/Jackson deserialization targeting JsonUtils, Mockito mock creation, Dagger/Guice DI scanning, or ProGuard/R8 keep rules that assume an instantiable class.","commonSituations":"A dependency injection graph or serialization framework scans a package and tries to construct every class; a unit test uses Mockito.mock() on the wrong target; a ProGuard keep rule references the class and a post-obfuscation reflection call attempts construction.","solutions":["Use only static methods: JsonUtils.isJSONObject(obj), JsonUtils.isJSONArray(obj). Never call new JsonUtils().","If a DI/serialization framework triggers this, exclude JsonUtils from component scanning or add it to the framework's ignore list.","If reflection-based testing causes it, mock individual static method calls with PowerMock/mockito-inline rather than instantiating the class.","If using ProGuard/R8, ensure no keep rules or serialization configs target utility classes for instantiation."],"exampleFix":"// before\nJsonUtils utils = new JsonUtils();\nboolean isObj = utils.isJSONObject(jsonStr);\n\n// after\nboolean isObj = JsonUtils.isJSONObject(jsonStr);","handlingStrategy":"type-guard","validationCode":"// Utility classes are never instantiated — guard at the call site\nif (clazz != JsonUtils.class) {\n    Object instance = clazz.getDeclaredConstructor().newInstance();\n}","typeGuard":"// Verify the target is not a utility class before reflective instantiation\nstatic boolean isInstantiable(Class<?> clazz) {\n    try {\n        java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor();\n        c.setAccessible(true);\n        // Can't truly guard without instantiating; instead check by convention\n        return !clazz.getName().endsWith(\"Utils\");\n    } catch (NoSuchMethodException e) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    Constructor<JsonUtils> c = JsonUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    JsonUtils instance = c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // This is a utility class — use static methods instead\n    Log.w(TAG, \"JsonUtils is a utility class, use static methods\");\n}","preventionTips":["Never call new on any class ending in 'Utils' in this library — all are static-only.","Exclude com.blankj.utilcode.util.* from DI component scanning and serialization frameworks.","Add a project-wide Checkstyle/PMD rule to flag instantiation of utility classes.","Document utility-class usage in your codebase onboarding guide."],"tags":["java","android","utility-class","instantiation","reflection"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}