Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when abstract class cannot be constructed #1814

Conversation

Marcono1234
Copy link
Collaborator

Previously Gson tried to invoke the no-arg constructor of abstract classes. This lead to rather unhelpful exception messages, e.g.:

java.lang.RuntimeException: Failed to invoke public test.AbstractClass() with no args
    at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:119)
    ...

Now Gson will not try to invoke constructors of abstract classes anymore, which generates more helpful exception messages:

java.lang.RuntimeException: Unable to invoke no-args constructor for class test.AbstractClass. Registering an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:235)
    ...
Caused by: java.lang.UnsupportedOperationException: Abstract class can't be instantiated! Class name: test.AbstractClass
    at com.google.gson.internal.UnsafeAllocator.assertInstantiable(UnsafeAllocator.java:120)
    ...

@google-cla google-cla bot added the cla: yes label Nov 5, 2020
@Marcono1234 Marcono1234 force-pushed the marcono1234/abstract-class-constructor branch from d2ec571 to 2bc8b69 Compare November 5, 2020 14:54
@eamonnmcmanus
Copy link
Member

Thanks, I actually ran into a user issue with this earlier this week.

@eamonnmcmanus eamonnmcmanus merged commit 47dea2e into google:master Feb 4, 2022
@Marcono1234 Marcono1234 deleted the marcono1234/abstract-class-constructor branch February 4, 2022 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants