Package uk.ac.ebi.utils.reflection
Class ReflectionUtils
java.lang.Object
uk.ac.ebi.utils.reflection.ReflectionUtils
Jan 15, 2008
- Author:
- brandizi
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <ArgumentType,
BaseType>
Class<ArgumentType>getTypeArgument
(Class<BaseType> baseClass, Class<? extends BaseType> childClass, int argIndex) A wrapper to getTypeArgument which retrieves the ith argument WARNING: This method won't work if the generic argument i in childClass is a generic, no matter if it is bound in the object assignment (see the post above for details).getTypeArguments
(Class<T> baseClass, Class<? extends T> childClass) This method has been copied from: http://www.artima.com/weblogs/viewpostP.jsp?static Class<?>
getTypeClass
(Type type) This method has been copied from: http://www.artima.com/weblogs/viewpostP.jsp?static <RT> RT
invoke
(boolean makeAccessible, Object object, String methodName, Class<?>[] paramTypes, Object... params) Default class is object.getClass().static <T,
RT> RT invoke
(boolean makeAccessible, T object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params) Invokes a method by means of reflection.static <T,
RT> RT invoke
(Object object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params) Defaults to makeAccessible = true.static <T,
RT> RT Defaults to methodClass = object.getClass() and makeAccessible = true.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getTypeClass
This method has been copied from: http://www.artima.com/weblogs/viewpostP.jsp?thread=208860 WARNING: This method won't work if type is a generic, no matter if the generic variable is bound in the object assignment (see the post above for details). If that is the case, re-declare type as abstract and use anonymous classes. Gets the underlying class for a type, or null if the type is a variable type.- Parameters:
type
- the type- Returns:
- the underlying class
-
getTypeArguments
public static <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass) This method has been copied from: http://www.artima.com/weblogs/viewpostP.jsp?thread=208860 Gets the actual type arguments a child class has used to extend a generic base class. WARNING: This method won't work if childClass is a generic, no matter if the generic variables are bound in the object assignment (see the post above for details). If that is the case, re-declare childClass as abstract and use anonymous classes.- Parameters:
baseClass
- the base classchildClass
- the child class- Returns:
- a list of the raw classes for the actual type arguments.
-
getTypeArgument
public static <ArgumentType,BaseType> Class<ArgumentType> getTypeArgument(Class<BaseType> baseClass, Class<? extends BaseType> childClass, int argIndex) A wrapper to getTypeArgument which retrieves the ith argument WARNING: This method won't work if the generic argument i in childClass is a generic, no matter if it is bound in the object assignment (see the post above for details). If that is the case, re-declare childClass as abstract and use anonymous classes.- Type Parameters:
Argument
- the type of i parameterBaseType
- the type of base class- Parameters:
baseClass
- base classchildClass
- your classargIndex
- the argument you want
-
invoke
public static <T,RT> RT invoke(boolean makeAccessible, T object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params) Invokes a method by means of reflection. This is a shortcut for the classes and methods existing in the standard reflection package.- Parameters:
makeAccessible
- if true, usesAccessibleObject.setAccessible(boolean)
to make the method accessible, even when it's protected. As Java documentation says, this will possibly trigger exceptions (wrapped by aRuntimeException
).object
- the object for which the method is invokedmethodClass
- the class where the method is declared (might be an objec't superclass and you need to identify the declaring class).methodName
- the method nameparamTypes
- the types of parameters the method accepts (it's like inClass.getDeclaredMethod(String, Class...)
.params
- the actual parameters for the invocation.- Returns:
- whatever the method returns, or null if it's void.
-
invoke
public static <RT> RT invoke(boolean makeAccessible, Object object, String methodName, Class<?>[] paramTypes, Object... params) Default class is object.getClass().NullPointerException
if object is null. -
invoke
public static <T,RT> RT invoke(Object object, Class<? super T> methodClass, String methodName, Class<?>[] paramTypes, Object... params) Defaults to makeAccessible = true. -
invoke
public static <T,RT> RT invoke(Object object, String methodName, Class<?>[] paramTypes, Object... params) Defaults to methodClass = object.getClass() and makeAccessible = true.NullPointerException
if object is null.
-