I made some massive changes to a json file in my project and now Gson is complaining with this error:
java.lang.RuntimeException: Failed to invoke protected java.util.AbstractCollection() with no args
The json source file can be found here.
My implementation for the corresponding object can be found here
I'm firing up Gson like this:
new Gson().fromJson(json.toString(), Config.class);
The source file that integrates Gson can be found here
I did some scrubbing around on SO and I found this article but the links were broken so I couldn't really get a grip on the problem. https://stackguides.com/questions/17351720/deserializing-a-custom-object-with-gson-error-failed-to-invoke-public-custom-cla
The only thing I think could be causing the issue is this block of code found in Config.java:91
private AbstractCollection<String> expectedMessages = null;
public Structure() {
if(this.messagesInOrder) {
this.expectedMessages = new ArrayList<>();
} else {
this.expectedMessages = new HashSet<>();
}
}
But I'm not sure why it would be causing the error since you can do this normally. Perhaps Gson doesn't construct a new instance of the inner class?
Here's the stack trace for reference
Exception in thread "main" java.lang.RuntimeException: Failed to invoke protected java.util.AbstractCollection() with no args
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:107)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:78)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at config.ParseConfig.convertJsonToObject(ParseConfig.java:70)
at config.ParseConfig.parseConfig(ParseConfig.java:36)
at JavaGrader.main(JavaGrader.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:104)
... 18 more
Process finished with exit code 1