google/gson · error · UnsupportedOperationException
Cannot allocate {c}. Usage of JDK sun.misc.Unsafe is enabled
Error message
Cannot allocate {c}. Usage of JDK sun.misc.Unsafe is enabled, but it could not be used. Make sure your runtime is configured correctly. What it means
Thrown by the final fallback UnsafeAllocator when none of the allocation strategies (sun.misc.Unsafe.allocateInstance, Dalvik ObjectStreamClass.getConstructorId/newInstance, or pre-Gingerbread ObjectInputStream.newInstance) could be loaded. Gson uses these to instantiate classes that have no no-arg constructor without invoking any constructor; when all strategies fail the allocator becomes a stub that throws UnsupportedOperationException for every allocation. The {c} is the Class Gson tried to allocate.
Source
Thrown at gson/src/main/java/com/google/gson/internal/UnsafeAllocator.java:121
ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
newInstance.setAccessible(true);
return new UnsafeAllocator() {
@Override
@SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> c) throws Exception {
assertInstantiable(c);
return (T) newInstance.invoke(null, c, Object.class);
}
};
} catch (Exception ignored) {
// OK: try the next way
}
// give up
return new UnsafeAllocator() {
@Override
public <T> T newInstance(Class<T> c) {
throw new UnsupportedOperationException(
"Cannot allocate "
+ c
+ ". Usage of JDK sun.misc.Unsafe is enabled, but it could not be used."
+ " Make sure your runtime is configured correctly.");
}
};
}
}
View on GitHub (pinned to 8b8628c656)
Solutions
- Add the required --add-opens to the JVM launch (e.g. --add-opens java.base/java.util=ALL-UNNAMED) so sun.misc.Unsafe.allocateInstance is reachable.
- Give the target class a no-arg constructor (can be private) so Gson's ConstructorConstructor uses ordinary reflection instead of Unsafe.
- Register an InstanceCreator for the type so Gson never reaches the Unsafe fallback.
- If you intentionally forbid Unsafe, call GsonBuilder.disableJdkUnsafe() so Gson fails fast with a clear message about a missing InstanceCreator instead of hitting this stub.
Example fix
// before: no default ctor + Unsafe blocked by JPMS
class Money { final long cents; Money(long c){cents=c;} }
// -> UnsupportedOperationException: Cannot allocate Money ...
// after: add a no-arg ctor Gson can reflect
class Money {
long cents;
private Money() {}
Money(long c){cents=c;}
} Defensive patterns
Strategy: validation
Validate before calling
// Verify Unsafe reachability at startup if you rely on unsafe allocation
try {
Class<?> u = Class.forName("sun.misc.Unsafe");
Field f = u.getDeclaredField("theUnsafe'); f.setAccessible(true);
Object unsafe = f.get(null);
u.getMethod("allocateInstance", Class.class).invoke(unsafe, Object.class);
} catch (Throwable t) {
// Unsafe unavailable — every class Gson deserializes must have a no-arg ctor or InstanceCreator
} Try / catch
try {
return gson.fromJson(json, Money.class);
} catch (UnsupportedOperationException e) {
if (e.getMessage() != null && e.getMessage().startsWith("Cannot allocate ")) {
throw new ConfigurationException("No no-arg constructor or InstanceCreator for " + type
+ " and sun.misc.Unsafe is blocked; add --add-opens or register an InstanceCreator", e);
}
throw e;
} Prevention
- Add --add-opens java.base/<pkg>=ALL-UNNAMED for JDK built-in collection types.
- Give deserialized classes a no-arg constructor (private is fine).
- Call GsonBuilder.disableJdkUnsafe() during development to surface missing ctors/InstanceCreators early.
- Register InstanceCreator for types you cannot modify.
When it happens
Trigger: Deserializing a class with no accessible no-arg constructor on a JVM where sun.misc.Unsafe is inaccessible (removed or blocked by JPMS, or a non-HotSpot JVM). Used by ConstructorConstructor when reflection-based construction falls back to unsafe allocation (which is the default unless GsonBuilder.disableJdkUnsafe() is set).
Common situations: Running on JDK 17+ without --add-opens java.base/java.util=ALL-UNNAMED (or the relevant package); GraalVM Native Image; J9 / Avian / embedded JVMs; Android ART historically. Common when deserializing java.util collections or third-party value types without no-arg ctors.
Related errors
- Unable to create instance of {rawType}. Registering an Insta
- Failed invoking canAccess
- Invalid EnumSet type: {type}
- Invalid EnumMap type: {type}
- Failed to invoke constructor '{constructor}' with no args
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/529dbce1d450fc94.json.
Report an issue: GitHub.