Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
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.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ThrowableUtils.java:22
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2019/02/12
* desc : utils about exception
* </pre>
*/
public class ThrowableUtils {
private static final String LINE_SEP = System.getProperty("line.separator");
private ThrowableUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
public static String getFullStackTrace(Throwable throwable) {
final List<Throwable> throwableList = new ArrayList<>();
while (throwable != null && !throwableList.contains(throwable)) {
throwableList.add(throwable);
throwable = throwable.getCause();
}
final int size = throwableList.size();
final List<String> frames = new ArrayList<>();
List<String> nextTrace = getStackFrameList(throwableList.get(size - 1));
for (int i = size; --i >= 0; ) {
final List<String> trace = nextTrace;
if (i != 0) {
nextTrace = getStackFrameList(throwableList.get(i - 1));
removeCommonFrames(trace, nextTrace);
}
if (i == size - 1) {View on GitHub (pinned to 7b4caf9e54)
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.
Example fix
// before ThrowableUtils tu = new ThrowableUtils(); // after String trace = ThrowableUtils.getFullStackTrace(t);
Defensive patterns
Strategy: validation
Validate before calling
// ThrowableUtils is not final but constructor is guarded.
try {
ThrowableUtils.class.getDeclaredConstructor();
// present and private — treat as non-instantiable
} catch (NoSuchMethodException ignored) {} Prevention
- Use ThrowableUtils.getFullStackTrace(...) statically.
- Register InstanceCreator/mixin for JSON fields.
- Mock statically rather than constructing.
- Treat the private throwing constructor as 'do not instantiate'.
When it happens
Trigger: Reflective instantiation via ThrowableUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan.
Common situations: Serialization frameworks binding a field to ThrowableUtils; mocking in tests; coverage tooling; accidental `new ThrowableUtils()` after a fork weakened modifiers.
Related errors
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/31917d2604f87a1a.
Report an issue: GitHub.