{"record":{"id":"54eab6284858f4d9","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-54eab6","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/GsonUtils.java","lineNumber":36,"sourceCode":"\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2018/04/05\n *     desc  : utils about gson\n * </pre>\n */\npublic final class GsonUtils {\n\n    private static final String KEY_DEFAULT   = \"defaultGson\";\n    private static final String KEY_DELEGATE  = \"delegateGson\";\n    private static final String KEY_LOG_UTILS = \"logUtilsGson\";\n\n    private static final Map<String, Gson> GSONS = new ConcurrentHashMap<>();\n\n    private GsonUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Set the delegate of {@link Gson}.\n     *\n     * @param delegate The delegate of {@link Gson}.\n     */\n    public static void setGsonDelegate(Gson delegate) {\n        if (delegate == null) return;\n        GSONS.put(KEY_DELEGATE, delegate);\n    }\n\n    /**\n     * Set the {@link Gson} with key.\n     *\n     * @param key  The key.\n     * @param gson The {@link Gson}.\n     */","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java#L18-L54","documentation":"GsonUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). It wraps Gson via static methods backed by a static ConcurrentHashMap of Gson instances (default, delegate, logUtils); all access is static, so no instance is needed and the constructor only blocks instantiation, per the library-wide *Utils convention.","triggerScenarios":"Reflective construction via getDeclaredConstructor().newInstance() with setAccessible, or a framework that new-ups classes by Class reference. A direct `new GsonUtils()` fails to compile because the constructor is private.","commonSituations":"Coverage tooling that instantiates all classes; DI containers scanning packages; a developer who assumes per-instance Gson configuration; reflective test harnesses that exercise all constructors.","solutions":["Use the static API directly: GsonUtils.setGsonDelegate(gson), GsonUtils.toJson(obj), GsonUtils.fromJson(json, type), etc.","Configure Gson globally via setGsonDelegate rather than per-instance.","Exclude final utility classes from any reflective instantiation framework."],"exampleFix":"// before (reflection)\nGsonUtils gu = GsonUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nGsonUtils.setGsonDelegate(new GsonBuilder().disableHtmlEscaping().create());\nString json = GsonUtils.toJson(obj);","handlingStrategy":"validation","validationCode":"// Never construct GsonUtils; it is static-only.\nif (clazz == GsonUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use GsonUtils.xxx() statically\n}","typeGuard":"if (clazz.getName().endsWith(\"Utils\") && Modifier.isFinal(clazz.getModifiers())) {\n    // static-only; do not newInstance()\n}","tryCatchPattern":"try {\n    Constructor<?> c = GsonUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use GsonUtils.xxx() instead\n}","preventionTips":["GsonUtils is static-only — configure Gson via setGsonDelegate, not via instances.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call GsonUtils.toJson()/fromJson() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","gson","json"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}