org.axonframework.util
Class ReflectionUtils

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

public abstract class ReflectionUtils
extends Object

Utility class for working with Java Reflection API.

Since:
0.7
Author:
Allard Buijze

Method Summary
static void ensureAccessible(Field field)
          Makes the given field accessible via reflection if it is not the case already.
static Iterable<Field> fieldsOf(Class<?> clazz)
          Returns an Iterable of all the fields declared on the given class and its super classes.
static
<T> Collection<T>
findFieldValuesOfType(Object instance, Class<T> type)
          Returns a collection of values contained in the fields of the given instance that are assignable to the given type.
static Object getFieldValue(Field field, Object object)
          Returns the value of the given field in the given object.
static boolean isAccessible(Field field)
          Indicates whether the given field is accessible.
static Iterable<Method> methodsOf(Class<?> clazz)
          Returns an Iterable of all the methods declared on the given class and its super classes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

findFieldValuesOfType

public static <T> Collection<T> findFieldValuesOfType(Object instance,
                                                      Class<T> type)
Returns a collection of values contained in the fields of the given instance that are assignable to the given type. If the given instance contains fields with Collections or Maps, the contents of them are investigated as well. Collections inside these collections (e.g. a List of Maps) are not evaluated.

Type Parameters:
T - The type that the values in the fields must be assignable to
Parameters:
instance - The instance to search the fields in
type - The type that the values in the fields must be assignable to
Returns:
a collection of values contained in the fields of the given instance. Can be empty. Is never null.

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

ensureAccessible

public static void ensureAccessible(Field field)
Makes the given field accessible via reflection if it is not the case already.

Parameters:
field - The field to make accessible
Throws:
IllegalStateException - if the field is not accessible and the security manager doesn't allow it to be made accessible

isAccessible

public static boolean isAccessible(Field field)
Indicates whether the given field is accessible. It does so by checking whether the field is non-final and public, or made accessible via reflection.

Parameters:
field - The field to check for accessibility
Returns:
true if the field is accessible, otherwise false.

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


Copyright © 2011. All Rights Reserved.