Closed hfxu closed 3 weeks ago
It looks like this code is incorrect, but the older version of Gson didn't notice that. This commit from two years ago added validation that the number of type arguments given to TypeToken.getParameterized
is equal to the number of type parameters that the base type actually has. Here, Response
has only one type argument, <T>
, but the call to TypeToken.getParameterized
is giving it two, <List, Long>
. I think what you want is something like this:
Type listOfLong = TypeToken.getParameterized(List.class, Long.class).getType();
Type responseType = TypeToken.getParameterized(Response.class, listOfLong);
Or alternatively create an anonymous TypeToken
subclass:
var responseType = new TypeToken<Response<List<Long>>>() {};
This prevents such malformed type issues already at compile time.
You can then also use the T Gson.fromJson(..., TypeToken<T>)
overloads which were added in Gson 2.10. These are more type safe compared to the Gson.fromJson(..., Type)
overloads.
Hi @Marcono1234 @eamonnmcmanus Thank you for your suggestion! It appears that I had been used the method in incorrect way all along.
Gson version
2.11.0
Java / Android version
JDK17
Used tools
Maven
Description
When upgrade gson from 2.8.9 to 2.11.0, the below code not working with gson-2.11.0:
Type responseType = TypeToken.getParameterized(Response.class, List.class, Long.class).getType();
And it's thrown a exception:Expected behavior
Got a correct Type of
Response<List<Long>>
.Actual behavior
Throw a exception like
java.lang.IllegalArgumentException: GsonTest$Response requires 1 type arguments, but got 2
Reproduction steps
Exception stack trace