Class ReflectionUtil


  • public class ReflectionUtil
    extends java.lang.Object
    Basic reflection utilities to enhance what the JDK provides.
    Since:
    4.0
    Author:
    Manik Surtani (manik@jboss.org)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.Class<?>[] EMPTY_CLASS_ARRAY  
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String extractFieldName​(java.lang.String setterOrGetter)  
      static java.lang.reflect.Method findGetterForField​(java.lang.Class<?> c, java.lang.String fieldName)  
      static java.lang.reflect.Method findMethod​(java.lang.Class<?> type, java.lang.String methodName)  
      static java.lang.reflect.Method findMethod​(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>[] parameters)  
      static java.lang.reflect.Method findSetterForField​(java.lang.Class<?> c, java.lang.String fieldName)  
      static java.util.List<java.lang.reflect.Field> getAllFields​(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)  
      static java.util.List<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      Returns a set of Methods that contain the given method annotation.
      static java.util.List<java.lang.reflect.Method> getAllMethodsShallow​(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      Returns a set of Methods that contain the given method annotation.
      static java.util.List<java.lang.reflect.Field> getAnnotatedFields​(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)  
      static <T extends java.lang.annotation.Annotation>
      T
      getAnnotation​(java.lang.Class<?> clazz, java.lang.Class<T> ann)
      Inspects the class passed in for the class level annotation specified.
      static java.lang.Class<?> getClassForName​(java.lang.String name, java.lang.ClassLoader cl)  
      static java.lang.reflect.Field getField​(java.lang.String fieldName, java.lang.Class<?> objectClass)  
      static int getIntAccessibly​(java.lang.reflect.Field f, java.lang.Object instance)  
      static java.lang.Object getValue​(java.lang.Object instance, java.lang.String fieldName)
      Retrieves the value of a field of an object instance via reflection
      static java.lang.Object invokeAccessibly​(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object[] parameters)
      Invokes a method using reflection, in an accessible manner (by using Method.setAccessible(boolean)
      static java.lang.Object invokeMethod​(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object[] parameters)  
      static boolean isAnnotationPresent​(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
      Tests whether an annotation is present on a class.
      static void setAccessibly​(java.lang.Object instance, java.lang.reflect.Field field, java.lang.Object value)  
      static void setField​(java.lang.Object instance, java.lang.reflect.Field field, java.lang.Object value)  
      static java.lang.Class<?>[] toClassArray​(java.lang.String[] typeList, java.lang.ClassLoader classLoader)  
      static java.lang.String[] toStringArray​(java.lang.Class<?>[] classes)  
      static <T> T unwrap​(java.lang.Object obj, java.lang.Class<T> clazz)  
      static <T> T unwrapAny​(java.lang.Class<T> clazz, java.lang.Object... objs)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_CLASS_ARRAY

        public static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
    • Constructor Detail

      • ReflectionUtil

        public ReflectionUtil()
    • Method Detail

      • getAllMethods

        public static java.util.List<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> c,
                                                                             java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Returns a set of Methods that contain the given method annotation. This includes all public, protected, package and private methods, as well as those of superclasses. Note that this does *not* include overridden methods.
        Parameters:
        c - class to inspect
        annotationType - the type of annotation to look for
        Returns:
        List of Method objects that require injection.
      • getAllMethodsShallow

        public static java.util.List<java.lang.reflect.Method> getAllMethodsShallow​(java.lang.Class<?> c,
                                                                                    java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
        Returns a set of Methods that contain the given method annotation. This includes all public, protected, package and private methods, but not those of superclasses and interfaces.
        Parameters:
        c - class to inspect
        annotationType - the type of annotation to look for
        Returns:
        List of Method objects that require injection.
      • getAllFields

        public static java.util.List<java.lang.reflect.Field> getAllFields​(java.lang.Class<?> c,
                                                                           java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      • getAnnotatedFields

        public static java.util.List<java.lang.reflect.Field> getAnnotatedFields​(java.lang.Class<?> c,
                                                                                 java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class<?> type,
                                                          java.lang.String methodName)
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class<?> type,
                                                          java.lang.String methodName,
                                                          java.lang.Class<?>[] parameters)
      • invokeAccessibly

        public static java.lang.Object invokeAccessibly​(java.lang.Object instance,
                                                        java.lang.reflect.Method method,
                                                        java.lang.Object[] parameters)
        Invokes a method using reflection, in an accessible manner (by using Method.setAccessible(boolean)
        Parameters:
        instance - instance on which to execute the method
        method - method to execute
        parameters - parameters
      • invokeMethod

        public static java.lang.Object invokeMethod​(java.lang.Object instance,
                                                    java.lang.reflect.Method method,
                                                    java.lang.Object[] parameters)
      • setAccessibly

        public static void setAccessibly​(java.lang.Object instance,
                                         java.lang.reflect.Field field,
                                         java.lang.Object value)
      • setField

        public static void setField​(java.lang.Object instance,
                                    java.lang.reflect.Field field,
                                    java.lang.Object value)
      • findGetterForField

        public static java.lang.reflect.Method findGetterForField​(java.lang.Class<?> c,
                                                                  java.lang.String fieldName)
      • findSetterForField

        public static java.lang.reflect.Method findSetterForField​(java.lang.Class<?> c,
                                                                  java.lang.String fieldName)
      • extractFieldName

        public static java.lang.String extractFieldName​(java.lang.String setterOrGetter)
      • getValue

        public static java.lang.Object getValue​(java.lang.Object instance,
                                                java.lang.String fieldName)
        Retrieves the value of a field of an object instance via reflection
        Parameters:
        instance - to inspect
        fieldName - name of field to retrieve
        Returns:
        a value
      • getAnnotation

        public static <T extends java.lang.annotation.Annotation> T getAnnotation​(java.lang.Class<?> clazz,
                                                                                  java.lang.Class<T> ann)
        Inspects the class passed in for the class level annotation specified. If the annotation is not available, this method recursively inspects superclasses and interfaces until it finds the required annotation.

        Returns null if the annotation cannot be found.

        Parameters:
        clazz - class to inspect
        ann - annotation to search for. Must be a class-level annotation.
        Returns:
        the annotation instance, or null
      • isAnnotationPresent

        public static boolean isAnnotationPresent​(java.lang.Class<?> clazz,
                                                  java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Tests whether an annotation is present on a class. The order tested is:
        • The class itself
        • All implemented interfaces
        • Any superclasses
        Parameters:
        clazz - class to test
        annotation - annotation to look for
        Returns:
        true if the annotation is found, false otherwise
      • toClassArray

        public static java.lang.Class<?>[] toClassArray​(java.lang.String[] typeList,
                                                        java.lang.ClassLoader classLoader)
                                                 throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException
      • getClassForName

        public static java.lang.Class<?> getClassForName​(java.lang.String name,
                                                         java.lang.ClassLoader cl)
                                                  throws java.lang.ClassNotFoundException
        Throws:
        java.lang.ClassNotFoundException
      • toStringArray

        public static java.lang.String[] toStringArray​(java.lang.Class<?>[] classes)
      • getField

        public static java.lang.reflect.Field getField​(java.lang.String fieldName,
                                                       java.lang.Class<?> objectClass)
      • unwrap

        public static <T> T unwrap​(java.lang.Object obj,
                                   java.lang.Class<T> clazz)
      • unwrapAny

        public static <T> T unwrapAny​(java.lang.Class<T> clazz,
                                      java.lang.Object... objs)
      • getIntAccessibly

        public static int getIntAccessibly​(java.lang.reflect.Field f,
                                           java.lang.Object instance)