public abstract class ReflectUtil extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
ReflectUtil.MethodDispatcher<T>
Can invoke a method on an object of type E with return type T.
|
| Constructor and Description |
|---|
ReflectUtil() |
| Modifier and Type | Method and Description |
|---|---|
static <R extends ReflectiveVisitor,E> |
createDispatcher(Class<R> visitorBaseClazz,
Class<E> visiteeBaseClazz)
Creates a dispatcher for calls to
lookupVisitMethod(java.lang.Class<?>, java.lang.Class<?>, java.lang.String). |
static <E,T> ReflectUtil.MethodDispatcher<T> |
createMethodDispatcher(Class<T> returnClazz,
ReflectiveVisitor visitor,
String methodName,
Class<E> arg0Clazz,
Class... otherArgClasses)
Creates a dispatcher for calls to a single multi-method on a particular
object.
|
static Class |
getBoxingClass(Class primitiveClass)
Gets the Java boxing class for a primitive class.
|
static Method |
getByteBufferReadMethod(Class clazz)
Uses reflection to find the correct java.nio.ByteBuffer "absolute get"
method for a given primitive type.
|
static Method |
getByteBufferWriteMethod(Class clazz)
Uses reflection to find the correct java.nio.ByteBuffer "absolute put"
method for a given primitive type.
|
static String |
getUnmangledMethodName(Class declaringClass,
String methodName,
Class[] paramTypes)
Composes a string representing a human-readable method name (with neither
exception nor return type information).
|
static String |
getUnmangledMethodName(Method method)
Composes a string representing a human-readable method name (with neither
exception nor return type information).
|
static String |
getUnqualifiedClassName(Class c)
Gets the name of a class with no package qualifiers; if it's an inner
class, it will still be qualified by the containing class (X$Y).
|
static boolean |
invokeVisitor(ReflectiveVisitor visitor,
Object visitee,
Class hierarchyRoot,
String visitMethodName)
Implements the
Glossary.VISITOR_PATTERN via
reflection. |
static Method |
lookupVisitMethod(Class<?> visitorClass,
Class<?> visiteeClass,
String visitMethodName)
Looks up a visit method.
|
static Method |
lookupVisitMethod(Class<?> visitorClass,
Class<?> visiteeClass,
String visitMethodName,
List<Class> additionalParameterTypes)
Looks up a visit method taking additional parameters beyond the
overloaded visitee type.
|
public static Method getByteBufferReadMethod(Class clazz)
clazz - the Class object representing the primitive typepublic static Method getByteBufferWriteMethod(Class clazz)
clazz - the Class object representing the primitive typepublic static Class getBoxingClass(Class primitiveClass)
primitiveClass - representative class for primitive (e.g.
java.lang.Integer.TYPE)public static String getUnqualifiedClassName(Class c)
c - the class of interestpublic static String getUnmangledMethodName(Class declaringClass, String methodName, Class[] paramTypes)
declaringClass - class on which method is definedmethodName - simple name of method without signatureparamTypes - method parameter typespublic static String getUnmangledMethodName(Method method)
method - method whose name is to be generatedpublic static boolean invokeVisitor(ReflectiveVisitor visitor, Object visitee, Class hierarchyRoot, String visitMethodName)
Glossary.VISITOR_PATTERN via
reflection. The basic technique is taken from a
Javaworld article. For an example of how to use it, see
ReflectVisitorTest.
Visit method lookup follows the same rules as if compile-time resolution for VisitorClass.visit(VisiteeClass) were performed. An ambiguous match due to multiple interface inheritance results in an IllegalArgumentException. A non-match is indicated by returning false.
visitor - object whose visit method is to be invokedvisitee - object to be passed as a parameter to the visit
methodhierarchyRoot - if non-null, visitor method will only be invoked if
it takes a parameter whose type is a subtype of
hierarchyRootvisitMethodName - name of visit method, e.g. "visit"public static Method lookupVisitMethod(Class<?> visitorClass, Class<?> visiteeClass, String visitMethodName)
visitorClass - class of object whose visit method is to be invokedvisiteeClass - class of object to be passed as a parameter to the
visit methodvisitMethodName - name of visit methodpublic static Method lookupVisitMethod(Class<?> visitorClass, Class<?> visiteeClass, String visitMethodName, List<Class> additionalParameterTypes)
visitorClass - class of object whose visit method is to be
invokedvisiteeClass - class of object to be passed as a parameter
to the visit methodvisitMethodName - name of visit methodadditionalParameterTypes - list of additional parameter typescreateDispatcher(Class, Class)public static <R extends ReflectiveVisitor,E> ReflectiveVisitDispatcher<R,E> createDispatcher(Class<R> visitorBaseClazz, Class<E> visiteeBaseClazz)
lookupVisitMethod(java.lang.Class<?>, java.lang.Class<?>, java.lang.String). The
dispatcher caches methods between invocations.visitorBaseClazz - Visitor base classvisiteeBaseClazz - Visitee base classpublic static <E,T> ReflectUtil.MethodDispatcher<T> createMethodDispatcher(Class<T> returnClazz, ReflectiveVisitor visitor, String methodName, Class<E> arg0Clazz, Class... otherArgClasses)
Calls to that multi-method are resolved by looking for a method on the runtime type of that object, with the required name, and with the correct type or a subclass for the first argument, and precisely the same types for other arguments.
For instance, a dispatcher created for the method
String foo(Vehicle, int, List)could be used to call the methods
String foo(Car, int, List)(because Car and Bus are subclasses of Vehicle, and they occur in the polymorphic first argument) but not the method
String foo(Bus, int, List)
String foo(Car, int, ArrayList)(only the first argument is polymorphic).
You must create an implementation of the method for the base class.
Otherwise throws IllegalArgumentException.
returnClazz - Return type of methodvisitor - Object on which to invoke the methodmethodName - Name of methodarg0Clazz - Base type of argument zerootherArgClasses - Types of remaining argumentsCopyright © 2012–2015 The Apache Software Foundation. All rights reserved.