Class ReflectionUtil

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

public class ReflectionUtil extends Object
  • Constructor Details

    • ReflectionUtil

      public ReflectionUtil()
  • Method Details

    • 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

      @SafeVarargs public static ReflectionUtil.FieldPredicate hasAtLeastOneAnnotation(Class<? extends Annotation>... annotation)
    • allFields

      public static ReflectionUtil.FieldPredicate allFields()
    • nonStaticField

      public static ReflectionUtil.FieldPredicate nonStaticField()
    • hasConstructor

      public static boolean hasConstructor(Class<?> type, Class<?>... parameterTypes)
    • findMethodTransitively

      public static Optional<Method> findMethodTransitively(Class<?> clazz, String methodName)
    • newInstance

      public static <T> T newInstance(Class<T> type)
    • newInstance

      public static <T> T newInstance(Class<T> type, Class<?>[] parameterTypes, Object... parameterValues)
    • 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)
    • getFieldValueOrNull

      public static Object getFieldValueOrNull(String fieldName, Object target, String errorDescription)
    • getFieldValueOrNull

      public static Object getFieldValueOrNull(Field field, Object target, String errorDescription)
    • getAllNonStaticFields

      public static List<Field> getAllNonStaticFields(Class<?> clazz)
    • getAllFieldValues

      public static List<Object> getAllFieldValues(Object target, Iterable<Field> fields, String errorDescription)
    • getAllFieldNames

      public static List<String> getAllFieldNames(Iterable<Field> fields)
    • 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)