com.samskivert.util
Class MethodFinder

java.lang.Object
  extended by com.samskivert.util.MethodFinder

public class MethodFinder
extends Object

Finds methods and constructors that can be invoked reflectively. Attempts to address some of the limitations of the JDK's Class.getMethod(java.lang.String, java.lang.Class...) and Class.getConstructor(java.lang.Class...), and other JDK reflective facilities.

Because those methods only match exact method signatures, one is unable to perform the same method matching that the compiler does at compile time (e.g. matching the method foo(Exception) when the user wants to call a method named foo with an IOException argument) with the basic reflection services. This class implements the method resolution process according to the same rules used by a Java compiler. These rules are outlined in the Java Language Specification, variously in sections 5.1.2, 5.1.4, 5.3, and 15.12.2.

This code was adapted from code provided by Paul Hosler in an article for Java Report Online.


Field Summary
protected  Class<?> _clazz
          The target class to look for methods and constructors in.
protected  List<Member> _ctorList
          List of the Constructors in the target class.
protected  Map<String,List<Member>> _methodMap
          Mapping from method name to the Methods in the target class with that name.
protected  Map<Member,Class<?>[]> _paramMap
          Mapping from a Constructor or Method object to the Class objects representing its formal parameters.
 
Constructor Summary
MethodFinder(Class<?> clazz)
          Constructs a method finder for the supplied class.
 
Method Summary
 boolean equals(Object o)
           
 Constructor<?> findConstructor(Class<?>[] parameterTypes)
          Returns the most specific public constructor in my target class that accepts the number and type of parameters in the given Class array in a reflective invocation.
protected  Member findMemberIn(List<Member> memberList, Class<?>[] parameterTypes)
          Basis of findConstructor(java.lang.Class[]) and findMethod(java.lang.String, java.lang.Class[]).
 Method findMethod(String methodName, Class<?>[] parameterTypes)
          Returns the most specific public method in my target class that has the given name and accepts the number and type of parameters in the given Class array in a reflective invocation.
 Method findMethod(String methodName, Object[] args)
          Like findMethod(String,Class[]) except that it takes the actual arguments that will be passed to the found method and creates the array of class objects for you using ClassUtil.getParameterTypesFrom(java.lang.Object[]).
protected  Member findMostSpecificMemberIn(List<Member> memberList)
           
 int hashCode()
           
protected  void maybeLoadConstructors()
          Loads up the data structures for my target class's constructors.
protected  void maybeLoadMethods()
          Loads up the data structures for my target class's methods.
protected  boolean memberIsMoreSpecific(Member first, Member second)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_clazz

protected Class<?> _clazz
The target class to look for methods and constructors in.


_methodMap

protected Map<String,List<Member>> _methodMap
Mapping from method name to the Methods in the target class with that name.


_ctorList

protected List<Member> _ctorList
List of the Constructors in the target class.


_paramMap

protected Map<Member,Class<?>[]> _paramMap
Mapping from a Constructor or Method object to the Class objects representing its formal parameters.

Constructor Detail

MethodFinder

public MethodFinder(Class<?> clazz)
Constructs a method finder for the supplied class.

Parameters:
clazz - Class in which I will look for methods and constructors.
Throws:
IllegalArgumentException - if clazz is null, or represents a primitive, or represents an array type.
Method Detail

equals

public boolean equals(Object o)
Overrides:
equals in class Object

findConstructor

public Constructor<?> findConstructor(Class<?>[] parameterTypes)
                               throws NoSuchMethodException
Returns the most specific public constructor in my target class that accepts the number and type of parameters in the given Class array in a reflective invocation.

A null value or Void.TYPE parameterTypes matches a corresponding Object or array reference in a constructor's formal parameter list, but not a primitive formal parameter.

Parameters:
parameterTypes - array representing the number and types of parameters to look for in the constructor's signature. A null array is treated as a zero-length array.
Returns:
Constructor object satisfying the conditions.
Throws:
NoSuchMethodException - if no constructors match the criteria, or if the reflective call is ambiguous based on the parameter types.

findMethod

public Method findMethod(String methodName,
                         Class<?>[] parameterTypes)
                  throws NoSuchMethodException
Returns the most specific public method in my target class that has the given name and accepts the number and type of parameters in the given Class array in a reflective invocation.

A null value or Void.TYPE in parameterTypes will match a corresponding Object or array reference in a method's formal parameter list, but not a primitive formal parameter.

Parameters:
methodName - name of the method to search for.
parameterTypes - array representing the number and types of parameters to look for in the method's signature. A null array is treated as a zero-length array.
Returns:
Method object satisfying the conditions.
Throws:
NoSuchMethodException - if no methods match the criteria, or if the reflective call is ambiguous based on the parameter types, or if methodName is null.

findMethod

public Method findMethod(String methodName,
                         Object[] args)
                  throws NoSuchMethodException
Like findMethod(String,Class[]) except that it takes the actual arguments that will be passed to the found method and creates the array of class objects for you using ClassUtil.getParameterTypesFrom(java.lang.Object[]).

Throws:
NoSuchMethodException

findMemberIn

protected Member findMemberIn(List<Member> memberList,
                              Class<?>[] parameterTypes)
                       throws NoSuchMethodException
Basis of findConstructor(java.lang.Class[]) and findMethod(java.lang.String, java.lang.Class[]). The member list fed to this method will be either all Constructor objects or all Method objects.

Throws:
NoSuchMethodException

findMostSpecificMemberIn

protected Member findMostSpecificMemberIn(List<Member> memberList)
                                   throws NoSuchMethodException
Parameters:
memberList - a list of members (either all constructors or all methods).
Returns:
the most specific of all members in the list.
Throws:
NoSuchMethodException - if there is an ambiguity as to which is most specific.

hashCode

public int hashCode()
Overrides:
hashCode in class Object

maybeLoadConstructors

protected void maybeLoadConstructors()
Loads up the data structures for my target class's constructors.


maybeLoadMethods

protected void maybeLoadMethods()
Loads up the data structures for my target class's methods.


memberIsMoreSpecific

protected boolean memberIsMoreSpecific(Member first,
                                       Member second)
Parameters:
first - a Member.
second - a Member.
Returns:
true if the first Member is more specific than the second, false otherwise. Specificity is determined according to the procedure in the Java Language Specification, section 15.12.2.


Copyright © 2000-2008 Michael Bayne. All Rights Reserved.