Inspect resolved member methods
To inspect the methods of a generic type like ArrayList<String>, java-classmate provides a mechanism to resolve the full type hierarchy and extract member details. This process involves converting a raw class into a ResolvedType, then using a MemberResolver to aggregate all methods, including those inherited from superclasses and interfaces.
Resolving Type Members
The MemberResolver class is the entry point for gathering member information. By calling its resolve method, you obtain a ResolvedTypeWithMembers object, which acts as a container for the resolved fields, constructors, and methods of the target type.
When you call getMemberMethods on the ResolvedTypeWithMembers instance, java-classmate returns an array of ResolvedMethod objects. Each ResolvedMethod provides access to the method's metadata, such as its name via the getName method inherited from ResolvedMember.
The following example demonstrates how to resolve ArrayList<String> and verify that the add method is present in the resolved member list.
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 generic type ArrayList<String>
ResolvedType arrayListStringType = typeResolver.resolve(ArrayList.class, String.class);
// Initialize MemberResolver to extract members from the resolved type
MemberResolver memberResolver = new MemberResolver(typeResolver);
// Resolve members without specific annotation configurations or overrides
ResolvedTypeWithMembers typeWithMembers = memberResolver.resolve(arrayListStringType, null, null);
// Retrieve all member methods from the type and its hierarchy
ResolvedMethod[] methods = typeWithMembers.getMemberMethods();
boolean foundAddMethod = false;
for (ResolvedMethod method : methods) {
// Use getName to identify the method
if ("add".equals(method.getName())) {
foundAddMethod = true;
break;
}
}
// Verify that the expected method was found
if (!foundAddMethod) {
throw new AssertionError("Expected to find the 'add' method in ArrayList<String>.");
}
}
}
Internal Behavior
When MemberResolver.resolve is invoked, java-classmate performs the following:
- Hierarchy Traversal: It traverses the inheritance chain of the
ResolvedType, identifying all superclasses and implemented interfaces. - Member Aggregation: It collects raw methods from each level of the hierarchy.
- Type Binding: It binds generic type variables (like the
EinArrayList<E>) to the actual types provided (likeString), ensuring thatResolvedMethodreturn types and parameter types reflect the specific generic instantiation. - Member Access: The
getNamemethod provides the unqualified name of the method as defined in the source code, allowing for deterministic identification of members within theResolvedTypeWithMemberscollection.