Class ReflectionUtils


  • public final class ReflectionUtils
    extends java.lang.Object
    ReflectionUtils - Reflection utilities holder.
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String GETTER_PREFIX  
      private static java.lang.String SETTER_PREFIX  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ReflectionUtils()
      Prevent any instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.lang.Class<T> getClass​(java.lang.String className)
      Get the Java Class with the given fully-qualified name.
      static <T> java.lang.Class<T> getClassOrNull​(java.lang.String className)
      Get the Java Class with the given fully-qualified name or or null if no Class was found matching the provided name.
      static java.lang.reflect.Method getDeclaredMethodOrNull​(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Class, excluding inherited ones, or null if no Method was found.
      static java.lang.reflect.Field getField​(java.lang.Class targetClass, java.lang.String fieldName)
      Get the Field with the given name belonging to the provided Java Class.
      static java.lang.reflect.Field getFieldOrNull​(java.lang.Class targetClass, java.lang.String fieldName)
      Get the Field with the given name belonging to the provided Java Class or null if no Field was found.
      static <T> T getFieldValue​(java.lang.Object target, java.lang.String fieldName)
      Get the value of the field matching the given name and belonging to target Object.
      static <T> T getFieldValueOrNull​(java.lang.Object target, java.lang.String fieldName)
      Get the value of the field matching the given name and belonging to target Object or null if no Field was found..
      static <T> java.lang.Class<T> getFirstSuperClassFromPackage​(java.lang.Class clazz, java.lang.String packageName)
      Get the first super class matching the provided package name.
      static java.util.Set<java.lang.Class> getGenericTypes​(java.lang.reflect.ParameterizedType parameterizedType)
      Get the generic types of a given Class.
      static java.lang.reflect.Method getGetter​(java.lang.Object target, java.lang.String propertyName)
      Get the property getter Method with the given name belonging to the provided Java Object.
      static java.lang.reflect.Type getMemberGenericTypeOrNull​(java.lang.Class targetClass, java.lang.String memberName)
      Get the generic Type of the Member with the given name belonging to the provided Java Class or null if no Member was found.
      static java.lang.reflect.Member getMemberOrNull​(java.lang.Class targetClass, java.lang.String memberName)
      Get the Member with the given name belonging to the provided Java Class or null if no Member was found.
      static java.lang.reflect.Method getMethod​(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Class.
      static java.lang.reflect.Method getMethod​(java.lang.Object target, java.lang.String methodName, java.lang.Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Object.
      static java.lang.reflect.Method getMethodOrNull​(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Object or null if no Method was found.
      static java.lang.reflect.Method getMethodOrNull​(java.lang.Object target, java.lang.String methodName, java.lang.Class... parameterTypes)
      Get the Method with the given signature (name and parameter types) belonging to the provided Java Object or null if no Method was found.
      static java.lang.reflect.Method getSetter​(java.lang.Object target, java.lang.String propertyName, java.lang.Class<?> parameterType)
      Get the property setter Method with the given signature (name and parameter types) belonging to the provided Java Object.
      static java.lang.Class<?> getWrapperClass​(java.lang.Class<?> clazz)
      Get the Java Wrapper Class associated to the given primitive type.
      private static java.lang.IllegalArgumentException handleException​(java.lang.ClassNotFoundException e)
      Handle the ClassNotFoundException by rethrowing it as an IllegalArgumentException.
      private static java.lang.IllegalArgumentException handleException​(java.lang.IllegalAccessException e)
      Handle the IllegalAccessException by rethrowing it as an IllegalArgumentException.
      private static java.lang.IllegalArgumentException handleException​(java.lang.InstantiationException e)
      Handle the InstantiationException by rethrowing it as an IllegalArgumentException.
      private static java.lang.IllegalArgumentException handleException​(java.lang.NoSuchFieldException e)
      Handle the NoSuchFieldException by rethrowing it as an IllegalArgumentException.
      private static java.lang.IllegalArgumentException handleException​(java.lang.NoSuchMethodException e)
      Handle the NoSuchMethodException by rethrowing it as an IllegalArgumentException.
      private static java.lang.IllegalArgumentException handleException​(java.lang.reflect.InvocationTargetException e)
      Handle the InvocationTargetException by rethrowing it as an IllegalArgumentException.
      static boolean hasMethod​(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class... parameterTypes)
      Check if the provided Java Class contains a method matching the given signature (name and parameter types).
      static <T> T invokeGetter​(java.lang.Object target, java.lang.String propertyName)
      Invoke the property getter with the provided name on the given Java Object.
      static <T> T invokeMethod​(java.lang.Object target, java.lang.reflect.Method method, java.lang.Object... parameters)
      Invoke the provided Method on the given Java Object.
      static <T> T invokeMethod​(java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
      Invoke the method with the provided signature (name and parameter types) on the given Java Object.
      static void invokeSetter​(java.lang.Object target, java.lang.String propertyName, boolean parameter)
      Invoke the boolean property setter with the provided name on the given Java Object.
      static void invokeSetter​(java.lang.Object target, java.lang.String propertyName, int parameter)
      Invoke the int property setter with the provided name on the given Java Object.
      static void invokeSetter​(java.lang.Object target, java.lang.String propertyName, java.lang.Object parameter)
      Invoke the property setter with the provided signature (name and parameter types) on the given Java Object.
      static <T> T invokeStaticMethod​(java.lang.reflect.Method method, java.lang.Object... parameters)
      Invoke the static Method with the provided parameters.
      static <T> T newInstance​(java.lang.Class clazz)
      Instantiate a new Object of the provided type.
      static <T> T newInstance​(java.lang.Class clazz, java.lang.Object[] args, java.lang.Class[] argsTypes)
      Instantiate a new Object of the provided type.
      static <T> T newInstance​(java.lang.String className)
      Instantiate a new Object of the provided type.
      static void setFieldValue​(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
      Set the value of the field matching the given name and belonging to target Object.
      • Methods inherited from class java.lang.Object

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

      • ReflectionUtils

        private ReflectionUtils()
        Prevent any instantiation.
    • Method Detail

      • newInstance

        public static <T> T newInstance​(java.lang.String className)
        Instantiate a new Object of the provided type.
        Type Parameters:
        T - class type
        Parameters:
        className - The fully-qualified Java class name of the Object to instantiate
        Returns:
        new Java Object of the provided type
      • newInstance

        public static <T> T newInstance​(java.lang.Class clazz)
        Instantiate a new Object of the provided type.
        Type Parameters:
        T - class type
        Parameters:
        clazz - The Java Class of the Object to instantiate
        Returns:
        new Java Object of the provided type
      • newInstance

        public static <T> T newInstance​(java.lang.Class clazz,
                                        java.lang.Object[] args,
                                        java.lang.Class[] argsTypes)
        Instantiate a new Object of the provided type.
        Type Parameters:
        T - class type
        Parameters:
        clazz - The Java Class of the Object to instantiate
        args - The arguments that need to be passed to the constructor
        argsTypes - The argument types that need to be passed to the constructor
        Returns:
        new Java Object of the provided type
      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class targetClass,
                                                       java.lang.String fieldName)
        Get the Field with the given name belonging to the provided Java Class.
        Parameters:
        targetClass - the provided Java Class the field belongs to
        fieldName - the Field name
        Returns:
        the Field matching the given name
      • getFieldOrNull

        public static java.lang.reflect.Field getFieldOrNull​(java.lang.Class targetClass,
                                                             java.lang.String fieldName)
        Get the Field with the given name belonging to the provided Java Class or null if no Field was found.
        Parameters:
        targetClass - the provided Java Class the field belongs to
        fieldName - the Field name
        Returns:
        the Field matching the given name or null
      • getFieldValue

        public static <T> T getFieldValue​(java.lang.Object target,
                                          java.lang.String fieldName)
        Get the value of the field matching the given name and belonging to target Object.
        Type Parameters:
        T - field type
        Parameters:
        target - target Object whose field we are retrieving the value from
        fieldName - field name
        Returns:
        field value
      • getFieldValueOrNull

        public static <T> T getFieldValueOrNull​(java.lang.Object target,
                                                java.lang.String fieldName)
        Get the value of the field matching the given name and belonging to target Object or null if no Field was found..
        Type Parameters:
        T - field type
        Parameters:
        target - target Object whose field we are retrieving the value from
        fieldName - field name
        Returns:
        field value matching the given name or null
      • setFieldValue

        public static void setFieldValue​(java.lang.Object target,
                                         java.lang.String fieldName,
                                         java.lang.Object value)
        Set the value of the field matching the given name and belonging to target Object.
        Parameters:
        target - target object
        fieldName - field name
        value - field value
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Object target,
                                                         java.lang.String methodName,
                                                         java.lang.Class... parameterTypes)
        Get the Method with the given signature (name and parameter types) belonging to the provided Java Object.
        Parameters:
        target - target Object
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        return Method matching the provided signature
      • getMethodOrNull

        public static java.lang.reflect.Method getMethodOrNull​(java.lang.Object target,
                                                               java.lang.String methodName,
                                                               java.lang.Class... parameterTypes)
        Get the Method with the given signature (name and parameter types) belonging to the provided Java Object or null if no Method was found.
        Parameters:
        target - target Object
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        return Method matching the provided signature or null
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class targetClass,
                                                         java.lang.String methodName,
                                                         java.lang.Class... parameterTypes)
        Get the Method with the given signature (name and parameter types) belonging to the provided Java Class.
        Parameters:
        targetClass - target Class
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        the Method matching the provided signature
      • getMethodOrNull

        public static java.lang.reflect.Method getMethodOrNull​(java.lang.Class targetClass,
                                                               java.lang.String methodName,
                                                               java.lang.Class... parameterTypes)
        Get the Method with the given signature (name and parameter types) belonging to the provided Java Object or null if no Method was found.
        Parameters:
        targetClass - target Class
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        return Method matching the provided signature or null
      • getDeclaredMethodOrNull

        public static java.lang.reflect.Method getDeclaredMethodOrNull​(java.lang.Class targetClass,
                                                                       java.lang.String methodName,
                                                                       java.lang.Class... parameterTypes)
        Get the Method with the given signature (name and parameter types) belonging to the provided Java Class, excluding inherited ones, or null if no Method was found.
        Parameters:
        targetClass - target Class
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        return Method matching the provided signature or null
      • hasMethod

        public static boolean hasMethod​(java.lang.Class<?> targetClass,
                                        java.lang.String methodName,
                                        java.lang.Class... parameterTypes)
        Check if the provided Java Class contains a method matching the given signature (name and parameter types).
        Parameters:
        targetClass - target Class
        methodName - method name
        parameterTypes - method parameter types
        Returns:
        the provided Java Class contains a method with the given signature
      • getSetter

        public static java.lang.reflect.Method getSetter​(java.lang.Object target,
                                                         java.lang.String propertyName,
                                                         java.lang.Class<?> parameterType)
        Get the property setter Method with the given signature (name and parameter types) belonging to the provided Java Object.
        Parameters:
        target - target Object
        propertyName - property name
        parameterType - setter property type
        Returns:
        the setter Method matching the provided signature
      • getGetter

        public static java.lang.reflect.Method getGetter​(java.lang.Object target,
                                                         java.lang.String propertyName)
        Get the property getter Method with the given name belonging to the provided Java Object.
        Parameters:
        target - target Object
        propertyName - property name
        Returns:
        the getter Method matching the provided name
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object target,
                                         java.lang.reflect.Method method,
                                         java.lang.Object... parameters)
        Invoke the provided Method on the given Java Object.
        Type Parameters:
        T - return value object type
        Parameters:
        target - target Object whose method we are invoking
        method - method to invoke
        parameters - parameters passed to the method call
        Returns:
        the value return by the Method invocation
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object target,
                                         java.lang.String methodName,
                                         java.lang.Object... parameters)
        Invoke the method with the provided signature (name and parameter types) on the given Java Object.
        Type Parameters:
        T - return value object type
        Parameters:
        target - target Object whose method we are invoking
        methodName - method name to invoke
        parameters - parameters passed to the method call
        Returns:
        the value return by the method invocation
      • invokeGetter

        public static <T> T invokeGetter​(java.lang.Object target,
                                         java.lang.String propertyName)
        Invoke the property getter with the provided name on the given Java Object.
        Type Parameters:
        T - return value object type
        Parameters:
        target - target Object whose property getter we are invoking
        propertyName - property name whose getter we are invoking
        Returns:
        the value return by the getter invocation
      • invokeSetter

        public static void invokeSetter​(java.lang.Object target,
                                        java.lang.String propertyName,
                                        java.lang.Object parameter)
        Invoke the property setter with the provided signature (name and parameter types) on the given Java Object.
        Parameters:
        target - target Object whose property setter we are invoking
        propertyName - property name whose setter we are invoking
        parameter - parameter passed to the setter call
      • invokeSetter

        public static void invokeSetter​(java.lang.Object target,
                                        java.lang.String propertyName,
                                        boolean parameter)
        Invoke the boolean property setter with the provided name on the given Java Object.
        Parameters:
        target - target Object whose property setter we are invoking
        propertyName - property name whose setter we are invoking
        parameter - boolean parameter passed to the setter call
      • invokeSetter

        public static void invokeSetter​(java.lang.Object target,
                                        java.lang.String propertyName,
                                        int parameter)
        Invoke the int property setter with the provided name on the given Java Object.
        Parameters:
        target - target Object whose property setter we are invoking
        propertyName - property name whose setter we are invoking
        parameter - int parameter passed to the setter call
      • invokeStaticMethod

        public static <T> T invokeStaticMethod​(java.lang.reflect.Method method,
                                               java.lang.Object... parameters)
        Invoke the static Method with the provided parameters.
        Type Parameters:
        T - return value object type
        Parameters:
        method - target static Method to invoke
        parameters - parameters passed to the method call
        Returns:
        the value return by the method invocation
      • getClass

        public static <T> java.lang.Class<T> getClass​(java.lang.String className)
        Get the Java Class with the given fully-qualified name.
        Type Parameters:
        T - Class type
        Parameters:
        className - the Java Class name to be retrieved
        Returns:
        the Java Class object
      • getClassOrNull

        public static <T> java.lang.Class<T> getClassOrNull​(java.lang.String className)
        Get the Java Class with the given fully-qualified name or or null if no Class was found matching the provided name.
        Type Parameters:
        T - Class type
        Parameters:
        className - the Java Class name to be retrieved
        Returns:
        the Java Class object or null
      • getWrapperClass

        public static java.lang.Class<?> getWrapperClass​(java.lang.Class<?> clazz)
        Get the Java Wrapper Class associated to the given primitive type.
        Parameters:
        clazz - primitive class
        Returns:
        the Java Wrapper Class
      • getFirstSuperClassFromPackage

        public static <T> java.lang.Class<T> getFirstSuperClassFromPackage​(java.lang.Class clazz,
                                                                           java.lang.String packageName)
        Get the first super class matching the provided package name.
        Type Parameters:
        T - class generic type
        Parameters:
        clazz - Java class
        packageName - package name
        Returns:
        the first super class matching the provided package name or null.
      • getGenericTypes

        public static java.util.Set<java.lang.Class> getGenericTypes​(java.lang.reflect.ParameterizedType parameterizedType)
        Get the generic types of a given Class.
        Parameters:
        parameterizedType - parameterized Type
        Returns:
        generic types for the given Class.
      • getMemberOrNull

        public static java.lang.reflect.Member getMemberOrNull​(java.lang.Class targetClass,
                                                               java.lang.String memberName)
        Get the Member with the given name belonging to the provided Java Class or null if no Member was found.
        Parameters:
        targetClass - the provided Java Class the field or method belongs to
        memberName - the Field or Method name
        Returns:
        the Field or Method matching the given name or null
      • getMemberGenericTypeOrNull

        public static java.lang.reflect.Type getMemberGenericTypeOrNull​(java.lang.Class targetClass,
                                                                        java.lang.String memberName)
        Get the generic Type of the Member with the given name belonging to the provided Java Class or null if no Member was found.
        Parameters:
        targetClass - the provided Java Class the field or method belongs to
        memberName - the Field or Method name
        Returns:
        the generic Type of the Field or Method matching the given name or null
      • handleException

        private static java.lang.IllegalArgumentException handleException​(java.lang.NoSuchFieldException e)
        Handle the NoSuchFieldException by rethrowing it as an IllegalArgumentException.
        Parameters:
        e - the original NoSuchFieldException
        Returns:
        the IllegalArgumentException wrapping exception
      • handleException

        private static java.lang.IllegalArgumentException handleException​(java.lang.NoSuchMethodException e)
        Handle the NoSuchMethodException by rethrowing it as an IllegalArgumentException.
        Parameters:
        e - the original NoSuchMethodException
        Returns:
        the IllegalArgumentException wrapping exception
      • handleException

        private static java.lang.IllegalArgumentException handleException​(java.lang.IllegalAccessException e)
        Handle the IllegalAccessException by rethrowing it as an IllegalArgumentException.
        Parameters:
        e - the original IllegalAccessException
        Returns:
        the IllegalArgumentException wrapping exception
      • handleException

        private static java.lang.IllegalArgumentException handleException​(java.lang.reflect.InvocationTargetException e)
        Handle the InvocationTargetException by rethrowing it as an IllegalArgumentException.
        Parameters:
        e - the original InvocationTargetException
        Returns:
        the IllegalArgumentException wrapping exception
      • handleException

        private static java.lang.IllegalArgumentException handleException​(java.lang.ClassNotFoundException e)
        Handle the ClassNotFoundException by rethrowing it as an IllegalArgumentException.
        Parameters:
        e - the original ClassNotFoundException
        Returns:
        the IllegalArgumentException wrapping exception
      • handleException

        private static java.lang.IllegalArgumentException handleException​(java.lang.InstantiationException e)
        Handle the InstantiationException by rethrowing it as an IllegalArgumentException.
        Parameters:
        e - the original InstantiationException
        Returns:
        the IllegalArgumentException wrapping exception