Resolve parameterized collection types
To resolve a parameterized collection like List<String> in java-classmate, you use the TypeResolver.resolve method by passing the base collection class and the specific type parameter class. This process creates a ResolvedType instance that retains the generic information, which can be verified using ResolvedType.getBriefDescription.
import com.fasterxml.classmate.TypeResolver;
import com.fasterxml.classmate.ResolvedType;
import java.util.List;
public final class ResolveParameterizedTypes {
public static void main(String[] args) {
TypeResolver resolver = new TypeResolver();
// Resolve List<String> by providing the base class and the parameter class
ResolvedType resolvedList = resolver.resolve(List.class, String.class);
// Verify the resolved type using the brief description
String description = resolvedList.getBriefDescription();
if (!"java.util.List<java.lang.String>".equals(description)) {
throw new AssertionError("Expected java.util.List<java.lang.String> but got: " + description);
}
}
}
Type Resolution with TypeResolver
The TypeResolver.resolve method in java-classmate handles the mapping of type parameters to a base class. When you provide List.class and String.class, the resolver constructs a ResolvedType that represents the specific instantiation of the generic interface. This allows the library to track that the first type parameter of the List is String.
Verifying Results with ResolvedType
The ResolvedType.getBriefDescription method provides a human-readable string representation of the resolved type. Unlike full descriptions, the brief description focuses on the class name and its immediate type parameters, omitting details about the class hierarchy or implemented interfaces. This makes it a deterministic way to verify that the TypeResolver correctly applied the provided type parameters.