-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Unsatisfactory handling of type variables #1741
Comments
I think Gson should only resolve type variables for raw types. For every other case I can think of the user is expecting behavior which Gson does not provide and an exception should be thrown (though this is currently not the case). Please let me know if you can think of any other legit use cases for resolving type variables. Your And Gson could not even support all type variable usages for raw types:
Note that I am not a member of this project. |
GSON Version: 2.8.6
When
$Gson$Types.getRawType(Type)
is passed aTypeVariable
instance, it simply gives up and returnsObject.class
:That's not very good, and giving up just because there may be several bounds seems unnecessary. Since the behavior causes issues with my custom type adapter, I've had to resort to the following hack. (The
TypedField
class is a component of my custom type adapter. If you want to see the full code, here's the whole shebang.)As you see, the way I handle type variables is to traverse their upper bounds and return the first type that's an actual class, i.e. subtype of
Object
. Incidentally, type variables can only have one upper bound that's an actual class, so there can be no confusion here. Only if no upper bound is an actual class, then the fallback is once againObject.class
. (Even better might be to return the one upper bound if there is only one, and only fall back toObject.class
in case of ambiguity, but I personally don't need that.)I think GSON might want to adopt this behavior, but I first wanted some feedback before I make a patch. I'm also not sure whether I understand the relevant parts of GSON's internals correctly.
Another issue I encountered today because of GSON's current behavior, which I'm not able to fix with my custom type adapter, is the following:
Let's say I have a class
PencilBox<PencilType extends Pencil>
with a fieldList<PencilType> myPencils
:And let's say I have a custom type adapter for the class hierarchy starting from
Pencil
.When
new PencilBox<ColoringPencil>(reader)
is called, I expect that GSON will use my custom type adapter which I've registered for thePencil
type hierarchy. But no such thing happens, because GSON gives up on trying to figure out the upper bound of thePencilType
type variable. I get a list ofLinkedTreeMap
instances which then causes aClassCastException
as I'm trying to stuff them in aList
that's meant to hold objects of typePencil
.I guess the current solution here would be to use
TypeToken<List<Pencil>>
. I'm not sure if that might cause any headaches during refactoring, but it certainly breaks intuition. GSON should realize thatList<PencilType>
is effectivelyList<Pencil>
in that situation.The text was updated successfully, but these errors were encountered: