Inspect resolved member methods
When you need to programmatically inspect the methods of a generic type like ArrayList<String>, standard tools often lose the specific type information of the generic parameters. java-classmate preserves these details by resolving the type hierarchy and its members into a structured representation.
To inspect the methods of a specific type, you first use a TypeResolver to create a ResolvedType. This object is then passed to a MemberResolver, which aggregates all fields, constructors, and methods. The resolve method on MemberResolver returns a ResolvedTypeWithMembers object, which provides access to the full set of members available on that type, including those inherited from parent classes or interfaces.
The getMemberMethods method returns an array of ResolvedMethod objects. Each ResolvedMethod (which extends ResolvedMember) allows you to access metadata such as the method name via getName().
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) {
// 1. Resolve the generic type ArrayList<String>
TypeResolver typeResolver = new TypeResolver();
ResolvedType arrayListType = typeResolver.resolve(ArrayList.class, String.class);
// 2. Prepare the MemberResolver to extract members
MemberResolver memberResolver = new MemberResolver(typeResolver);
// 3. Resolve members without additional annotation configuration or overrides
ResolvedTypeWithMembers members = memberResolver.resolve(arrayListType, null, null);
// 4. Iterate through member methods and verify the 'add' method exists
boolean foundAdd = false;
for (ResolvedMethod method : members.getMemberMethods()) {
if ("add".equals(method.getName())) {
foundAdd = true;
break;
}
}
if (!foundAdd) {
throw new AssertionError("Method 'add' not found in resolved ArrayList<String>");
}
}
}
Internally, MemberResolver.resolve processes the type hierarchy of the provided ResolvedType. It collects raw members and applies type substitutions based on the generic parameters (like String in ArrayList<String>). The resulting ResolvedTypeWithMembers instance acts as a container for these processed members, ensuring that getName() and other inspection methods return information consistent with the fully resolved generic context.