Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
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.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/JsonUtils.java:26
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2019/01/07
* desc : utils about json
* </pre>
*/
public final class JsonUtils {
private static final byte TYPE_BOOLEAN = 0x00;
private static final byte TYPE_INT = 0x01;
private static final byte TYPE_LONG = 0x02;
private static final byte TYPE_DOUBLE = 0x03;
private static final byte TYPE_STRING = 0x04;
private static final byte TYPE_JSON_OBJECT = 0x05;
private static final byte TYPE_JSON_ARRAY = 0x06;
private JsonUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Checks if a given input is a JSONObject.
*
* @param input Anything.
* @return true if it is a JSONObject.
*/
public static <T> boolean isJSONObject(final T input) {
return input instanceof JSONObject;
}
/**
* Checks if a given input is a JSONArray
*
* @param input Anything.
* @return true if it is a JSONArray.View on GitHub (pinned to 7b4caf9e54)
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.
Example fix
// before JsonUtils utils = new JsonUtils(); boolean isObj = utils.isJSONObject(jsonStr); // after boolean isObj = JsonUtils.isJSONObject(jsonStr);
Defensive patterns
Strategy: type-guard
Validate before calling
// Utility classes are never instantiated — guard at the call site
if (clazz != JsonUtils.class) {
Object instance = clazz.getDeclaredConstructor().newInstance();
} Type guard
// Verify the target is not a utility class before reflective instantiation
static boolean isInstantiable(Class<?> clazz) {
try {
java.lang.reflect.Constructor<?> c = clazz.getDeclaredConstructor();
c.setAccessible(true);
// Can't truly guard without instantiating; instead check by convention
return !clazz.getName().endsWith("Utils");
} catch (NoSuchMethodException e) {
return false;
}
} Try / catch
try {
Constructor<JsonUtils> c = JsonUtils.class.getDeclaredConstructor();
c.setAccessible(true);
JsonUtils instance = c.newInstance();
} catch (UnsupportedOperationException e) {
// This is a utility class — use static methods instead
Log.w(TAG, "JsonUtils is a utility class, use static methods");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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/d4f0d266fc6585c4.
Report an issue: GitHub.