Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

If generics are erased, how do frameworks like Jackson, Spring and Hibernate learn that a field is List<Order>? Explain the reflection APIs and their limits.

Erasure removes type arguments from objects and from the running code, but every class file still records the declared generic signatures of its fields, methods and superclass in Signature attributes. Frameworks read them through reflection: Field.getGenericType, Method.getGenericReturnType and Class.getGenericSuperclass return a ParameterizedType whose getActualTypeArguments gives Order. When there is no declaration to read, they use a super type token, an anonymous subclass such as new TypeReference<List<Order>>() {}, and Spring wraps all of this in ResolvableType and ParameterizedTypeReference.

Why this question is asked

If generics are erased, it seems impossible for Jackson to turn a JSON array into a List<Order> rather than a list of maps, or for Spring to inject a Repository<Order> instead of any repository. Yet both work every day. Interviewers ask this question to see whether we understand exactly what erasure removes and what it keeps, and whether we know the limits, because those limits explain real bugs such as a generic method that deserialises into LinkedHashMap. In this answer, we will see what the class file keeps, read it with the reflection API, learn the super type token trick, look at what Spring adds, and then go through the cases no API can solve.

1What Erasure Removes and What It Keeps

Erasure, the way javac removes type arguments after checking them, works on the code that runs. Local variables, method calls and individual objects lose their type arguments: an ArrayList created as new ArrayList<Order>() is just an ArrayList at run time and has no field that says Order.

Declarations are different. When javac compiles a field such as private List<Order> orders, it writes the erased type List into the field descriptor that the JVM uses for linking, and it also writes the full generic type into a Signature attribute, a small piece of metadata in the class file. The JVM ignores this attribute while running, but reflection can read it. The same is done for method parameters and return types, for the superclass and interfaces of a class, and for type parameter bounds.

Java
class Customer {
private List<Order> orders;
}
// javap -v Customer.class shows, for the field:
// descriptor: Ljava/util/List;
// Signature: Ljava/util/List<Lcom/shop/Order;>;
What survives erasure
Where the type appearsKept at run time?How to read it
Field declaration List<Order> ordersYes, Signature attributeField.getGenericType()
Method return or parameter typeYes, Signature attributeMethod.getGenericReturnType()
class OrderList extends ArrayList<Order>Yes, superclass signatureClass.getGenericSuperclass()
Local variable List<Order> xNo (only debug tables)Not available
An ArrayList object made with <Order>NoNot available

So the generic type of a declared member survives compilation. What does not survive is the type argument of an object created at run time.

2Reading Generic Types With Reflection

The reflection API has two kinds of methods. The older ones, such as Field.getType(), return a Class and give the erased type. The generic ones, added in Java 5, return a java.lang.reflect.Type, which may be a ParameterizedType holding the type arguments.

Java
Field f = Customer.class.getDeclaredField("orders");
System.out.println(f.getType()); // interface java.util.List
Type t = f.getGenericType();
System.out.println(t); // java.util.List<com.shop.Order>
ParameterizedType p = (ParameterizedType) t;
System.out.println(p.getRawType()); // interface java.util.List
System.out.println(p.getActualTypeArguments()[0]); // class com.shop.Order
Method m = OrderService.class.getMethod("findAll"); // List<Order> findAll()
System.out.println(m.getGenericReturnType()); // java.util.List<com.shop.Order>

This is exactly what Jackson does when it deserialises a Customer: it asks for the generic type of the orders field, sees List of Order, and builds Order objects for each array element. Hibernate does the same for a @OneToMany List<Order> to know the target entity. Notice that a type argument can itself be a ParameterizedType (List<Map<String, Order>>), a TypeVariable (T, if the field was declared as List<T>), or a WildcardType, so frameworks walk the structure recursively.

3The Super Type Token Trick

Sometimes there is no field or method to read. If we call objectMapper.readValue(json, List.class), Jackson only gets the raw class List, and the elements come back as LinkedHashMap. We cannot write List<Order>.class, because class literals are erased.

The trick is to create a declaration on the spot. An anonymous subclass, written new TypeReference<List<Order>>() {}, is a new class whose superclass is TypeReference<List<Order>>. The compiler records that superclass, with its type argument, in the new class's Signature attribute, and TypeReference's constructor reads it back with getClass().getGenericSuperclass().

Java
abstract class TypeRef<T> {
final Type type;
protected TypeRef() {
ParameterizedType sup = (ParameterizedType) getClass().getGenericSuperclass();
this.type = sup.getActualTypeArguments()[0];
}
}
Type t = new TypeRef<List<Order>>() {}.type;
System.out.println(t); // java.util.List<com.shop.Order>
List<Order> orders = mapper.readValue(json, new TypeReference<List<Order>>() {});
orders.get(0).getClass(); // class com.shop.Order
Step throughHow a super type token captures List<Order>
Our codejavacTypeReferenceJackson
new TypeReference<List<Order>>() {}
compile anonymous subclass, record superclass signature
constructor runs
getClass().getGenericSuperclass() -> List<Order>
readValue(json, typeRef)
List of real Order objects

