Package io.micronaut.core.reflect
Class ReflectionUtils
java.lang.Object
io.micronaut.core.reflect.ReflectionUtils
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 SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <T> Optional<Constructor<T>>findConstructor(Class<T> type, Class<?>... argTypes) Obtains a declared method.findDeclaredField(Class<?> type, String name) Finds a field in the type or super type.findMethod(Class<?> type, String name, Class<?>... argumentTypes) Finds a method on the given type for the given name.findMethodsByName(Class<?> type, String name) Finds a method on the given type for the given name.getAllInterfaces(Class<?> aClass) getDeclaredMethod(Class<?> type, String methodName, Class<?>... argTypes) Obtains a declared method.static ObjectGets the value of the given field reflectively.getFieldValue(@NonNull Class<?> fieldOwnerClass, @NonNull String fieldName, @NonNull Object instance) Finds field's value or return an empty if exception occurs or if the value is null.Obtains a method.static Class<?>getPrimitiveType(Class<?> wrapperType) Obtain the primitive type for the given wrapper type.static FieldgetRequiredField(Class<?> type, String name) Finds a field on the given type for the given name.static <T> Constructor<T>getRequiredInternalConstructor(Class<T> type, Class<?>... argumentTypes) Finds an internal constructor defined by the Micronaut API and throws aNoSuchMethodErrorif it doesn't exist.static MethodgetRequiredInternalMethod(Class<?> type, String name, Class<?>... argumentTypes) Finds an internal method defined by the Micronaut API and throws aNoSuchMethodErrorif it doesn't exist.static MethodgetRequiredMethod(Class<?> type, String name, Class<?>... argumentTypes) Finds a method on the given type for the given name.static Class<?>getWrapperType(Class<?> primitiveType) Obtain the wrapper type for the given primitive.static <R,T> R invokeInaccessibleMethod(T instance, Method method, Object... arguments) Invokes an inaccessible method.static <R,T> R invokeMethod(T instance, Method method, Object... arguments) Invokes a method.static booleanIs the method a setter.static NoSuchMethodErrornewNoSuchMethodError(Class<?> declaringType, String name, Class<?>[] argumentTypes) populateInterfaces(Class<?> aClass, Set<Class<?>> interfaces) static voidsetField(@NonNull Class<?> clazz, @NonNull String fieldName, @NonNull Object instance, @Nullable Object value) Sets the value of the given field reflectively.static voidSets the value of the given field reflectively.
- 
Field Details- 
EMPTY_CLASS_ARRAYConstant for empty class array.
 
- 
- 
Constructor Details- 
ReflectionUtilspublic ReflectionUtils()
 
- 
- 
Method Details- 
isSetterIs the method a setter.- Parameters:
- name- The method name
- args- The arguments
- Returns:
- True if it is
 
- 
getWrapperTypeObtain the wrapper type for the given primitive.- Parameters:
- primitiveType- The primitive type
- Returns:
- The wrapper type
 
- 
getPrimitiveTypeObtain the primitive type for the given wrapper type.- Parameters:
- wrapperType- The primitive type
- Returns:
- The wrapper type
 
- 
getDeclaredMethodpublic 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
 
- 
getMethodObtains a method.- Parameters:
- type- The type
- methodName- The method name
- argTypes- The argument types
- Returns:
- An optional Method
 
- 
findConstructorObtains a declared method.- Type Parameters:
- T- The generic type
- Parameters:
- type- The type
- argTypes- The argument types
- Returns:
- The method
 
- 
invokeMethodInvokes 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
 
- 
invokeInaccessibleMethodInvokes an inaccessible method.- Type Parameters:
- R- The return type
- T- The instance type
- Parameters:
- instance- The instance
- method- The method
- arguments- The arguments
- Returns:
- The result
- Since:
- 4.8
 
- 
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 Optionalcontains 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 Optionalcontains 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 aNoSuchMethodErrorif it doesn't exist.- Parameters:
- type- The type
- name- The name
- argumentTypes- The argument types
- Returns:
- An Optionalcontains 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 aNoSuchMethodErrorif it doesn't exist.- Type Parameters:
- T- The type
- Parameters:
- type- The type
- argumentTypes- The argument types
- Returns:
- An Optionalcontains the method or empty
- Throws:
- NoSuchMethodError- If the method doesn't exist
 
- 
getRequiredFieldFinds a field on the given type for the given name.- Parameters:
- type- The type
- name- The name
- Returns:
- An Optionalcontains 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 Optionalcontains the value or empty of the value is null or an error occurred
- Since:
- 4.0.0
 
- 
findFieldFinds a field in the type or super type.- Parameters:
- type- The type
- name- The field name
- Returns:
- An Optionalof field
 
- 
findMethodsByNameFinds a method on the given type for the given name.- Parameters:
- type- The type
- name- The name
- Returns:
- An Optionalcontains the method or empty
 
- 
findDeclaredField- Parameters:
- type- The type
- name- The field name
- Returns:
- An optional with the declared field
 
- 
getAllInterfaces- Parameters:
- aClass- A class
- Returns:
- All the interfaces
 
- 
populateInterfaces- Parameters:
- aClass- A class
- interfaces- The interfaces
- Returns:
- A set with the interfaces
 
- 
newNoSuchMethodErrorpublic 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
 
- 
setFieldpublic 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
 
- 
getFieldpublic 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
 
- 
setFieldpublic 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
 
 
-