Blankj/AndroidUtilCode · info · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
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.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/GsonUtils.java:36
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/04/05
* desc : utils about gson
* </pre>
*/
public final class GsonUtils {
private static final String KEY_DEFAULT = "defaultGson";
private static final String KEY_DELEGATE = "delegateGson";
private static final String KEY_LOG_UTILS = "logUtilsGson";
private static final Map<String, Gson> GSONS = new ConcurrentHashMap<>();
private GsonUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Set the delegate of {@link Gson}.
*
* @param delegate The delegate of {@link Gson}.
*/
public static void setGsonDelegate(Gson delegate) {
if (delegate == null) return;
GSONS.put(KEY_DELEGATE, delegate);
}
/**
* Set the {@link Gson} with key.
*
* @param key The key.
* @param gson The {@link Gson}.
*/View on GitHub (pinned to 7b4caf9e54)
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.
Example fix
// before (reflection) GsonUtils gu = GsonUtils.class.getDeclaredConstructor().newInstance(); // after GsonUtils.setGsonDelegate(new GsonBuilder().disableHtmlEscaping().create()); String json = GsonUtils.toJson(obj);
Defensive patterns
Strategy: validation
Validate before calling
// Never construct GsonUtils; it is static-only.
if (clazz == GsonUtils.class || Modifier.isFinal(clazz.getModifiers())) {
// skip instantiation; use GsonUtils.xxx() statically
} Type guard
if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
// static-only; do not newInstance()
} Try / catch
try {
Constructor<?> c = GsonUtils.class.getDeclaredConstructor();
c.setAccessible(true);
c.newInstance();
} catch (UnsupportedOperationException e) {
// expected; use GsonUtils.xxx() instead
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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/54eab6284858f4d9.
Report an issue: GitHub.