Class ReflectionUtils

java.lang.Object
uk.ac.ebi.utils.reflection.ReflectionUtils

public class ReflectionUtils extends Object
Jan 15, 2008
Author:
brandizi
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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).
    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?
    static Class<?>
    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
    invoke(Object object, String methodName, Class<?>[] paramTypes, Object... params)
    Defaults to methodClass = object.getClass() and makeAccessible = true.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getTypeClass

      public static Class<?> getTypeClass(Type type)
      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 class
      childClass - 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 parameter
      BaseType - the type of base class
      Parameters:
      baseClass - base class
      childClass - your class
      argIndex - 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, uses AccessibleObject.setAccessible(boolean) to make the method accessible, even when it's protected. As Java documentation says, this will possibly trigger exceptions (wrapped by a RuntimeException).
      object - the object for which the method is invoked
      methodClass - the class where the method is declared (might be an objec't superclass and you need to identify the declaring class).
      methodName - the method name
      paramTypes - the types of parameters the method accepts (it's like in Class.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.