public abstract class ReflectionUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static boolean |
NOT_RECURSIVE
Specifying a reflection operation should not be performed recursive.
|
static boolean |
RECURSIVE
Specifying a reflection operation should be performed recursive.
|
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
declaringClass(Class<?> instanceClass,
String methodName,
Class<?>... parameterTypes)
Returns the class on which the method with given
methodName and parameters of type
parameterTypes is declared. |
static <T extends AccessibleObject> |
ensureAccessible(T member)
Makes the given
member accessible via reflection if it is not the case already. |
static boolean |
explicitlyUnequal(Object value,
Object otherValue)
Indicates whether the two given objects are not the same, override an equals method that indicates
they are not equal, or implements
Comparable which indicates the two are not equal. |
static Iterable<Field> |
fieldsOf(Class<?> clazz)
Returns an
Iterable of all the fields declared on the given class and its super classes. |
static Iterable<Field> |
fieldsOf(Class<?> clazz,
boolean recursive)
Returns an
Iterable of all the fields declared on the given class. |
static <R> R |
getFieldValue(Field field,
Object object)
Returns the value of the given
field in the given object. |
static String |
getMemberGenericString(Member member)
Returns the generic string of the given
member. |
static Type |
getMemberGenericType(Member member)
|
static <R> R |
getMemberValue(Member member,
Object target)
Returns the value of the given
member in the given object, either by returning Field
value or invoking the method. |
static Class<?> |
getMemberValueType(Member member)
|
static boolean |
hasEqualsMethod(Class<?> type)
Indicates whether the given class implements a customized equals method.
|
static <R> R |
invokeAndGetMethodValue(Method method,
Object object)
Invokes and returns the return value of the given
method in the given object. |
static boolean |
isAccessible(AccessibleObject member)
Indicates whether the given
member is accessible. |
static boolean |
isNonFinalPublicMember(Member member)
Checks whether the given
member is public and non-final. |
static boolean |
isTransient(Field field)
Indicates whether the given field has the "transient" modifier
|
static Method |
methodOf(Class<?> clazz,
String methodName,
Class<?>... parameterTypes)
Utility function which returns a
Method matching the given methodName and
parameterTypes in the clazz. |
static Iterable<Method> |
methodsOf(Class<?> clazz)
Returns an
Iterable of all the methods declared on the given class and its super classes. |
static Iterable<Method> |
methodsOf(Class<?> clazz,
boolean recursive)
Returns an
Iterable of all the methods declared on the given class. |
static Optional<Class<?>> |
resolveGenericType(Field field,
int genericTypeIndex)
Resolve a generic type parameter from a field declaration
|
static Optional<Class<?>> |
resolveMemberGenericType(Member member,
int genericTypeIndex)
Resolve a generic type parameter from a member declaration.
|
static Class<?> |
resolvePrimitiveWrapperType(Class<?> primitiveType)
Returns the boxed wrapper type for the given
primitiveType. |
static Type |
resolvePrimitiveWrapperTypeIfPrimitive(Type type)
Returns the boxed wrapper type for the given
type if it is primitive. |
static <T> void |
setFieldValue(Field field,
Object object,
T value)
Set the
field of object to a certain value. |
static String |
toDiscernibleSignature(Executable executable)
Returns a discernible signature without including the classname.
|
static Type |
unwrapIfType(Type type,
Class<?>... wrapperTypes)
Unwrap the given
type if is wrapped by any of the given wrapperTypes. |
public static final boolean RECURSIVE
public static final boolean NOT_RECURSIVE
public static <R> R getFieldValue(Field field, Object object)
field in the given object. If necessary, the field is
made accessible, assuming the security manager allows it.field - The field containing the valueobject - The object to retrieve the field's value fromfield in the objectIllegalStateException - if the field is not accessible and the security manager doesn't allow it to be
made accessiblepublic static <T> void setFieldValue(Field field, Object object, T value)
field of object to a certain value. If necessary, the field is made accessible,
assuming the security manager allows it.T - The type of the valuefield - The field to set value onobject - The object to set the value on fieldvalue - The value to set on fieldpublic static Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes)
methodName and parameters of type
parameterTypes is declared. The given instanceClass is the instance on which the
method can be called. If the method is not available on the given instanceClass, null
is returned.instanceClass - The class on which to look for the methodmethodName - The name of the methodparameterTypes - The parameter types of the methodnull if not foundpublic static boolean hasEqualsMethod(Class<?> type)
Object.type - The type to inspecttrue if the given type overrides the equals method, otherwise falsepublic static boolean explicitlyUnequal(Object value, Object otherValue)
Comparable which indicates the two are not equal. If this
method cannot safely indicate two objects are not equal, it returns
false.value - One of the values to compareotherValue - other value to comparetrue if these objects explicitly indicate they are not equal, false otherwise.public static <T extends AccessibleObject> T ensureAccessible(T member)
member accessible via reflection if it is not the case already.T - The type of member to make accessiblemember - The member (field, method, constructor, etc) to make accessiblemember, for easier method chainingIllegalStateException - if the member is not accessible and the security manager doesn't allow it to be
made accessiblepublic static boolean isAccessible(AccessibleObject member)
member is accessible. It does so by checking whether the member is
non-final and public, or made accessible via reflection.member - The member (field, method, constructor, etc) to check for accessibilitytrue if the member is accessible, otherwise false.public static boolean isNonFinalPublicMember(Member member)
member is public and non-final. These members do no need to be set
accessible using reflection.member - The member to checktrue if the member is public and non-final, otherwise false.isAccessible(java.lang.reflect.AccessibleObject),
ensureAccessible(java.lang.reflect.AccessibleObject)public static Iterable<Field> fieldsOf(Class<?> clazz)
Iterable of all the fields declared on the given class and its super classes. The iterator
will always return fields declared in a subtype before returning fields declared in a super type.clazz - the class to return fields forIterable providing access to all declared fields in the class hierarchypublic static Iterable<Field> fieldsOf(Class<?> clazz, boolean recursive)
Iterable of all the fields declared on the given class.
Will include the given clazz' super classes if recursive has been set. The iterator will always
return fields declared in a subtype before returning fields declared in a super type.
clazz - the class to return fields forrecursive - defining whether fields should be found recursively on super classes as wellIterable providing access to all declared fields in the class, including the hierarchy if
recursive was setpublic static Method methodOf(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException
Method matching the given methodName and
parameterTypes in the clazz.clazz - The Class to return a method formethodName - A String for the simple name of the method to returnparameterTypes - An array of type Class for all the parameters which are part of the
Method being searched forMethod object from the given clazz matching the specified
methodNameNoSuchMethodException - if no Method can be found matching the methodName
in clazzpublic static Iterable<Method> methodsOf(Class<?> clazz)
Iterable of all the methods declared on the given class and its super classes. The iterator
will always return methods declared in a subtype before returning methods declared in a super type.clazz - the class to return methods forIterable providing access to all declared methods in the class hierarchypublic static Iterable<Method> methodsOf(Class<?> clazz, boolean recursive)
Iterable of all the methods declared on the given class.
Will include the given clazz' super classes if recursive has been set. The iterator will always
return fields declared in a subtype before returning fields declared in a super type.
clazz - the class to return methods forrecursive - defining whether methods should be found recursively on super classes as wellIterable providing access to all declared methods in the class, including the hierarchy if
recursive was setpublic static Class<?> resolvePrimitiveWrapperType(Class<?> primitiveType)
primitiveType.primitiveType - The primitive type to return boxed wrapper type forprimitiveTypeIllegalArgumentException - will be thrown instead of returning null if no wrapper class was found.public static Type resolvePrimitiveWrapperTypeIfPrimitive(Type type)
type if it is primitive.type - a Type to return boxed wrapper type fortype, or type if no wrapper class was found.public static Type unwrapIfType(Type type, Class<?>... wrapperTypes)
type if is wrapped by any of the given wrapperTypes. This method assumes that
the wrapperTypes have a single generic argument, which identifies the type they wrap.
For example, if invoked with Future.class and Optional.class as wrapperTypes:
Future<String> resolves to StringOptional<String> resolves to StringOptional<Future<List<String>>> resolves to List<String>type - The type to unwrap, if wrappedwrapperTypes - The wrapper types to unwrappublic static boolean isTransient(Field field)
field - the field to inspecttrue if the field is marked transient, otherwise false
public static Optional<Class<?>> resolveGenericType(Field field, int genericTypeIndex)
field - The field to find generic parameters forgenericTypeIndex - The index of the typepublic static Optional<Class<?>> resolveMemberGenericType(Member member, int genericTypeIndex)
member - the member to find generic parameters forgenericTypeIndex - the index of the typepublic static <R> R invokeAndGetMethodValue(Method method, Object object)
method in the given object. If necessary, the
method is made accessible, assuming the security manager allows it.method - the method to invokeobject - the target object the given method is invoked onmethod in the objectIllegalStateException - if the method is not accessible and the security manager doesn't allow it to be
made accessiblepublic static <R> R getMemberValue(Member member, Object target)
member in the given object, either by returning Field
value or invoking the method. If necessary, the member is made accessible, assuming the security manager allows
it. Supported members are Field and non-void Method without parameters.member - the member containing or returning the valuetarget - the object to retrieve the member's value frommember in the objectIllegalStateException - if the member is not supportedpublic static Class<?> getMemberValueType(Member member)
member, either by returning the type of Field or type of
the return value of a Method.member - the member to get the value type frommemberIllegalStateException - if the member is not supportedpublic static Type getMemberGenericType(Member member)
member, either by returning the generic type of Field or generic return type of a Method.member - the member to get generic type ofmemberIllegalStateException - if the member is not supportedpublic static String getMemberGenericString(Member member)
member.member - the member to get the generic string formemberIllegalStateException - if the member is not supportedpublic static String toDiscernibleSignature(Executable executable)
thisIfMyMethod(java.lang.String myString, com.acme.MyGreatObject).executable - The executable to make a signature of.Copyright © 2010–2025. All rights reserved.