6 messages in order. Play them, or step through them one at a time.

Notice the braces at the end: without them there is no subclass and nothing is recorded. This pattern is often called a super type token, because the type travels through the superclass of a throwaway class.

4What Spring Adds: ResolvableType and ParameterizedTypeReference

Working with raw Type objects is tedious, especially when type variables must be resolved through a hierarchy. If class OrderRepo implements Repository<Order, Long> and Repository declares T findById(ID id), the return type is just the variable T until we look up what OrderRepo bound it to. Spring's ResolvableType does this resolution for us, and the container uses it to match generic beans, so injecting Repository<Order, Long> picks OrderRepo and not CustomerRepo.

Java
ResolvableType t = ResolvableType.forClass(OrderRepo.class).as(Repository.class);
t.getGeneric(0).resolve(); // class com.shop.Order
t.getGeneric(1).resolve(); // class java.lang.Long
// ParameterizedTypeReference is Spring's super type token for HTTP clients
List<Order> list = restClient.get().uri("/orders")
.retrieve()
.body(new ParameterizedTypeReference<List<Order>>() {});

ParameterizedTypeReference works the same way as Jackson's TypeReference: an anonymous subclass records the type, and RestClient, WebClient and RestTemplate pass it to the message converters.

5The Limits No API Can Get Around

A runtime instance of ArrayList has no record of its element type. If a method receives Object o and o happens to be an ArrayList, nothing can tell us whether it was created as a list of orders or of strings; we can only inspect the elements, and an empty list tells us nothing at all.

Type variables are another limit. In a generic method <T> List<T> load(String json) { return mapper.readValue(json, new TypeReference<List<T>>() {}); }, the token records List<T>, not List<Order>, because T is what the source said. Jackson cannot resolve T and falls back to maps. The fix is to let the caller pass the type, as a Class<T>, a JavaType or a TypeReference, from the place where the real type is written. Local variable types, including those of lambdas, are also not reliably available, which is why some frameworks ask for method references or explicit types.

6Follow-up questions

Why does Jackson return LinkedHashMap elements sometimes?

Because it was given only a raw type, such as List.class, or a type it could not resolve, such as List<T> inside a generic method. Without an element type Jackson deserialises each JSON object into a Map. Passing a TypeReference or a JavaType built with TypeFactory.constructCollectionType fixes it.

Does getGenericType work on a lambda's parameters?

Not reliably. Lambdas are compiled into synthetic methods and hidden classes whose generic signatures are often not recorded the way an ordinary method's are, so frameworks that need type information from a lambda usually cannot get it. This is one reason APIs prefer an explicit Class or type token parameter.

Is reading Signature attributes slow?

Reflection is slower than a normal call, but frameworks do it once per class and cache the result. Spring caches ResolvableType lookups and Jackson caches its deserializers, so the cost is paid at start-up or on first use, not on every request.

7Conclusion

In this answer, we saw that erasure removes type arguments from objects and running code but leaves the generic signatures of fields, methods and superclasses in Signature attributes. Reflection reads them through Field.getGenericType, Method.getGenericReturnType and Class.getGenericSuperclass, which return ParameterizedType objects. When there is no declaration, a super type token, an anonymous subclass of TypeReference, records the type in its superclass, and Spring's ResolvableType and ParameterizedTypeReference wrap these APIs. A runtime instance like an ArrayList, or an unresolved type variable, still carries no type argument, so the caller has to supply it.

What to say in the interviewAbout 70 seconds aloud

Open with

Erasure removes type arguments from objects and executable code, but not from declarations. When javac compiles a field like List<Order> orders, it writes the erased type List for the JVM and also a Signature attribute with the full generic type.

Then, if they want more

Reflection exposes it: Field.getGenericType, Method.getGenericReturnType and Class.getGenericSuperclass return a ParameterizedType, and getActualTypeArguments gives me Order. That is how Jackson deserialises a field into real Order objects and how Hibernate knows the target of a collection mapping.

When there is no field to read, for example readValue on a JSON string, frameworks use a super type token: new TypeReference<List<Order>>() {} creates an anonymous subclass, its superclass signature records List<Order>, and the constructor reads it back with getGenericSuperclass.

Spring wraps this in ResolvableType, which also resolves type variables through the hierarchy so it can inject Repository<Order> correctly, and ParameterizedTypeReference for RestClient. The limits: an ArrayList instance has no record of its element type, and a token written inside a generic method only captures T, not the real type, so the caller must pass the type in.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. 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 "If generics are erased, how do frameworks like Jackson, Spring and Hibernate learn that...": https://scrivio-interview-library.netlify.app/java/generics/f80b9ae4ca/