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 Summary
-
Constructor Summary
-
Method Summary
Modifier 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 Object
Gets 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 Field
getRequiredField
(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 aNoSuchMethodError
if it doesn't exist.static Method
getRequiredInternalMethod
(Class<?> type, String name, Class<?>... argumentTypes) Finds an internal method defined by the Micronaut API and throws aNoSuchMethodError
if it doesn't exist.static Method
getRequiredMethod
(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 invokeMethod
(T instance, Method method, Object... arguments) Invokes a method.static boolean
Is the method a setter.static NoSuchMethodError
newNoSuchMethodError
(Class<?> declaringType, String name, Class<?>[] argumentTypes) populateInterfaces
(Class<?> aClass, Set<Class<?>> interfaces) static void
setField
(@NonNull Class<?> clazz, @NonNull String fieldName, @NonNull Object instance, @Nullable Object value) Sets the value of the given field reflectively.static void
Sets the value of the given field reflectively.
-
Field Details
-
EMPTY_CLASS_ARRAY
Constant for empty class array.
-
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
isSetter
Is the method a setter.- Parameters:
name
- The method nameargs
- The arguments- Returns:
- True if it is
-
getWrapperType
Obtain the wrapper type for the given primitive.- Parameters:
primitiveType
- The primitive type- Returns:
- The wrapper type
-
getPrimitiveType
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 typemethodName
- The method nameargTypes
- The argument types- Returns:
- The method
-
getMethod
Obtains a method.- Parameters:
type
- The typemethodName
- The method nameargTypes
- The argument types- Returns:
- An optional
Method
-
findConstructor
Obtains a declared method.- Type Parameters:
T
- The generic type- Parameters:
type
- The typeargTypes
- The argument types- Returns:
- The method
-
invokeMethod
Invokes a method.- Type Parameters:
R
- The return typeT
- The instance type- Parameters:
instance
- The instancemethod
- The methodarguments
- 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 typename
- The nameargumentTypes
- 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 typename
- The nameargumentTypes
- 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 aNoSuchMethodError
if it doesn't exist.- Parameters:
type
- The typename
- The nameargumentTypes
- 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 aNoSuchMethodError
if it doesn't exist.- Type Parameters:
T
- The type- Parameters:
type
- The typeargumentTypes
- The argument types- Returns:
- An
Optional
contains the method or empty - Throws:
NoSuchMethodError
- If the method doesn't exist
-
getRequiredField
Finds a field on the given type for the given name.- Parameters:
type
- The typename
- 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 classfieldName
- The field nameinstance
- The instance- Returns:
- An
Optional
contains the value or empty of the value is null or an error occurred - Since:
- 4.0.0
-
findField
Finds a field in the type or super type.- Parameters:
type
- The typename
- The field name- Returns:
- An
Optional
of field
-
findMethodsByName
Finds a method on the given type for the given name.- Parameters:
type
- The typename
- The name- Returns:
- An
Optional
contains the method or empty
-
findDeclaredField
- Parameters:
type
- The typename
- The field name- Returns:
- An optional with the declared field
-
getAllInterfaces
- Parameters:
aClass
- A class- Returns:
- All the interfaces
-
populateInterfaces
- Parameters:
aClass
- A classinterfaces
- The interfaces- Returns:
- A set with the interfaces
-
newNoSuchMethodError
public static NoSuchMethodError newNoSuchMethodError(Class<?> declaringType, String name, Class<?>[] argumentTypes) - Parameters:
declaringType
- The declaring typename
- The method nameargumentTypes
- 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 fieldinstance
- The instancevalue
- 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 classfieldName
- The fieldNameinstance
- 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 classfieldName
- The fieldNameinstance
- The instancevalue
- The value- Since:
- 4.0.0
-