{"record":{"id":"31917d2604f87a1a","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-31917d","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/ThrowableUtils.java","lineNumber":22,"sourceCode":"import java.io.StringWriter;\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.StringTokenizer;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2019/02/12\n *     desc  : utils about exception\n * </pre>\n */\npublic class ThrowableUtils {\n\n    private static final String LINE_SEP = System.getProperty(\"line.separator\");\n\n    private ThrowableUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    public static String getFullStackTrace(Throwable throwable) {\n        final List<Throwable> throwableList = new ArrayList<>();\n        while (throwable != null && !throwableList.contains(throwable)) {\n            throwableList.add(throwable);\n            throwable = throwable.getCause();\n        }\n        final int size = throwableList.size();\n        final List<String> frames = new ArrayList<>();\n        List<String> nextTrace = getStackFrameList(throwableList.get(size - 1));\n        for (int i = size; --i >= 0; ) {\n            final List<String> trace = nextTrace;\n            if (i != 0) {\n                nextTrace = getStackFrameList(throwableList.get(i - 1));\n                removeCommonFrames(trace, nextTrace);\n            }\n            if (i == size - 1) {","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ThrowableUtils.java#L4-L40","documentation":"ThrowableUtils is a utility class for rendering full stack traces (including cause chains). Its private constructor throws UnsupportedOperationException to block instantiation. Every public method (getFullStackTrace, etc.) is static, so the class is a namespace, not an instantiable type.","triggerScenarios":"Reflective instantiation via ThrowableUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan.","commonSituations":"Serialization frameworks binding a field to ThrowableUtils; mocking in tests; coverage tooling; accidental `new ThrowableUtils()` after a fork weakened modifiers.","solutions":["Call the static API: ThrowableUtils.getFullStackTrace(throwable) instead of constructing.","Register a Gson InstanceCreator / Jackson mixin returning a sentinel so the constructor is not invoked.","Use Mockito.mockStatic for mocking rather than instantiation.","Keep the class and constructor as-is (non-final here but constructor-protected) to preserve the guard."],"exampleFix":"// before\nThrowableUtils tu = new ThrowableUtils();\n\n// after\nString trace = ThrowableUtils.getFullStackTrace(t);","handlingStrategy":"validation","validationCode":"// ThrowableUtils is not final but constructor is guarded.\ntry {\n  ThrowableUtils.class.getDeclaredConstructor();\n  // present and private — treat as non-instantiable\n} catch (NoSuchMethodException ignored) {}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use ThrowableUtils.getFullStackTrace(...) statically.","Register InstanceCreator/mixin for JSON fields.","Mock statically rather than constructing.","Treat the private throwing constructor as 'do not instantiate'."],"tags":["android","utility-class","reflection","instantiation","exception"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}