Resolve parameterized collection types
java-classmate provides the TypeResolver class to handle the resolution of generic type information that is otherwise lost at runtime. By using the resolve method, you can programmatically construct a ResolvedType that represents a parameterized collection, such as List<String>.
The following example demonstrates how to instantiate a TypeResolver, resolve a parameterized List, and verify the resulting type using getBriefDescription.
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import java.util.List;
public final class ResolveParameterizedTypes {
public static void main(String[] args) {
// Initialize the resolver which manages type caching
TypeResolver typeResolver = new TypeResolver();
// Resolve List<String> by providing the raw class and its type parameter
ResolvedType resolvedType = typeResolver.resolve(List.class, String.class);
// Retrieve a human-readable string representation of the resolved type
String description = resolvedType.getBriefDescription();
// Verify that the type was resolved with the correct parameterization
if (!description.equals("java.util.List<java.lang.String>")) {
throw new AssertionError("Unexpected description: " + description);
}
}
}
Type Resolution with TypeResolver
The TypeResolver.resolve method accepts a base type and an optional array of type parameters. When you pass List.class as the first argument and String.class as the second, java-classmate constructs a ResolvedType instance that correctly binds java.lang.String to the type variable of java.util.List.
Verifying Resolved Types
The ResolvedType.getBriefDescription method returns a compact, human-readable string of the type. Unlike a full description, the brief version focuses on the current type and its parameters without including the entire supertype hierarchy. This is useful for deterministic verification of resolution results, as it produces a predictable string format like package.Container<package.Parameter>.