Resolve parameterized collection types
To resolve a parameterized collection type like List<String> in java-classmate, you use the resolve method of the TypeResolver class. This method accepts a base class and an array of type parameters to produce a ResolvedType instance that preserves generic information. You can then verify the structure of the resolved type using the getBriefDescription method.
The following example demonstrates how to instantiate a TypeResolver, resolve a List with a String type parameter, and assert that the resulting description matches the expected fully-qualified generic signature.
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import java.util.List;
public final class ResolveParameterizedTypes {
public static void main(String[] args) {
TypeResolver typeResolver = new TypeResolver();
// Resolve List<String> by providing the base class and the parameter class
ResolvedType resolvedType = typeResolver.resolve(List.class, String.class);
// Retrieve the human-readable brief description of the resolved type
String briefDescription = resolvedType.getBriefDescription();
// Verify that the resolved type matches the expected generic signature
if (!briefDescription.equals("java.util.List<java.lang.String>")) {
throw new AssertionError("Expected java.util.List<java.lang.String> but got: " + briefDescription);
}
}
}
Type Resolution Behavior
When TypeResolver.resolve is called with List.class and String.class, java-classmate performs the following:
- It identifies
Listas the erased base type. - It binds the
Stringclass as the first type parameter for theListinterface. - It returns a
ResolvedTypeimplementation that encapsulates these bindings.
The ResolvedType.getBriefDescription method provides a deterministic string representation of this resolution, including the fully-qualified names of both the container and its type parameters.