Package org.axonframework.common
Class ReflectionUtils
java.lang.Object
org.axonframework.common.ReflectionUtils
Utility class for working with Java Reflection API.
- 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 TypeMethodDescriptionstatic Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes) Returns the class on which the method with givenmethodNameand parameters of typeparameterTypesis declared.static <T extends AccessibleObject>
TensureAccessible(T member) Makes the givenmemberaccessible via reflection if it is not the case already.static booleanexplicitlyUnequal(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 implementsComparablewhich indicates the two are not equal.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 TypegetMemberGenericType(Member member) 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" modifierstatic MethodUtility function which returns aMethodmatching the givenmethodNameandparameterTypesin theclazz.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 declarationresolveMemberGenericType(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) Set thefieldofobjectto a certainvalue.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.- Parameters:
field- The field containing the valueobject- 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
Set thefieldofobjectto a certainvalue. If necessary, the field is made accessible, assuming the security manager allows it.- Type Parameters:
T- The type of thevalue- Parameters:
field- The field to setvalueonobject- The object to set thevalueonfieldvalue- The value to set onfield
-
declaringClass
public static Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes) Returns the class on which the method with givenmethodNameand parameters of typeparameterTypesis declared. The giveninstanceClassis 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 methodmethodName- The name of the methodparameterTypes- 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 methods returns true if 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 returnsfalse.- Parameters:
value- One of the values to compareotherValue- 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, etc) 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, etc) to check for accessibility- Returns:
trueif the member is accessible, otherwisefalse.
-
isNonFinalPublicMember
Checks whether the givenmemberis public and non-final. These members do no 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 forrecursive- 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
-
methodOf
public static Method methodOf(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException Utility function which returns aMethodmatching the givenmethodNameandparameterTypesin theclazz.- Parameters:
clazz- TheClassto return a method formethodName- AStringfor the simple name of the method to returnparameterTypes- An array of typeClassfor all the parameters which are part of theMethodbeing searched for- Returns:
- a
Methodobject from the givenclazzmatching the specifiedmethodName - Throws:
NoSuchMethodException- if noMethodcan be found matching themethodNameinclazz
-
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 forrecursive- 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 thewrapperTypeshave a single generic argument, which identifies the type they wrap. For example, if invoked withFuture.classandOptional.classaswrapperTypes:-
Future<String>resolves toString -
Optional<String>resolves toString -
Optional<Future<List<String>>>resolves toList<String>
- Parameters:
type- The type to unwrap, if wrappedwrapperTypes- 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 forgenericTypeIndex- 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 forgenericTypeIndex- 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.- Parameters:
method- the method to invokeobject- 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.- Parameters:
member- the member containing or returning the valuetarget- 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
-
getMemberGenericType
Returns the generic type of value of the givenmember, either by returning the generic type ofFieldor generic return type of aMethod.- Parameters:
member- the member to get generic type of- Returns:
- the generic 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.
-