Class ReflectionUtils


  • @Internal
    public class ReflectionUtils
    extends java.lang.Object
    Utility methods for reflection related tasks. Micronaut tries to avoid using reflection wherever possible, this class is therefore considered an internal class and covers edge cases needed by Micronaut, often at compile time. Do not use in application code.
    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.Class[] EMPTY_CLASS_ARRAY
      Constant for empty class array.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.Optional<java.lang.reflect.Constructor<T>> findConstructor​(java.lang.Class<T> type, java.lang.Class... argTypes)
      Obtains a declared method.
      static java.util.Optional<java.lang.reflect.Field> findDeclaredField​(java.lang.Class type, java.lang.String name)  
      static java.util.Optional<java.lang.reflect.Field> findField​(java.lang.Class type, java.lang.String name)
      Finds a field in the type or super type.
      static java.util.Optional<java.lang.reflect.Method> findMethod​(java.lang.Class type, java.lang.String name, java.lang.Class... argumentTypes)
      Finds a method on the given type for the given name.
      static java.util.stream.Stream<java.lang.reflect.Method> findMethodsByName​(java.lang.Class type, java.lang.String name)
      Finds a method on the given type for the given name.
      static java.util.Set<java.lang.Class> getAllInterfaces​(java.lang.Class<?> aClass)  
      static java.util.Optional<java.lang.reflect.Method> getDeclaredMethod​(java.lang.Class type, java.lang.String methodName, java.lang.Class... argTypes)
      Obtains a declared method.
      static java.lang.Object getField​(java.lang.Class clazz, java.lang.String fieldName, java.lang.Object instance)
      Gets the value of the given field reflectively.
      static java.util.Optional<java.lang.reflect.Method> getMethod​(java.lang.Class type, java.lang.String methodName, java.lang.Class... argTypes)
      Obtains a method.
      static java.lang.Class getPrimitiveType​(java.lang.Class wrapperType)
      Obtain the primitive type for the given wrapper type.
      static java.lang.reflect.Field getRequiredField​(java.lang.Class type, java.lang.String name)
      Finds a field on the given type for the given name.
      static <T> java.lang.reflect.Constructor<T> getRequiredInternalConstructor​(java.lang.Class<T> type, java.lang.Class... argumentTypes)
      Finds an internal constructor defined by the Micronaut API and throws a NoSuchMethodError if it doesn't exist.
      static java.lang.reflect.Method getRequiredInternalMethod​(java.lang.Class type, java.lang.String name, java.lang.Class... argumentTypes)
      Finds an internal method defined by the Micronaut API and throws a NoSuchMethodError if it doesn't exist.
      static java.lang.reflect.Method getRequiredMethod​(java.lang.Class type, java.lang.String name, java.lang.Class... argumentTypes)
      Finds a method on the given type for the given name.
      static java.lang.Class getWrapperType​(java.lang.Class primitiveType)
      Obtain the wrapper type for the given primitive.
      static <R,​T>
      R
      invokeMethod​(T instance, java.lang.reflect.Method method, java.lang.Object... arguments)
      Invokes a method.
      static boolean isSetter​(java.lang.String name, java.lang.Class[] args)
      Is the method a setter.
      static java.lang.NoSuchMethodError newNoSuchMethodError​(java.lang.Class declaringType, java.lang.String name, java.lang.Class[] argumentTypes)  
      protected static java.util.Set<java.lang.Class> populateInterfaces​(java.lang.Class<?> aClass, java.util.Set<java.lang.Class> interfaces)  
      static void setField​(java.lang.reflect.Field field, java.lang.Object instance, java.lang.Object value)
      Sets the value of the given field reflectively.
      • 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
        Constant for empty class array.
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • isSetter

        public static boolean isSetter​(java.lang.String name,
                                       java.lang.Class[] args)
        Is the method a setter.
        Parameters:
        name - The method name
        args - The arguments
        Returns:
        True if it is
      • getWrapperType

        public static java.lang.Class getWrapperType​(java.lang.Class primitiveType)
        Obtain the wrapper type for the given primitive.
        Parameters:
        primitiveType - The primitive type
        Returns:
        The wrapper type
      • getPrimitiveType

        public static java.lang.Class getPrimitiveType​(java.lang.Class wrapperType)
        Obtain the primitive type for the given wrapper type.
        Parameters:
        wrapperType - The primitive type
        Returns:
        The wrapper type
      • getDeclaredMethod

        public static java.util.Optional<java.lang.reflect.Method> getDeclaredMethod​(java.lang.Class type,
                                                                                     java.lang.String methodName,
                                                                                     java.lang.Class... argTypes)
        Obtains a declared method.
        Parameters:
        type - The type
        methodName - The method name
        argTypes - The argument types
        Returns:
        The method
      • getMethod

        public static java.util.Optional<java.lang.reflect.Method> getMethod​(java.lang.Class type,
                                                                             java.lang.String methodName,
                                                                             java.lang.Class... argTypes)
        Obtains a method.
        Parameters:
        type - The type
        methodName - The method name
        argTypes - The argument types
        Returns:
        An optional Method
      • findConstructor

        public static <T> java.util.Optional<java.lang.reflect.Constructor<T>> findConstructor​(java.lang.Class<T> type,
                                                                                               java.lang.Class... argTypes)
        Obtains a declared method.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        argTypes - The argument types
        Returns:
        The method
      • invokeMethod

        public static <R,​T> R invokeMethod​(T instance,
                                                 java.lang.reflect.Method method,
                                                 java.lang.Object... arguments)
        Invokes a method.
        Type Parameters:
        R - The return type
        T - The instance type
        Parameters:
        instance - The instance
        method - The method
        arguments - The arguments
        Returns:
        The result
      • findMethod

        @Internal
        public static java.util.Optional<java.lang.reflect.Method> findMethod​(java.lang.Class type,
                                                                              java.lang.String name,
                                                                              java.lang.Class... argumentTypes)
        Finds a method on the given type for the given name.
        Parameters:
        type - The type
        name - The name
        argumentTypes - The argument types
        Returns:
        An Optional contains the method or empty
      • getRequiredMethod

        @Internal
        public static java.lang.reflect.Method getRequiredMethod​(java.lang.Class type,
                                                                 java.lang.String name,
                                                                 java.lang.Class... argumentTypes)
        Finds a method on the given type for the given name.
        Parameters:
        type - The type
        name - The name
        argumentTypes - The argument types
        Returns:
        An Optional contains the method or empty
      • getRequiredInternalMethod

        @Internal
        public static java.lang.reflect.Method getRequiredInternalMethod​(java.lang.Class type,
                                                                         java.lang.String name,
                                                                         java.lang.Class... argumentTypes)
        Finds an internal method defined by the Micronaut API and throws a NoSuchMethodError if it doesn't exist.
        Parameters:
        type - The type
        name - The name
        argumentTypes - The argument types
        Returns:
        An Optional contains the method or empty
        Throws:
        java.lang.NoSuchMethodError - If the method doesn't exist
      • getRequiredInternalConstructor

        @Internal
        public static <T> java.lang.reflect.Constructor<T> getRequiredInternalConstructor​(java.lang.Class<T> type,
                                                                                          java.lang.Class... argumentTypes)
        Finds an internal constructor defined by the Micronaut API and throws a NoSuchMethodError if it doesn't exist.
        Type Parameters:
        T - The type
        Parameters:
        type - The type
        argumentTypes - The argument types
        Returns:
        An Optional contains the method or empty
        Throws:
        java.lang.NoSuchMethodError - If the method doesn't exist
      • getRequiredField

        @Internal
        public static java.lang.reflect.Field getRequiredField​(java.lang.Class type,
                                                               java.lang.String name)
        Finds a field on the given type for the given name.
        Parameters:
        type - The type
        name - The name
        Returns:
        An Optional contains the method or empty
      • findField

        @Internal
        public static java.util.Optional<java.lang.reflect.Field> findField​(java.lang.Class type,
                                                                            java.lang.String name)
        Finds a field in the type or super type.
        Parameters:
        type - The type
        name - The field name
        Returns:
        An Optional of field
      • findMethodsByName

        public static java.util.stream.Stream<java.lang.reflect.Method> findMethodsByName​(java.lang.Class type,
                                                                                          java.lang.String name)
        Finds a method on the given type for the given name.
        Parameters:
        type - The type
        name - The name
        Returns:
        An Optional contains the method or empty
      • findDeclaredField

        public static java.util.Optional<java.lang.reflect.Field> findDeclaredField​(java.lang.Class type,
                                                                                    java.lang.String name)
        Parameters:
        type - The type
        name - The field name
        Returns:
        An optional with the declared field
      • getAllInterfaces

        public static java.util.Set<java.lang.Class> getAllInterfaces​(java.lang.Class<?> aClass)
        Parameters:
        aClass - A class
        Returns:
        All the interfaces
      • populateInterfaces

        protected static java.util.Set<java.lang.Class> populateInterfaces​(java.lang.Class<?> aClass,
                                                                           java.util.Set<java.lang.Class> interfaces)
        Parameters:
        aClass - A class
        interfaces - The interfaces
        Returns:
        A set with the interfaces
      • newNoSuchMethodError

        public static java.lang.NoSuchMethodError newNoSuchMethodError​(java.lang.Class declaringType,
                                                                       java.lang.String name,
                                                                       java.lang.Class[] argumentTypes)
        Parameters:
        declaringType - The declaring type
        name - The method name
        argumentTypes - The argument types
        Returns:
        A NoSuchMethodError
      • setField

        public static void setField​(@NonNull
                                    java.lang.reflect.Field field,
                                    @NonNull
                                    java.lang.Object instance,
                                    @Nullable
                                    java.lang.Object value)
        Sets the value of the given field reflectively.
        Parameters:
        field - The field
        instance - The instance
        value - The value
      • getField

        public static java.lang.Object getField​(@NonNull
                                                java.lang.Class clazz,
                                                @NonNull
                                                java.lang.String fieldName,
                                                @NonNull
                                                java.lang.Object instance)
        Gets the value of the given field reflectively.
        Parameters:
        clazz - The class
        fieldName - The fieldName
        instance - The instance
        Returns:
        field value of instance
        Since:
        3.7.0