org.axonframework.common
Class ReflectionUtils

java.lang.Object
  extended by org.axonframework.common.ReflectionUtils

public abstract class ReflectionUtils
extends Object

Utility class for working with Java Reflection API.

Since:
0.7
Author:
Allard Buijze

Method Summary
static Class<?> declaringClass(Class<?> instanceClass, String methodName, Class<?>... parameterTypes)
          Returns the class on which the method with name "getter" and parameters of type parameterTypes is declared.
static
<T extends AccessibleObject>
T
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 Object getFieldValue(Field field, Object object)
          Returns the value of the given field in the given object.
static boolean hasEqualsMethod(Class<?> type)
          Indicates whether the given class implements a customized equals method.
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 Iterable<Method> methodsOf(Class<?> clazz)
          Returns an Iterable of all the methods declared on the given class and its super classes.
static Class<?> resolvePrimitiveWrapperType(Class<?> primitiveType)
          Returns the boxed wrapper type for the given primitiveType.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getFieldValue

public static Object getFieldValue(Field field,
                                   Object object)
Returns the value of the given field in the given object. If necessary, the field is made accessible, assuming the security manager allows it.

Parameters:
field - The field containing the value
object - The object to retrieve the field's value from
Returns:
the value of the field in the object
Throws:
IllegalStateException - if the field is not accessible and the security manager doesn't allow it to be made accessible

declaringClass

public static Class<?> declaringClass(Class<?> instanceClass,
                                      String methodName,
                                      Class<?>... parameterTypes)
Returns the class on which the method with name "getter" and parameters of type parameterTypes is declared. The given instanceClass is the instance on which the method cn be called. If the method is not available on the given instanceClass, null is 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 null if not found

hasEqualsMethod

public static boolean hasEqualsMethod(Class<?> type)
Indicates whether the given class implements a customized equals method. This methods returns true if the declaring type of the equals method is not Object.

Parameters:
type - The type to inspect
Returns:
true if the given type overrides the equals method, otherwise false

explicitlyUnequal

public 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. 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:
true if these objects explicitly indicate they are not equal, false otherwise.

ensureAccessible

public static <T extends AccessibleObject> T ensureAccessible(T member)
Makes the given member accessible 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

public static boolean isAccessible(AccessibleObject member)
Indicates whether the given member is 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:
true if the member is accessible, otherwise false.

isNonFinalPublicMember

public static boolean isNonFinalPublicMember(Member member)
Checks whether the given member is public and non-final. These members do no need to be set accessible using reflection.

Parameters:
member - The member to check
Returns:
true if the member is public and non-final, otherwise false.
See Also:
isAccessible(java.lang.reflect.AccessibleObject), ensureAccessible(java.lang.reflect.AccessibleObject)

fieldsOf

public static Iterable<Field> fieldsOf(Class<?> clazz)
Returns an 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.

Parameters:
clazz - The class to return fields for
Returns:
an Iterable providing access to all declared fields in the class hierarchy

methodsOf

public static Iterable<Method> methodsOf(Class<?> clazz)
Returns an 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.

Parameters:
clazz - The class to return methods for
Returns:
an Iterable providing access to all declared methods in the class hierarchy

resolvePrimitiveWrapperType

public static Class<?> resolvePrimitiveWrapperType(Class<?> primitiveType)
Returns the boxed wrapper type for the given primitiveType.

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.

isTransient

public static boolean isTransient(Field field)
Indicates whether the given field has the "transient" modifier

Parameters:
field - the field to inspect
Returns:
true if the field is marked transient, otherwise false


Copyright © 2010-2016. All Rights Reserved.