com.tngtech.jgiven.impl.util
Class ReflectionUtil

java.lang.Object
  extended by com.tngtech.jgiven.impl.util.ReflectionUtil

public class ReflectionUtil
extends Object


Nested Class Summary
static interface ReflectionUtil.ClassAction
           
static interface ReflectionUtil.FieldAction
           
static interface ReflectionUtil.FieldPredicate
           
static interface ReflectionUtil.MethodAction
           
 
Constructor Summary
ReflectionUtil()
           
 
Method Summary
static ReflectionUtil.FieldPredicate allFields()
           
static com.google.common.base.Optional<Method> findMethodTransitively(Class<?> clazz, String methodName)
           
static void forEachField(Object object, Class<?> clazz, ReflectionUtil.FieldPredicate predicate, ReflectionUtil.FieldAction action)
          Iterates over all fields of the given class and all its super classes and calls action.act() for the fields that are annotated with the given annotation.
static void forEachMethod(Object object, Class<?> clazz, Class<? extends Annotation> annotation, ReflectionUtil.MethodAction action)
          Iterates over all methods of the given class and all its super classes and calls action.act() for the methods that are annotated with the given annotation.
static void forEachSuperClass(Class<?> clazz, ReflectionUtil.ClassAction action)
          Iterates over all super classes of the given class (including the class itself) and calls action.act() for these classes.
static List<Object> getAllNonStaticFieldValuesFrom(Class<?> clazz, Object target, String errorDescription)
          Returns a List of objects reflecting all the non-static field values declared by the class or interface represented by the given Class object and defined by the given Object.
static List<Method> getNonStaticMethod(Method[] declaredMethods)
           
static ReflectionUtil.FieldPredicate hasAtLeastOneAnnotation(Class<? extends Annotation>... annotation)
           
static void invokeMethod(Object object, Method method, String errorDescription)
           
static void makeAccessible(AccessibleObject object, String errorDescription)
           
static
<T> T
newInstance(Class<T> value)
           
static ReflectionUtil.FieldPredicate nonStaticField()
           
static void setField(Field field, Object object, Object value, String errorDescription)
           
static String toReadableString(AccessibleObject object)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtil

public ReflectionUtil()
Method Detail

forEachField

public static void forEachField(Object object,
                                Class<?> clazz,
                                ReflectionUtil.FieldPredicate predicate,
                                ReflectionUtil.FieldAction action)
Iterates over all fields of the given class and all its super classes and calls action.act() for the fields that are annotated with the given annotation.


forEachMethod

public static void forEachMethod(Object object,
                                 Class<?> clazz,
                                 Class<? extends Annotation> annotation,
                                 ReflectionUtil.MethodAction action)
Iterates over all methods of the given class and all its super classes and calls action.act() for the methods that are annotated with the given annotation.


forEachSuperClass

public static void forEachSuperClass(Class<?> clazz,
                                     ReflectionUtil.ClassAction action)
Iterates over all super classes of the given class (including the class itself) and calls action.act() for these classes.


hasAtLeastOneAnnotation

public static ReflectionUtil.FieldPredicate hasAtLeastOneAnnotation(Class<? extends Annotation>... annotation)

allFields

public static ReflectionUtil.FieldPredicate allFields()

nonStaticField

public static ReflectionUtil.FieldPredicate nonStaticField()

findMethodTransitively

public static com.google.common.base.Optional<Method> findMethodTransitively(Class<?> clazz,
                                                                             String methodName)

newInstance

public static <T> T newInstance(Class<T> value)

invokeMethod

public static void invokeMethod(Object object,
                                Method method,
                                String errorDescription)

getAllNonStaticFieldValuesFrom

public static List<Object> getAllNonStaticFieldValuesFrom(Class<?> clazz,
                                                          Object target,
                                                          String errorDescription)
Returns a List of objects reflecting all the non-static field values declared by the class or interface represented by the given Class object and defined by the given Object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields. The elements in the List returned are not sorted and are not in any particular order. This method returns an empty List if the class or interface declares no fields, or if the given Class object represents a primitive type, an array class, or void.

Parameters:
clazz - class or interface declaring fields
target - instance of given clazz from which field values should be retrieved
errorDescription - customizable part of logged error message
Returns:
a List containing all the found field values (never null)

setField

public static void setField(Field field,
                            Object object,
                            Object value,
                            String errorDescription)

makeAccessible

public static void makeAccessible(AccessibleObject object,
                                  String errorDescription)

toReadableString

public static String toReadableString(AccessibleObject object)

getNonStaticMethod

public static List<Method> getNonStaticMethod(Method[] declaredMethods)


Copyright © 2014 TNG Technology Consulting. All rights reserved.