Skip to main content

Resolve parameterized collection types

To resolve a parameterized collection type in java-classmate, you use the TypeResolver.resolve method by providing the raw collection class followed by its type parameters. This process produces a ResolvedType instance that preserves generic information, which can be verified using the getBriefDescription method.

The following example demonstrates how to resolve a List<String> and verify its structure.

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 typeResolver = new TypeResolver();

// Resolve List<String> by passing the raw class and the parameter class
ResolvedType resolvedList = typeResolver.resolve(List.class, String.class);

// Verify the erased type is java.util.List
if (resolvedList.getErasedType() != List.class) {
throw new AssertionError("Expected erased type to be List.class");
}

// Verify the brief description contains the full class names and parameterization
String description = resolvedList.getBriefDescription();
if (!"java.util.List<java.lang.String>".equals(description)) {
throw new AssertionError("Unexpected description: " + description);
}

// Verify the number of type parameters is exactly 1
if (resolvedList.getTypeParameters().size() != 1) {
throw new AssertionError("Expected 1 type parameter for List");
}

// Verify the type parameter itself is java.lang.String
ResolvedType paramType = resolvedList.getTypeParameters().get(0);
if (paramType.getErasedType() != String.class) {
throw new AssertionError("Expected type parameter to be String.class");
}
}
}

Type Resolution Behavior

When TypeResolver.resolve is called with a Class and additional Type arguments, java-classmate constructs a ResolvedType (specifically a ResolvedInterfaceType for List) that encapsulates the relationship between the base type and its parameters.

The getBriefDescription method provides a deterministic string representation of the resolved type. It includes the fully qualified name of the erased class and, if the type is parameterized, a comma-separated list of the brief descriptions of its type parameters enclosed in angle brackets. For List<String>, this results in the string java.util.List<java.lang.String>.