Skip to main content

Inspect resolved member methods

To inspect the methods of a generic type like ArrayList<String>, java-classmate provides a multi-step resolution process that moves from raw classes to fully resolved member definitions. This allows you to access method signatures where generic type parameters have been replaced with their actual resolved types.

Resolving Type Members

The process begins by creating a ResolvedType using the TypeResolver. This object represents the specific generic instantiation. You then pass this type to a MemberResolver, which aggregates all members—including those inherited from parent classes and interfaces—into a ResolvedTypeWithMembers container.

When calling MemberResolver.resolve, you can provide optional configuration for annotations and overrides. Passing null for these parameters uses the default behavior, which focuses on the structural type information without additional metadata processing or member filtering.

Accessing Method Information

Once you have a ResolvedTypeWithMembers instance, you can call getMemberMethods to retrieve an array of ResolvedMethod objects. Each ResolvedMethod provides access to its metadata, such as its name via the getName method inherited from ResolvedMember. This allows for deterministic verification of the class interface after generic resolution.

import com.fasterxml.classmate.TypeResolver;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.MemberResolver;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.members.ResolvedMethod;
import java.util.ArrayList;

public final class InspectResolvedMembers {
public static void main(String[] args) {
TypeResolver typeResolver = new TypeResolver();

// Resolve the generic type ArrayList<String>
ResolvedType arrayListType = typeResolver.resolve(ArrayList.class, String.class);

// Initialize the member resolver to process the type hierarchy
MemberResolver memberResolver = new MemberResolver(typeResolver);

// Resolve all members for the type, using null for optional annotation and override configs
ResolvedTypeWithMembers typeWithMembers = memberResolver.resolve(arrayListType, null, null);

// Retrieve all member methods (instance methods)
ResolvedMethod[] methods = typeWithMembers.getMemberMethods();

boolean foundAddMethod = false;
for (ResolvedMethod method : methods) {
// Verify the method name using ResolvedMember.getName()
if ("add".equals(method.getName())) {
foundAddMethod = true;
break;
}
}

// Deterministic check to ensure the expected member was resolved
if (!foundAddMethod) {
throw new AssertionError("Expected method 'add' was not found in resolved ArrayList members");
}
}
}

The getMemberMethods call returns all instance methods available on the type, including those inherited from AbstractList, AbstractCollection, and List. By iterating through these ResolvedMethod instances, you can inspect the fully resolved signatures where the generic E from ArrayList<E> is correctly identified as String.