Widening.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2100 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2007-05-08 23:40:51 +00:00
parent 29e45ddf96
commit 6642bc00e7
+96 -136
View File
@@ -24,85 +24,72 @@ import java.lang.reflect.*;
import java.util.*; import java.util.*;
/** /**
* Finds methods and constructors that can be invoked reflectively. * Finds methods and constructors that can be invoked reflectively. Attempts to address some of
* Attempts to address some of the limitations of the JDK's {@link * the limitations of the JDK's {@link Class#getMethod} and {@link Class#getConstructor}, and other
* Class#getMethod} and {@link Class#getConstructor}, and other JDK * JDK reflective facilities.
* reflective facilities.
* *
* <p> Because those methods only match exact method signatures, one is * <p> Because those methods only match exact method signatures, one is unable to perform the same
* unable to perform the same method matching that the compiler does at * method matching that the compiler does at compile time (e.g. matching the method
* compile time (e.g. matching the method <code>foo(Exception)</code> when * <code>foo(Exception)</code> when the user wants to call a method named <code>foo</code> with an
* the user wants to call a method named <code>foo</code> with an * <code>IOException</code> argument) with the basic reflection services. This class implements
* <code>IOException</code> argument) with the basic reflection services. * the method resolution process according to the same rules used by a Java compiler. These rules
* This class implements the method resolution process according to the * are outlined in the Java Language Specification, variously in sections 5.1.2, 5.1.4, 5.3, and
* same rules used by a Java compiler. These rules are outlined in the * 15.12.2.
* Java Language Specification, variously in sections 5.1.2, 5.1.4, 5.3,
* and 15.12.2.
* *
* <p> This code was adapted from code provided by Paul Hosler in <a * <p> This code was adapted from code provided by Paul Hosler in <a
* href="http://www.javareport.com/html/from_pages/article.asp?id=4276">an * href="http://www.javareport.com/html/from_pages/article.asp?id=4276">an article</a> for Java
* article</a> for Java Report Online. * Report Online.
*/ */
public class MethodFinder public class MethodFinder
{ {
/** /**
* Constructs a method finder for the supplied class. * Constructs a method finder for the supplied class.
* *
* @param clazz Class in which I will look for methods and * @param clazz Class in which I will look for methods and constructors.
* constructors.
* *
* @exception IllegalArgumentException if clazz is null, or represents * @exception IllegalArgumentException if clazz is null, or represents a primitive, or
* a primitive, or represents an array type. * represents an array type.
*/ */
public MethodFinder (Class clazz) public MethodFinder (Class clazz)
{ {
if (clazz == null) { if (clazz == null) {
throw new IllegalArgumentException("null Class parameter"); throw new IllegalArgumentException("null Class parameter");
} }
if (clazz.isPrimitive()) { if (clazz.isPrimitive()) {
throw new IllegalArgumentException( throw new IllegalArgumentException("primitive Class parameter");
"primitive Class parameter");
} }
if (clazz.isArray()) { if (clazz.isArray()) {
throw new IllegalArgumentException( throw new IllegalArgumentException("array Class parameter");
"array Class parameter");
} }
_clazz = clazz;
this.clazz = clazz;
} }
public boolean equals (Object o) public boolean equals (Object o)
{ {
if (this == o) if (this == o) {
return true; return true;
else if (o == null || getClass() != o.getClass()) } else if (o == null || getClass() != o.getClass()) {
return false; return false;
else { } else {
MethodFinder other = (MethodFinder)o; MethodFinder other = (MethodFinder)o;
return clazz.equals(other.clazz); return _clazz.equals(other._clazz);
} }
} }
/** /**
* Returns the most specific public constructor in my target class * Returns the most specific public constructor in my target class that accepts the number and
* that accepts the number and type of parameters in the given Class * type of parameters in the given Class array in a reflective invocation.
* array in a reflective invocation.
* *
* <p> A null value or Void.TYPE parameterTypes matches a * <p> A null value or Void.TYPE parameterTypes matches a corresponding Object or array
* corresponding Object or array reference in a constructor's formal * reference in a constructor's formal parameter list, but not a primitive formal parameter.
* parameter list, but not a primitive formal parameter.
* *
* @param parameterTypes array representing the number and types of * @param parameterTypes array representing the number and types of parameters to look for in
* parameters to look for in the constructor's signature. A null * the constructor's signature. A null array is treated as a zero-length array.
* array is treated as a zero-length array.
* *
* @return Constructor object satisfying the conditions. * @return Constructor object satisfying the conditions.
* *
* @exception NoSuchMethodException if no constructors match the * @exception NoSuchMethodException if no constructors match the criteria, or if the reflective
* criteria, or if the reflective call is ambiguous based on the * call is ambiguous based on the parameter types.
* parameter types.
*/ */
public Constructor findConstructor (Class[] parameterTypes) public Constructor findConstructor (Class[] parameterTypes)
throws NoSuchMethodException throws NoSuchMethodException
@@ -114,28 +101,25 @@ public class MethodFinder
parameterTypes = new Class[0]; parameterTypes = new Class[0];
} }
return (Constructor)findMemberIn(ctorList, parameterTypes); return (Constructor)findMemberIn(_ctorList, parameterTypes);
} }
/** /**
* Returns the most specific public method in my target class that has * Returns the most specific public method in my target class that has the given name and
* the given name and accepts the number and type of parameters in the * accepts the number and type of parameters in the given Class array in a reflective
* given Class array in a reflective invocation. * invocation.
* *
* <p> A null value or Void.TYPE in parameterTypes will match a * <p> A null value or Void.TYPE in parameterTypes will match a corresponding Object or array
* corresponding Object or array reference in a method's formal * reference in a method's formal parameter list, but not a primitive formal parameter.
* parameter list, but not a primitive formal parameter.
* *
* @param methodName name of the method to search for. * @param methodName name of the method to search for.
* @param parameterTypes array representing the number and types of * @param parameterTypes array representing the number and types of parameters to look for in
* parameters to look for in the method's signature. A null array is * the method's signature. A null array is treated as a zero-length array.
* treated as a zero-length array.
* *
* @return Method object satisfying the conditions. * @return Method object satisfying the conditions.
* *
* @exception NoSuchMethodException if no methods match the criteria, * @exception NoSuchMethodException if no methods match the criteria, or if the reflective call
* or if the reflective call is ambiguous based on the parameter * is ambiguous based on the parameter types, or if methodName is null.
* types, or if methodName is null.
*/ */
public Method findMethod (String methodName, Class[] parameterTypes) public Method findMethod (String methodName, Class[] parameterTypes)
throws NoSuchMethodException throws NoSuchMethodException
@@ -143,10 +127,10 @@ public class MethodFinder
// make sure the constructor list is loaded // make sure the constructor list is loaded
maybeLoadMethods(); maybeLoadMethods();
List<Member> methodList = methodMap.get(methodName); List<Member> methodList = _methodMap.get(methodName);
if (methodList == null) { if (methodList == null) {
throw new NoSuchMethodException( throw new NoSuchMethodException(
"No method named " + clazz.getName() + "." + methodName); "No method named " + _clazz.getName() + "." + methodName);
} }
if (parameterTypes == null) { if (parameterTypes == null) {
@@ -157,9 +141,8 @@ public class MethodFinder
} }
/** /**
* Like {@link #findMethod(String,Class[])} except that it takes the * Like {@link #findMethod(String,Class[])} except that it takes the actual arguments that will
* actual arguments that will be passed to the found method and * be passed to the found method and creates the array of class objects for you using {@link
* creates the array of class objects for you using {@link
* ClassUtil#getParameterTypesFrom}. * ClassUtil#getParameterTypesFrom}.
*/ */
public Method findMethod (String methodName, Object[] args) public Method findMethod (String methodName, Object[] args)
@@ -169,34 +152,31 @@ public class MethodFinder
} }
/** /**
* Basis of {@link #findConstructor} and {@link #findMethod}. The * Basis of {@link #findConstructor} and {@link #findMethod}. The member list fed to this
* member list fed to this method will be either all {@link * method will be either all {@link Constructor} objects or all {@link Method} objects.
* Constructor} objects or all {@link Method} objects.
*/ */
private Member findMemberIn (List<Member> memberList, protected Member findMemberIn (List<Member> memberList, Class[] parameterTypes)
Class[] parameterTypes)
throws NoSuchMethodException throws NoSuchMethodException
{ {
List<Member> matchingMembers = new ArrayList<Member>(); List<Member> matchingMembers = new ArrayList<Member>();
for (Iterator<Member> it = memberList.iterator(); it.hasNext();) { for (Iterator<Member> it = memberList.iterator(); it.hasNext();) {
Member member = it.next(); Member member = it.next();
Class[] methodParamTypes = paramMap.get(member); Class[] methodParamTypes = _paramMap.get(member);
// check for exactly equal method signature // check for exactly equal method signature
if (Arrays.equals(methodParamTypes, parameterTypes)) { if (Arrays.equals(methodParamTypes, parameterTypes)) {
return member; return member;
} }
if (ClassUtil.compatibleClasses( if (ClassUtil.compatibleClasses(methodParamTypes, parameterTypes)) {
methodParamTypes, parameterTypes)) {
matchingMembers.add(member); matchingMembers.add(member);
} }
} }
if (matchingMembers.isEmpty()) { if (matchingMembers.isEmpty()) {
throw new NoSuchMethodException( throw new NoSuchMethodException(
"No member in " + clazz.getName() + " matching given args"); "No member in " + _clazz.getName() + " matching given args");
} }
if (matchingMembers.size() == 1) { if (matchingMembers.size() == 1) {
return (Member)matchingMembers.get(0); return (Member)matchingMembers.get(0);
@@ -206,15 +186,13 @@ public class MethodFinder
} }
/** /**
* @param memberList a list of members (either all constructors or all * @param memberList a list of members (either all constructors or all methods).
* methods).
* *
* @return the most specific of all members in the list. * @return the most specific of all members in the list.
* *
* @exception NoSuchMethodException if there is an ambiguity as to * @exception NoSuchMethodException if there is an ambiguity as to which is most specific.
* which is most specific.
*/ */
private Member findMostSpecificMemberIn (List<Member> memberList) protected Member findMostSpecificMemberIn (List<Member> memberList)
throws NoSuchMethodException throws NoSuchMethodException
{ {
List<Member> mostSpecificMembers = new ArrayList<Member>(); List<Member> mostSpecificMembers = new ArrayList<Member>();
@@ -228,24 +206,19 @@ public class MethodFinder
boolean moreSpecific = true; boolean moreSpecific = true;
boolean lessSpecific = false; boolean lessSpecific = false;
// Is member more specific than everyone in the // Is member more specific than everyone in the most-specific set?
// most-specific set?
for (Member moreSpecificMember : mostSpecificMembers) { for (Member moreSpecificMember : mostSpecificMembers) {
if (!memberIsMoreSpecific(member, moreSpecificMember)) { if (!memberIsMoreSpecific(member, moreSpecificMember)) {
// if the candidate member is not more specific // if the candidate member is not more specific than this member, then it's
// than this member, then it's not more specific // not more specific than the entire set, but it may still be equivalently
// than the entire set, but it may still be // specific, so we check that next
// equivalently specific, so we check that next
moreSpecific = false; moreSpecific = false;
// we check for a member of equal specificity by // we check for a member of equal specificity by checking to see if this
// checking to see if this most specific member is // most specific member is explicitly more specific than the candidate
// explicitly more specific than the candidate // member. if it is more specific, the candidate member can be chucked,
// member. if it is more specific, the candidate // otherwise we need to add the candidate member to the most-specific set
// member can be chucked, otherwise we need to add lessSpecific = memberIsMoreSpecific(moreSpecificMember, member);
// the candidate member to the most-specific set
lessSpecific =
memberIsMoreSpecific(moreSpecificMember, member);
break; break;
} }
} }
@@ -264,30 +237,29 @@ public class MethodFinder
if (mostSpecificMembers.size() > 1) { if (mostSpecificMembers.size() > 1) {
throw new NoSuchMethodException( throw new NoSuchMethodException(
"Ambiguous request for member in " "Ambiguous request for member in " + _clazz.getName() + " matching given args" );
+ clazz.getName()
+ " matching given args" );
} }
return (Member)mostSpecificMembers.get(0); return (Member)mostSpecificMembers.get(0);
} }
@Override // from Object
public int hashCode () public int hashCode ()
{ {
return clazz.hashCode(); return _clazz.hashCode();
} }
/** /**
* Loads up the data structures for my target class's constructors. * Loads up the data structures for my target class's constructors.
*/ */
private void maybeLoadConstructors () protected void maybeLoadConstructors ()
{ {
if (ctorList == null) { if (_ctorList == null) {
ctorList = new ArrayList<Member>(); _ctorList = new ArrayList<Member>();
Constructor[] ctors = clazz.getConstructors(); Constructor[] ctors = _clazz.getConstructors();
for (int i = 0; i < ctors.length; ++i) { for (int i = 0; i < ctors.length; ++i) {
ctorList.add(ctors[i]); _ctorList.add(ctors[i]);
paramMap.put(ctors[i], ctors[i].getParameterTypes()); _paramMap.put(ctors[i], ctors[i].getParameterTypes());
} }
} }
} }
@@ -295,32 +267,30 @@ public class MethodFinder
/** /**
* Loads up the data structures for my target class's methods. * Loads up the data structures for my target class's methods.
*/ */
private void maybeLoadMethods () protected void maybeLoadMethods ()
{ {
if (methodMap == null) { if (_methodMap == null) {
methodMap = new HashMap<String,List<Member>>(); _methodMap = new HashMap<String,List<Member>>();
Method[] methods = clazz.getMethods(); Method[] methods = _clazz.getMethods();
for (int i = 0; i < methods.length; ++i) { for (int i = 0; i < methods.length; ++i) {
Method m = methods[i]; Method m = methods[i];
String methodName = m.getName(); String methodName = m.getName();
Class[] paramTypes = m.getParameterTypes(); Class[] paramTypes = m.getParameterTypes();
List<Member> list = methodMap.get(methodName); List<Member> list = _methodMap.get(methodName);
if (list == null) { if (list == null) {
list = new ArrayList<Member>(); list = new ArrayList<Member>();
methodMap.put(methodName, list); _methodMap.put(methodName, list);
} }
if (!ClassUtil.classIsAccessible(clazz)) { if (!ClassUtil.classIsAccessible(_clazz)) {
m = ClassUtil.getAccessibleMethodFrom( m = ClassUtil.getAccessibleMethodFrom(_clazz, methodName, paramTypes );
clazz, methodName, paramTypes );
} }
if (m != null) { if (m != null) {
list.add(m); list.add(m);
paramMap.put(m, paramTypes); _paramMap.put(m, paramTypes);
} }
} }
} }
@@ -330,37 +300,27 @@ public class MethodFinder
* @param first a Member. * @param first a Member.
* @param second a Member. * @param second a Member.
* *
* @return true if the first Member is more specific than the second, * @return true if the first Member is more specific than the second, false otherwise.
* false otherwise. Specificity is determined according to the * Specificity is determined according to the procedure in the Java Language Specification,
* procedure in the Java Language Specification, section 15.12.2. * section 15.12.2.
*/ */
private boolean memberIsMoreSpecific (Member first, Member second) protected boolean memberIsMoreSpecific (Member first, Member second)
{ {
Class[] firstParamTypes = paramMap.get(first); Class[] firstParamTypes = _paramMap.get(first);
Class[] secondParamTypes = paramMap.get(second); Class[] secondParamTypes = _paramMap.get(second);
return ClassUtil.compatibleClasses( return ClassUtil.compatibleClasses(secondParamTypes, firstParamTypes);
secondParamTypes, firstParamTypes);
} }
/** /** The target class to look for methods and constructors in. */
* The target class to look for methods and constructors in. protected Class _clazz;
*/
private Class clazz;
/** /** Mapping from method name to the Methods in the target class with that name. */
* Mapping from method name to the Methods in the target class with protected Map<String,List<Member>> _methodMap = null;
* that name.
*/
private Map<String,List<Member>> methodMap = null;
/** /** List of the Constructors in the target class. */
* List of the Constructors in the target class. protected List<Member> _ctorList = null;
*/
private List<Member> ctorList = null;
/** /** Mapping from a Constructor or Method object to the Class objects representing its formal
* Mapping from a Constructor or Method object to the Class objects * parameters. */
* representing its formal parameters. protected Map<Member,Class[]> _paramMap = new HashMap<Member,Class[]>();
*/
private Map<Member,Class[]> paramMap = new HashMap<Member,Class[]>();
} }