Skip to main content

Inspect resolved member methods

When you need to inspect the methods of a parameterized type like ArrayList<String>, java-classmate provides a mechanism to resolve these members while maintaining their generic context. This is particularly useful when you need to verify the presence of specific methods or examine their signatures after type erasure has been accounted for.

You can achieve this by using TypeResolver to define the specific type and MemberResolver to extract its members. The MemberResolver.resolve method returns a ResolvedTypeWithMembers object, which allows you to iterate through methods as ResolvedMethod instances. Each ResolvedMethod inherits from ResolvedMember, providing access to the method name via getName.

The following example demonstrates how to resolve ArrayList<String> and verify that the add method is present among its member methods.

import com.fasterxml.classmate.MemberResolver;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.ResolvedTypeWithMembers;
import com.fasterxml.classmate.TypeResolver;
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 specific generic type ArrayList<String>
ResolvedType arrayListType = typeResolver.resolve(ArrayList.class, String.class);

// Initialize MemberResolver to process the resolved type
MemberResolver memberResolver = new MemberResolver(typeResolver);

// Resolve members without additional annotation configuration or overrides
ResolvedTypeWithMembers members = memberResolver.resolve(arrayListType, null, null);

boolean foundAddMethod = false;

// Iterate through member methods and verify the name of each ResolvedMember
for (ResolvedMethod method : members.getMemberMethods()) {
if ("add".equals(method.getName())) {
foundAddMethod = true;
break;
}
}

// Deterministic verification that the expected method was resolved
if (!foundAddMethod) {
throw new AssertionError("Expected method 'add' not found in resolved ArrayList<String> members");
}
}
}

Internally, MemberResolver processes the type hierarchy of the ResolvedType passed to it. When getMemberMethods is called on the resulting ResolvedTypeWithMembers, java-classmate aggregates methods from the class and its parent types. The getName method on the ResolvedMember base class provides the identifier for these methods as they appear in the source code. Passing null for the annotation configuration and overrides ensures that the resolution uses default settings without applying extra metadata or member replacements.