Class ReflectionUtils
- Since:
- 0.7
- Author:
- Allard Buijze
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanSpecifying a reflection operation should not be performed recursive.static final booleanSpecifying a reflection operation should be performed recursive. -
Method Summary
Modifier and TypeMethodDescriptioncollectMatchingMethodsAndFields(Class<?> type, Predicate<Member> filter) collectMethodsAndFields(Class<?> type) collectSealedHierarchyIfSealed(Class<T> rootType) Collects all concrete types from a sealed hierarchy if the given type is sealed.static @Nullable Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes) Returns the class on which the method with givenmethodNameand parameters of typeparameterTypesis declared.enclosingClassesOf(Class<?> clazz) Returns aListof all enclosing classes of the given class.static <T extends AccessibleObject>
TensureAccessible(T member) Makes the givenmemberaccessible via reflection if it is not the case already.static booleanexplicitlyUnequal(@Nullable Object value, @Nullable Object otherValue) Indicates whether the two given objects are not the same, override an equals method that indicates they are not equal, or implementsComparablewhich indicates the two are not equal.static StringfieldNameFromMember(Member member) Returns the name of the field represented by the givenmember.Returns anIterableof all the fields declared on the given class and its super classes.Returns anIterableof all the fields declared on the given class.static <R> RgetFieldValue(Field field, Object object) Returns the value of the givenfieldin the givenobject.static StringgetMemberGenericString(Member member) Returns the generic string of the givenmember.static <R> RgetMemberValue(Member member, Object target) Returns the value of the givenmemberin the givenobject, either by returningFieldvalue or invoking the method.static Class<?> getMemberValueType(Member member) static booleanhasEqualsMethod(Class<?> type) Indicates whether the given class implements a customized equals method.static <R> RinvokeAndGetMethodValue(Method method, Object object) Invokes and returns the return value of the givenmethodin the givenobject.static booleanisAccessible(AccessibleObject member) Indicates whether the givenmemberis accessible.static booleanisNonFinalPublicMember(Member member) Checks whether the givenmemberis public and non-final.static booleanisTransient(Field field) Indicates whether the given field has the "transient" modifier.Returns anIterableof all the methods declared on the given class and its super classes.Returns anIterableof all the methods declared on the given class.resolveGenericType(Field field, int genericTypeIndex) Resolve a generic type parameter from a field declaration.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 givenprimitiveType.static TypeReturns the boxed wrapper type for the giventypeif it is primitive.static <T> voidsetFieldValue(Field field, Object object, T value) static StringtoDiscernibleSignature(Executable executable) Returns a discernible signature without including the classname.static TypeunwrapIfType(Type type, Class<?>... wrapperTypes) Unwrap the giventypeif is wrapped by any of the givenwrapperTypes.
-
Field Details
-
RECURSIVE
public static final boolean RECURSIVESpecifying a reflection operation should be performed recursive.- See Also:
-
NOT_RECURSIVE
public static final boolean NOT_RECURSIVESpecifying a reflection operation should not be performed recursive.- See Also:
-
-
Method Details
-
getFieldValue
Returns the value of the givenfieldin the givenobject.If necessary, the field is made accessible, assuming the security manager allows it.
- Type Parameters:
R- The type of the value to return.- Parameters:
field- The field containing the value.object- The object to retrieve the field's value from.- Returns:
- The value of the
fieldin theobject. - Throws:
IllegalStateException- If the field is not accessible and the security manager doesn't allow it to be made accessible.
-
setFieldValue
-
declaringClass
public static @Nullable Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes) Returns the class on which the method with givenmethodNameand parameters of typeparameterTypesis declared.The given
instanceClassis the instance on which the method can be called. If the method is not available on the giveninstanceClass,nullis returned.- Parameters:
instanceClass- The class on which to look for the method.methodName- The name of the method.parameterTypes- The parameter types of the method.- Returns:
- The class on which the method is declared, or
nullif not found.
-
hasEqualsMethod
Indicates whether the given class implements a customized equals method.This method returns
trueif the declaring type of the equals method is notObject.- Parameters:
type- The type to inspect.- Returns:
trueif the given type overrides the equals method, otherwisefalse.
-
explicitlyUnequal
Indicates whether the two given objects are not the same, override an equals method that indicates they are not equal, or implementsComparablewhich indicates the two are not equal.If this method cannot safely indicate two objects are not equal, it returns
false.- Parameters:
value- One of the values to compare.otherValue- other value to compare.- Returns:
trueif these objects explicitly indicate they are not equal,falseotherwise.
-
ensureAccessible
Makes the givenmemberaccessible via reflection if it is not the case already.- Type Parameters:
T- The type of member to make accessible.- Parameters:
member- The member (field, method, constructor, e.g.) to make accessible.- Returns:
- The given
member, for easier method chaining. - Throws:
IllegalStateException- If the member is not accessible and the security manager doesn't allow it to be made accessible.
-
isAccessible
Indicates whether the givenmemberis accessible.It does so by checking whether the member is non-final and public, or made accessible via reflection.
- Parameters:
member- The member (field, method, constructor, e.g.) to check for accessibility- Returns:
trueif the member is accessible, otherwisefalse.
-
isNonFinalPublicMember
Checks whether the givenmemberis public and non-final.These members do not need to be set accessible using reflection.
- Parameters:
member- The member to check.- Returns:
trueif the member is public and non-final, otherwisefalse..- See Also:
-
fieldsOf
Returns anIterableof 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.
- Parameters:
clazz- The class to return fields for.- Returns:
- An
Iterableproviding access to all declared fields in the class hierarchy.
-
fieldsOf
Returns anIterableof all the fields declared on the given class.Will include the given
clazz' super classes ifrecursivehas been set. The iterator will always return fields declared in a subtype before returning fields declared in a super type.- Parameters:
clazz- The class to return fields for.recursive- Defining whether fields should be found recursively on super classes as well.- Returns:
- An
Iterableproviding access to all declared fields in the class, including the hierarchy ifrecursivewas set.
-
methodsOf
Returns anIterableof 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.
- Parameters:
clazz- The class to return methods for.- Returns:
- An
Iterableproviding access to all declared methods in the class hierarchy.
-
methodsOf
Returns anIterableof all the methods declared on the given class.Will include the given
clazz' super classes ifrecursivehas been set. The iterator will always return fields declared in a subtype before returning fields declared in a super type.- Parameters:
clazz- The class to return methods for.recursive- Defining whether methods should be found recursively on super classes as well.- Returns:
- An
Iterableproviding access to all declared methods in the class, including the hierarchy ifrecursivewas set.
-
resolvePrimitiveWrapperType
Returns the boxed wrapper type for the givenprimitiveType.- Parameters:
primitiveType- The primitive type to return boxed wrapper type for.- Returns:
- The boxed wrapper type for the given
primitiveType. - Throws:
IllegalArgumentException- will be thrown instead of returning null if no wrapper class was found.
-
resolvePrimitiveWrapperTypeIfPrimitive
Returns the boxed wrapper type for the giventypeif it is primitive.- Parameters:
type- ATypeto return boxed wrapper type for.- Returns:
- The boxed wrapper type for the give
type, ortypeif no wrapper class was found.
-
unwrapIfType
Unwrap the giventypeif is wrapped by any of the givenwrapperTypes.This method assumes that the
For example, if invoked withwrapperTypeshave a single generic argument, which identifies the type they wrap.Future.classandOptional.classaswrapperTypes:-
Future<String>resolves toString -
Optional<String>resolves toString -
Optional<Future<List<String>>>resolves toList<String>
- Parameters:
type- The type to unwrap, if wrapped.wrapperTypes- The wrapper types to unwrap.- Returns:
- The unwrapped Type, or the original if it wasn't wrapped in any of the given wrapper types.
-
-
isTransient
Indicates whether the given field has the "transient" modifier.- Parameters:
field- The field to inspect.- Returns:
trueif the field is marked transient, otherwisefalse.
-
resolveGenericType
Resolve a generic type parameter from a field declaration.- Parameters:
field- The field to find generic parameters for.genericTypeIndex- The index of the type.- Returns:
- An optional that contains the resolved type, if found.
-
resolveMemberGenericType
Resolve a generic type parameter from a member declaration.- Parameters:
member- The member to find generic parameters for.genericTypeIndex- The index of the type.- Returns:
- An optional that contains the resolved type, if found.
-
invokeAndGetMethodValue
Invokes and returns the return value of the givenmethodin the givenobject. If necessary, the method is made accessible, assuming the security manager allows it.- Type Parameters:
R- The type of return object from invoking the givenmethod.- Parameters:
method- The method to invoke.object- The target object the givenmethodis invoked on.- Returns:
- The resulting value of invocation of the
methodin theobject - Throws:
IllegalStateException- If the method is not accessible and the security manager doesn't allow it to be made accessible.
-
getMemberValue
Returns the value of the givenmemberin the givenobject, either by returningFieldvalue or invoking the method. If necessary, the member is made accessible, assuming the security manager allows it. Supported members areFieldand non-voidMethodwithout parameters.- Type Parameters:
R- The type of return object from retrieving themember'svalue.- Parameters:
member- The member containing or returning the value.target- The object to retrieve the member's value from.- Returns:
- The value of the
memberin theobject. - Throws:
IllegalStateException- if the member is not supported.
-
getMemberValueType
Returns the type of value of the givenmember, either by returning the type ofFieldor type of the return value of aMethod.- Parameters:
member- The member to get the value type from.- Returns:
- The type of value of the
member. - Throws:
IllegalStateException- If the member is not supported.
-
getMemberGenericString
Returns the generic string of the givenmember.- Parameters:
member- the member to get the generic string for.- Returns:
- The generic string of the
member. - Throws:
IllegalStateException- If the member is not supported.
-
toDiscernibleSignature
Returns a discernible signature without including the classname.This will contain the method name and the parameter types, such as:
thisIfMyMethod(java.lang.String myString, com.acme.MyGreatObject).- Parameters:
executable- The executable to make a signature of.- Returns:
- The discernible signature.
-
fieldNameFromMember
Returns the name of the field represented by the givenmember.If the member is a method, it will return the name of the field it represents. For example, if the member is a getter method for a field named "getFoo", "isFoo", or "foo", it will return "foo". If the member is a field, it will return the name of the field.
- Parameters:
member- The member to get the field name for.- Returns:
- The name of the field represented by the given member.
-
collectMethodsAndFields
Collects allMembersof typeFieldandMethodfor given type in one single list. If the given type is a record, the record-style getters are deduplicated.- Parameters:
type- The type which fields and methods are collected.- Returns:
- List of all found fields and messages.
- See Also:
-
collectMatchingMethodsAndFields
public static List<? extends Member> collectMatchingMethodsAndFields(Class<?> type, Predicate<Member> filter) Collects allMembersof typeFieldandMethodwhich match the given filter for a given type in one single list. If the given type is a record, the record-style getters are deduplicated.- Parameters:
type- The type which fields and methods are collected.filter- A filter that only keeps matching members.- Returns:
- List of all found fields and messages.
-
enclosingClassesOf
Returns aListof all enclosing classes of the given class.The
Listreturns enclosing classes from the immediate enclosing class outward to the outermost enclosing class. If the given class has no enclosing class, an emptyListis returned.- Parameters:
clazz- the class to return enclosing classes for- Returns:
- an immutable
Listproviding access to all enclosing classes, from innermost to outermost
-
collectSealedHierarchyIfSealed
Collects all concrete types from a sealed hierarchy if the given type is sealed. Returns an empty set if the type is not sealed.- Type Parameters:
T- The type parameter.- Parameters:
rootType- The root type to scan for sealed hierarchy.- Returns:
- A set of all concrete types in the hierarchy, or an empty set if the type is not sealed.
-