It appears you're running Gson on a JVM with a SecurityManager
enabled. You'll need to disable the security manager to use Gson.
Gson SecurityException: Can not make Class Constructor accessible
-
27-11-2021 - |
Question
I am using Gson to serialize/deserialize a class that contains a static nested class. The class looks like below.
public class ClassA {
private NestedClass nestedClass;
public NestedClass getNestedClass() {
return nestedClass;
}
public void setNestedClass(NestedClass nestedClass) {
this.nestedClass = nestedClass;
}
public static class NestedClass implements Serializable {
public NestedClass() {
}
}
}
The serialization works fine, but when i am trying to deserialize the json string back into an object i get the following SecurityException.
java.lang.SecurityException: Can not make a java.lang.Class constructor accessible
at java.lang.reflect.AccessibleObject.setAccessible0(Unknown Source)
at java.lang.reflect.AccessibleObject.setAccessible(Unknown Source)
at com.google.gson.MappedObjectConstructor.getNoArgsConstructor(MappedObjectConstructor.java:86)
at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:63)
at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:54)
at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:42)
at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:55)
The exception seems to be coming from Java while trying to reflect the inner class. Has anyone been in that situation before?
Any help appreciated.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow