Quick revision
The short answer
Inside the generic method, T is only a type variable, so the anonymous TypeToken captures the type List<T>, not the caller's List<Order>, and erasure means nothing at run time knows what T was. Gson cannot resolve T, so it falls back to LinkedTreeMap for each element, and newer Gson versions throw an error for this case instead (check your version). We fix it by passing the element class, parse(String json, Class<T> type), and building TypeToken.getParameterized(List.class, type).getType().
What to remember
In this answer, we saw that a TypeToken records exactly what is written in the source, so inside a generic method it captures List<T> with T as an unresolved type variable, and the caller's Order never reaches it. Gson then falls back to LinkedTreeMap, or, in newer versions, rejects the token. Passing a Class<T> and building the type with TypeToken.getParameterized, or accepting a TypeToken from the caller, fixes it, and Jackson's TypeReference and Spring's ParameterizedTypeReference have the same rule.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Inside the generic method, T is only a type variable, so the anonymous TypeToken captures the type List<T>, not the caller's List<Order>, and erasure means nothing at run time knows what T was. Gson cannot resolve T, so it falls back to LinkedTreeMap for each element, and newer Gson versions throw an error for this case instead (check your version). We fix it by passing the element class, parse(String json, Class<T> type), and building TypeToken.getParameterized(List.class, type).getType().
Why this question is asked
This bug usually appears right after someone has learned the TypeToken trick and tries to wrap it in a reusable helper. The code looks like it should work, because it is the same expression that worked a moment ago, now with T instead of Order. Understanding why it fails shows whether we really understand what a super type token captures. Interviewers ask it as an internals question for exactly that reason. In this answer, we will reproduce the failure, see exactly which type the token captures, follow what Gson does with an unresolved type variable, fix the method, and relate it to the same pattern in Jackson and Spring.
1Reproducing the Problem
Here is the helper and a call to it.
2What the TypeToken Really Captures
A TypeToken works because new TypeToken<...>(){} declares an anonymous subclass, and the class file records the generic signature of that subclass's superclass. TypeToken reads it with getGenericSuperclass(). The key point is that the signature is written once, when the compiler compiles the class, and it records exactly what was written in the source code.
In our helper, the source code says TypeToken<List<T>>. So the recorded type is List<T>, where T is a TypeVariable: a placeholder that names the method's type parameter, not a class. There is only one anonymous class for this expression, shared by every call, so it cannot possibly know that one caller wanted Order and another wanted Customer. The caller's type argument exists only for the compiler at the call site, and erasure removes it before the program runs.
| Where the token is written | Captured type | Gson's element type |
|---|---|---|
new TypeToken<List<Order>>(){} in the caller | List<Order> | Order |
new TypeToken<List<T>>(){} inside parse | List<T>, T a type variable | unknown: LinkedTreeMap or an error |
TypeToken.getParameterized(List.class, type) | List<Order> built at run time | Order |
So when we call parse with Order in mind, the type Gson receives is List<T> with T unresolved. The information was never there to begin with.
3What Gson Does With an Unresolved T
When Gson meets a type variable it cannot resolve, it can only treat it as its bound, which for an unbounded T is Object. For Object it uses its generic representation: JSON objects become LinkedTreeMap and numbers become Double. That is why the element prints as {id=7.0}. The cast to Order inserted by the compiler at the caller then fails.
Silently returning maps hid the bug until a later line, so newer Gson versions detect a captured type variable when the TypeToken is created and throw an IllegalArgumentException explaining the problem. That is a better failure, close to the real mistake. The exact version and message differ, so check yours.
4The Fix: Pass the Class and Build the Type
The method needs a real class from its caller, so we add a parameter for it and build the parameterized type at run time.
Notice that the Class<T> parameter both gives Gson the information at run time and lets the compiler infer T at the call site, so the call stays type-safe. If the element type is itself generic, for example List<Page<Order>>, a Class is not enough; the method should accept a Type or a TypeToken<T> from the caller instead, and the caller writes new TypeToken<Page<Order>>(){} where the real type is known, as the last method shows.
5The Same Pattern in Jackson and Spring
This is the general super type token pattern, and it has the same limit everywhere. Jackson's TypeReference, used as new TypeReference<List<Order>>(){}, captures its type argument the same way, and inside a generic method it captures List<T> just as uselessly. The Jackson fix is to build the type from its TypeFactory, for example mapper.getTypeFactory().constructCollectionType(List.class, type). Spring's ParameterizedTypeReference, used with RestClient, RestTemplate and WebClient, also works only where the concrete type is written; in generic code we can build a ResolvableType with ResolvableType.forClassWithGenerics(List.class, type). The rule to remember is that a super type token captures what is written in the source code at that spot, so it must be written where the concrete type is known.
6Follow-up questions
Why does the compiler not stop us from writing new TypeToken<List<T>>(){}?
Because it is legal Java: a type variable is a valid type argument. The problem is a run-time one, since a type variable cannot be turned into a class, and only the library can detect it, which is what newer Gson versions do.
Numbers came back as 7.0. Is that related?
Yes, indirectly. Because Gson treated the elements as untyped objects, it used its default number strategy for Object, which is Double. Setting ToNumberPolicy.LONG_OR_DOUBLE would give 7 as a Long, but the real fix is still to pass the proper type.
Could reflection on the method recover T at run time?
No. The method's generic signature only says that it has a type parameter T; the type chosen at each call site is erased. Only things declared with concrete types, such as fields, superclasses and method signatures with fixed types, keep their generic information.
7Conclusion
In this answer, we saw that a TypeToken records exactly what is written in the source, so inside a generic method it captures List<T> with T as an unresolved type variable, and the caller's Order never reaches it. Gson then falls back to LinkedTreeMap, or, in newer versions, rejects the token. Passing a Class<T> and building the type with TypeToken.getParameterized, or accepting a TypeToken from the caller, fixes it, and Jackson's TypeReference and Spring's ParameterizedTypeReference have the same rule.
Open with
A TypeToken works because the anonymous subclass records its superclass's generic signature in the class file, and that signature is exactly what is written in the source. Inside this method, the source says TypeToken<List<T>>, so what gets recorded is List<T> where T is a type variable, not Order.
Then, if they want more
There is just one anonymous class shared by all callers, and the caller's type argument is erased, so nothing at run time knows what T was. Gson cannot resolve T, treats it as Object, and builds LinkedTreeMap elements with Double numbers, so the caller gets a ClassCastException later.
Newer Gson versions detect a captured type variable and throw an IllegalArgumentException when the token is created, which is a clearer failure. The fix is to pass the element class: parse(String json, Class<T> type), and build the type at run time with TypeToken.getParameterized(List.class, type).getType().
If the element is itself generic, I would accept a Type or TypeToken from the caller, who writes the concrete token. It is the general super type token pattern, and Jackson's TypeReference and Spring's ParameterizedTypeReference have the same limit, with TypeFactory or ResolvableType as the run-time equivalents.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A generic method": https://scrivio-interview-library.netlify.app/java/json-libraries/73fb138e68/