Class ReflectionUtils

java.lang.Object
io.micronaut.core.reflect.ReflectionUtils

@Internal public class ReflectionUtils extends 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
Author:
Graeme Rocher
  • Field Details

    • EMPTY_CLASS_ARRAY

      public static final Class<?>[] EMPTY_CLASS_ARRAY
      Constant for empty class array.
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • isSetter

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

      public static Class<?> getWrapperType(Class<?> primitiveType)
      Obtain the wrapper type for the given primitive.
      Parameters:
      primitiveType - The primitive type
      Returns:
      The wrapper type
    • getPrimitiveType

      public static Class<?> getPrimitiveType(Class<?> wrapperType)
      Obtain the primitive type for the given wrapper type.
      Parameters:
      wrapperType - The primitive type
      Returns:
      The wrapper type
    • getDeclaredMethod

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

      public static Optional<Method> getMethod(Class<?> type, String methodName, 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> Optional<Constructor<T>> findConstructor(Class<T> type, 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, Method method, 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 Optional<Method> findMethod(Class<?> type, String name, 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 Method getRequiredMethod(Class<?> type, String name, 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 Method getRequiredInternalMethod(Class<?> type, String name, 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:
      NoSuchMethodError - If the method doesn't exist
    • getRequiredInternalConstructor

      @Internal public static <T> Constructor<T> getRequiredInternalConstructor(Class<T> type, 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:
      NoSuchMethodError - If the method doesn't exist
    • getRequiredField

      @Internal public static Field getRequiredField(Class<?> type, 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
    • getFieldValue

      @Internal public static Optional<Object> getFieldValue(@NonNull @NonNull Class<?> fieldOwnerClass, @NonNull @NonNull String fieldName, @NonNull @NonNull Object instance)
      Finds field's value or return an empty if exception occurs or if the value is null.
      Parameters:
      fieldOwnerClass - The field owner class
      fieldName - The field name
      instance - The instance
      Returns:
      An Optional contains the value or empty of the value is null or an error occurred
      Since:
      4.0.0
    • findField

      @Internal public static Optional<Field> findField(Class<?> type, 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 Stream<Method> findMethodsByName(Class<?> type, 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 Optional<Field> findDeclaredField(Class<?> type, String name)
      Parameters:
      type - The type
      name - The field name
      Returns:
      An optional with the declared field
    • getAllInterfaces

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

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

      public static NoSuchMethodError newNoSuchMethodError(Class<?> declaringType, String name, Class<?>[] argumentTypes)
      Parameters:
      declaringType - The declaring type
      name - The method name
      argumentTypes - The argument types
      Returns:
      A NoSuchMethodError
    • setField

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

      public static Object getField(@NonNull @NonNull Class<?> clazz, @NonNull @NonNull String fieldName, @NonNull @NonNull 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
    • setField

      public static void setField(@NonNull @NonNull Class<?> clazz, @NonNull @NonNull String fieldName, @NonNull @NonNull Object instance, @Nullable @Nullable Object value)
      Sets the value of the given field reflectively.
      Parameters:
      clazz - The class
      fieldName - The fieldName
      instance - The instance
      value - The value
      Since:
      4.0.0