{"record":{"id":"dd8dc14fe4c5329a","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-dd8dc1","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"lib/utildebug/src/main/java/com/blankj/utildebug/DebugUtils.java","lineNumber":31,"sourceCode":"import com.blankj.utildebug.menu.DebugMenu;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\nimport androidx.annotation.DrawableRes;\n\n/**\n * <pre>\n *     author: blankj\n *     blog  : http://blankj.com\n *     time  : 2019/08/28\n *     desc  : utils about debug\n * </pre>\n */\npublic class DebugUtils {\n\n    private DebugUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    static {\n        List<IDebug> debugList = new ArrayList<>();\n        AbsToolDebug.addToolDebugs(debugList);\n        DebugMenu.getInstance().setDebugs(debugList);\n        getApp().registerActivityLifecycleCallbacks(ActivityLifecycleImpl.instance);\n    }\n\n    public static void setIconId(final @DrawableRes int icon) {\n        DebugIcon.getInstance().setIconId(icon);\n    }\n\n    public static void addDebugs(final List<IDebug> debugs) {\n        DebugMenu.getInstance().addDebugs(debugs);\n    }\n\n    public static Application getApp() {","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utildebug/src/main/java/com/blankj/utildebug/DebugUtils.java#L13-L49","documentation":"DebugUtils in the full utildebug module bootstraps the debug menu: its static initializer registers tool debugs and an activity-lifecycle callback. Its private constructor throws UnsupportedOperationException to prevent instantiation; all public methods (setIconId, addDebugs) are static and rely on the class-level init having run.","triggerScenarios":"Reflective instantiation via DebugUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan. Instantiating also implies the static initializer (getApp()/DebugMenu setup) ran, which requires the library to be initialized.","commonSituations":"Serialization frameworks binding a field to DebugUtils; mock frameworks in debug tests; coverage probes; accidental `new DebugUtils()` after weakening modifiers; using DebugUtils before Utils.init() completed so the static initializer's getApp() fails.","solutions":["Call the static API: DebugUtils.setIconId(res) / DebugUtils.addDebugs(list) instead of constructing.","Ensure Utils.init() has run before any DebugUtils touch so its static initializer succeeds.","Provide a Gson InstanceCreator / Jackson mixin returning a sentinel so the constructor is not invoked.","Use Mockito.mockStatic for mocking rather than constructing DebugUtils."],"exampleFix":"// before\nDebugUtils du = new DebugUtils();\n\n// after\nDebugUtils.setIconId(R.drawable.ic_debug);\nDebugUtils.addDebugs(debugItems);","handlingStrategy":"validation","validationCode":"// Ensure the library is initialized before touching DebugUtils (static initializer needs app).\nif (Utils.getApp() == null) {\n  throw new IllegalStateException(\"Call Utils.init() before DebugUtils.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call DebugUtils.setIconId(...)/addDebugds(...) statically.","Ensure Utils.init() completed so the static initializer's getApp() works.","Register InstanceCreator/mixin for JSON fields.","Use Mockito.mockStatic for mocking."],"tags":["android","utility-class","reflection","instantiation","debug"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}