|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.samskivert.util.MethodFinder
public class MethodFinder
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 |
|---|
protected Class<?> _clazz
protected Map<String,List<Member>> _methodMap
protected List<Member> _ctorList
protected Map<Member,Class<?>[]> _paramMap
| Constructor Detail |
|---|
public MethodFinder(Class<?> clazz)
clazz - Class in which I will look for methods and constructors.
IllegalArgumentException - if clazz is null, or represents a primitive, or
represents an array type.| Method Detail |
|---|
public boolean equals(Object o)
equals in class Object
public Constructor<?> findConstructor(Class<?>[] parameterTypes)
throws NoSuchMethodException
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.
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.
NoSuchMethodException - if no constructors match the criteria, or if the reflective
call is ambiguous based on the parameter types.
public Method findMethod(String methodName,
Class<?>[] parameterTypes)
throws NoSuchMethodException
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.
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.
NoSuchMethodException - if no methods match the criteria, or if the reflective call
is ambiguous based on the parameter types, or if methodName is null.
public Method findMethod(String methodName,
Object[] args)
throws NoSuchMethodException
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[]).
NoSuchMethodException
protected Member findMemberIn(List<Member> memberList,
Class<?>[] parameterTypes)
throws NoSuchMethodException
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.
NoSuchMethodException
protected Member findMostSpecificMemberIn(List<Member> memberList)
throws NoSuchMethodException
memberList - a list of members (either all constructors or all methods).
NoSuchMethodException - if there is an ambiguity as to which is most specific.public int hashCode()
hashCode in class Objectprotected void maybeLoadConstructors()
protected void maybeLoadMethods()
protected boolean memberIsMoreSpecific(Member first,
Member second)
first - a Member.second - a Member.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||