Class ReflectionUtils


  • public final class ReflectionUtils
    extends java.lang.Object
    Utility class for performing actions requiring reflection.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.Class<? extends T> forName​(java.lang.String className, java.lang.Class<T> type)
      Lookup a class given className and type.
      static <T extends java.lang.annotation.Annotation>
      T
      getAnnotation​(java.lang.Class<?> clazz, java.lang.Class<T> annotation)
      Inspects the clazz and returns the Annotation instance if the annotation is present on the clazz.
      static java.lang.reflect.Method getGetter​(java.lang.Class<?> beanClass, java.lang.String name)
      Gets a getter method.
      static java.lang.reflect.Method getSetter​(java.lang.Class<?> beanClass, java.lang.String name, java.lang.Class<?> type)
      Gets a setter method.
      static java.lang.Object invoke​(java.lang.Class<?> beanClass, java.lang.Object bean, java.lang.String name, java.lang.Object... args)
      Invokes a method.
      static java.lang.Object invoke​(java.lang.Object bean, java.lang.String name, java.lang.Object... args)
      Invokes a method.
      static <T extends java.lang.annotation.Annotation>
      boolean
      isAnnotationPresent​(java.lang.Class<?> clazz, java.lang.Class<T> annotation)
      Inspects the clazz and returns true if the annotation is present on the clazz.
      static boolean isGetter​(java.lang.reflect.Method method)
      Gets if method is a getter method.
      static boolean isPublic​(java.lang.reflect.Method method)
      Gets if method is declared public.
      static boolean isSetter​(java.lang.reflect.Method method)
      Gets if method is a setter method.
      static boolean isSubclass​(java.lang.String className, java.lang.Class<?> type)  
      static <T> T newInstance​(java.lang.Class<? extends T> type)
      Intantiate a new instance of type.
      static java.lang.Object newInstance​(java.lang.String className)
      Instantiate a class by name.
      • Methods inherited from class java.lang.Object

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

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • isPublic

        public static boolean isPublic​(java.lang.reflect.Method method)
        Gets if method is declared public.
      • isGetter

        public static boolean isGetter​(java.lang.reflect.Method method)
        Gets if method is a getter method.
      • isSetter

        public static boolean isSetter​(java.lang.reflect.Method method)
        Gets if method is a setter method.
      • getGetter

        public static java.lang.reflect.Method getGetter​(java.lang.Class<?> beanClass,
                                                         java.lang.String name)
        Gets a getter method.
        Parameters:
        beanClass - the beanClass
        name - the getter name
        Returns:
        the getter Method (or null if not found)
      • getSetter

        public static java.lang.reflect.Method getSetter​(java.lang.Class<?> beanClass,
                                                         java.lang.String name,
                                                         java.lang.Class<?> type)
        Gets a setter method.
        Parameters:
        beanClass - the bean class
        name - the setter name
        type - the type
        Returns:
        the setter Method (or null if not found)
      • forName

        public static <T> java.lang.Class<? extends T> forName​(java.lang.String className,
                                                               java.lang.Class<T> type)
                                                        throws AttivioException
        Lookup a class given className and type.
        Throws:
        AttivioException - if the class is not found or if the type of the class is not a subclass of type
      • isSubclass

        public static boolean isSubclass​(java.lang.String className,
                                         java.lang.Class<?> type)
                                  throws AttivioException
        Parameters:
        className -
        type -
        Returns:
        true if className is a subclass or implementation of type (which can be an interface).
        Throws:
        AttivioException - if className could not be found
      • newInstance

        public static <T> T newInstance​(java.lang.Class<? extends T> type)
                                 throws AttivioException
        Intantiate a new instance of type.
        Throws:
        AttivioException
      • newInstance

        public static java.lang.Object newInstance​(java.lang.String className)
                                            throws AttivioException
        Instantiate a class by name.
        Throws:
        AttivioException
      • invoke

        public static java.lang.Object invoke​(java.lang.Object bean,
                                              java.lang.String name,
                                              java.lang.Object... args)
                                       throws java.lang.reflect.InvocationTargetException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.IllegalAccessException
        Invokes a method.
        Parameters:
        bean - the bean
        name - the method name
        args - the arguments
        Returns:
        result of method invokation
        Throws:
        java.lang.NoSuchMethodException - if method could not be found
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
      • invoke

        public static java.lang.Object invoke​(java.lang.Class<?> beanClass,
                                              java.lang.Object bean,
                                              java.lang.String name,
                                              java.lang.Object... args)
                                       throws java.lang.reflect.InvocationTargetException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.IllegalAccessException
        Invokes a method.
        Parameters:
        beanClass - the class for bean
        bean - the bean
        name - the method name
        args - the arguments
        Returns:
        result of method invokation
        Throws:
        java.lang.NoSuchMethodException - if method could not be found
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
      • getAnnotation

        public static <T extends java.lang.annotation.Annotation> T getAnnotation​(java.lang.Class<?> clazz,
                                                                                  java.lang.Class<T> annotation)
        Inspects the clazz and returns the Annotation instance if the annotation is present on the clazz. This method works with classes or interfaces. All implemented interfaces are checked.
        Parameters:
        clazz -
        annotation -
        Returns:
        the annotation if present on clazz, null otherwise.
      • isAnnotationPresent

        public static <T extends java.lang.annotation.Annotation> boolean isAnnotationPresent​(java.lang.Class<?> clazz,
                                                                                              java.lang.Class<T> annotation)
        Inspects the clazz and returns true if the annotation is present on the clazz. This method works with classes or interfaces. All implemented interfaces are checked.
        Parameters:
        clazz -
        annotation -
        Returns:
        true if the annotation is present on clazz, false otherwise.