Class ReflectUtil
- java.lang.Object
-
- org.apache.calcite.util.ReflectUtil
-
public abstract class ReflectUtil extends java.lang.Object
Static utilities for Java reflection.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ReflectUtil.MethodDispatcher<T>
Can invoke a method on an object of type E with return type T.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.Class,java.lang.Class>
primitiveToBoxingMap
private static java.util.Map<java.lang.Class,java.lang.reflect.Method>
primitiveToByteBufferReadMethod
private static java.util.Map<java.lang.Class,java.lang.reflect.Method>
primitiveToByteBufferWriteMethod
-
Constructor Summary
Constructors Constructor Description ReflectUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <R extends ReflectiveVisitor,E>
ReflectiveVisitDispatcher<R,E>createDispatcher(java.lang.Class<R> visitorBaseClazz, java.lang.Class<E> visiteeBaseClazz)
Creates a dispatcher for calls tolookupVisitMethod(java.lang.Class<?>, java.lang.Class<?>, java.lang.String)
.static <E,T>
ReflectUtil.MethodDispatcher<T>createMethodDispatcher(java.lang.Class<T> returnClazz, ReflectiveVisitor visitor, java.lang.String methodName, java.lang.Class<E> arg0Clazz, java.lang.Class... otherArgClasses)
Creates a dispatcher for calls to a single multi-method on a particular object.static java.lang.Class
getBoxingClass(java.lang.Class primitiveClass)
Gets the Java boxing class for a primitive class.static java.lang.reflect.Method
getByteBufferReadMethod(java.lang.Class clazz)
Uses reflection to find the correct java.nio.ByteBuffer "absolute get" method for a given primitive type.static java.lang.reflect.Method
getByteBufferWriteMethod(java.lang.Class clazz)
Uses reflection to find the correct java.nio.ByteBuffer "absolute put" method for a given primitive type.static java.lang.String
getParameterName(java.lang.reflect.Method method, int i)
Derives the name of thei
th parameter of a method.static java.lang.String
getUnmangledMethodName(java.lang.Class declaringClass, java.lang.String methodName, java.lang.Class[] paramTypes)
Composes a string representing a human-readable method name (with neither exception nor return type information).static java.lang.String
getUnmangledMethodName(java.lang.reflect.Method method)
Composes a string representing a human-readable method name (with neither exception nor return type information).static java.lang.String
getUnqualifiedClassName(java.lang.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, java.lang.Object visitee, java.lang.Class hierarchyRoot, java.lang.String visitMethodName)
Implements theGlossary.VISITOR_PATTERN
via reflection.private static boolean
invokeVisitorInternal(java.lang.Object visitor, java.lang.Object visitee, java.lang.Class hierarchyRoot, java.lang.String visitMethodName)
Shared implementation of the two forms of invokeVisitor.static boolean
isParameterOptional(java.lang.reflect.Method method, int i)
Derives whether thei
th parameter of a method is optional.static java.lang.reflect.Method
lookupVisitMethod(java.lang.Class<?> visitorClass, java.lang.Class<?> visiteeClass, java.lang.String visitMethodName)
Looks up a visit method.private static java.lang.reflect.Method
lookupVisitMethod(java.lang.Class<?> visitorClass, java.lang.Class<?> visiteeClass, java.lang.String visitMethodName, java.lang.Class<?>[] paramTypes, java.util.Map<java.lang.Class<?>,java.lang.reflect.Method> cache)
static java.lang.reflect.Method
lookupVisitMethod(java.lang.Class<?> visitorClass, java.lang.Class<?> visiteeClass, java.lang.String visitMethodName, java.util.List<java.lang.Class> additionalParameterTypes)
Looks up a visit method taking additional parameters beyond the overloaded visitee type.
-
-
-
Field Detail
-
primitiveToBoxingMap
private static java.util.Map<java.lang.Class,java.lang.Class> primitiveToBoxingMap
-
primitiveToByteBufferReadMethod
private static java.util.Map<java.lang.Class,java.lang.reflect.Method> primitiveToByteBufferReadMethod
-
primitiveToByteBufferWriteMethod
private static java.util.Map<java.lang.Class,java.lang.reflect.Method> primitiveToByteBufferWriteMethod
-
-
Method Detail
-
getByteBufferReadMethod
public static java.lang.reflect.Method getByteBufferReadMethod(java.lang.Class clazz)
Uses reflection to find the correct java.nio.ByteBuffer "absolute get" method for a given primitive type.- Parameters:
clazz
- the Class object representing the primitive type- Returns:
- corresponding method
-
getByteBufferWriteMethod
public static java.lang.reflect.Method getByteBufferWriteMethod(java.lang.Class clazz)
Uses reflection to find the correct java.nio.ByteBuffer "absolute put" method for a given primitive type.- Parameters:
clazz
- the Class object representing the primitive type- Returns:
- corresponding method
-
getBoxingClass
public static java.lang.Class getBoxingClass(java.lang.Class primitiveClass)
Gets the Java boxing class for a primitive class.- Parameters:
primitiveClass
- representative class for primitive (e.g. java.lang.Integer.TYPE)- Returns:
- corresponding boxing Class (e.g. java.lang.Integer)
-
getUnqualifiedClassName
public static java.lang.String getUnqualifiedClassName(java.lang.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).- Parameters:
c
- the class of interest- Returns:
- the unqualified name
-
getUnmangledMethodName
public static java.lang.String getUnmangledMethodName(java.lang.Class declaringClass, java.lang.String methodName, java.lang.Class[] paramTypes)
Composes a string representing a human-readable method name (with neither exception nor return type information).- Parameters:
declaringClass
- class on which method is definedmethodName
- simple name of method without signatureparamTypes
- method parameter types- Returns:
- unmangled method name
-
getUnmangledMethodName
public static java.lang.String getUnmangledMethodName(java.lang.reflect.Method method)
Composes a string representing a human-readable method name (with neither exception nor return type information).- Parameters:
method
- method whose name is to be generated- Returns:
- unmangled method name
-
invokeVisitor
public static boolean invokeVisitor(ReflectiveVisitor visitor, java.lang.Object visitee, java.lang.Class hierarchyRoot, java.lang.String visitMethodName)
Implements theGlossary.VISITOR_PATTERN
via reflection. The basic technique is taken from a Javaworld article. For an example of how to use it, seeReflectVisitorTest
.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.
- Parameters:
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"- Returns:
- true if a matching visit method was found and invoked
-
invokeVisitorInternal
private static boolean invokeVisitorInternal(java.lang.Object visitor, java.lang.Object visitee, java.lang.Class hierarchyRoot, java.lang.String visitMethodName)
Shared implementation of the two forms of invokeVisitor.- Parameters:
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"- Returns:
- true if a matching visit method was found and invoked
-
lookupVisitMethod
public static java.lang.reflect.Method lookupVisitMethod(java.lang.Class<?> visitorClass, java.lang.Class<?> visiteeClass, java.lang.String visitMethodName)
Looks up a visit method.- Parameters:
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 method- Returns:
- method found, or null if none found
-
lookupVisitMethod
public static java.lang.reflect.Method lookupVisitMethod(java.lang.Class<?> visitorClass, java.lang.Class<?> visiteeClass, java.lang.String visitMethodName, java.util.List<java.lang.Class> additionalParameterTypes)
Looks up a visit method taking additional parameters beyond the overloaded visitee type.- Parameters:
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 types- Returns:
- method found, or null if none found
- See Also:
createDispatcher(Class, Class)
-
lookupVisitMethod
private static java.lang.reflect.Method lookupVisitMethod(java.lang.Class<?> visitorClass, java.lang.Class<?> visiteeClass, java.lang.String visitMethodName, java.lang.Class<?>[] paramTypes, java.util.Map<java.lang.Class<?>,java.lang.reflect.Method> cache)
-
createDispatcher
public static <R extends ReflectiveVisitor,E> ReflectiveVisitDispatcher<R,E> createDispatcher(java.lang.Class<R> visitorBaseClazz, java.lang.Class<E> visiteeBaseClazz)
Creates a dispatcher for calls tolookupVisitMethod(java.lang.Class<?>, java.lang.Class<?>, java.lang.String)
. The dispatcher caches methods between invocations.- Parameters:
visitorBaseClazz
- Visitor base classvisiteeBaseClazz
- Visitee base class- Returns:
- cache of methods
-
createMethodDispatcher
public static <E,T> ReflectUtil.MethodDispatcher<T> createMethodDispatcher(java.lang.Class<T> returnClazz, ReflectiveVisitor visitor, java.lang.String methodName, java.lang.Class<E> arg0Clazz, java.lang.Class... otherArgClasses)
Creates a dispatcher for calls to a single multi-method on a particular object.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)
String foo(Bus, int, List)(because Car and Bus are subclasses of Vehicle, and they occur in the polymorphic first argument) but not the method
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
.- Parameters:
returnClazz
- Return type of methodvisitor
- Object on which to invoke the methodmethodName
- Name of methodarg0Clazz
- Base type of argument zerootherArgClasses
- Types of remaining arguments
-
getParameterName
public static java.lang.String getParameterName(java.lang.reflect.Method method, int i)
Derives the name of thei
th parameter of a method.
-
isParameterOptional
public static boolean isParameterOptional(java.lang.reflect.Method method, int i)
Derives whether thei
th parameter of a method is optional.
-
-