Class WhiteboxImpl

java.lang.Object
org.awaitility.reflect.WhiteboxImpl

public class WhiteboxImpl extends Object
Various utilities for accessing internals of a class. Basically a simplified reflection utility. Copied with permission from PowerMock project.
  • Constructor Details

    • WhiteboxImpl

      public WhiteboxImpl()
  • Method Details

    • getInternalState

      public static <T> T getInternalState(Object object, String fieldName)
      Get the value of a field using reflection. This method will iterate through the entire class hierarchy and return the value of the first field named fieldName. If you want to get a specific field value at specific place in the class hierarchy please refer to
      Type Parameters:
      T - the generic type
      Parameters:
      object - the object to modify
      fieldName - the name of the field
      Returns:
      the internal state.
    • findFieldInHierarchy

      private static Field findFieldInHierarchy(Object object, String fieldName)
      Find field in hierarchy.
      Parameters:
      object - the object
      fieldName - the field name
      Returns:
      the field
    • findFieldInHierarchy

      private static Field findFieldInHierarchy(Object object, FieldMatcherStrategy strategy)
      Find field in hierarchy.
      Parameters:
      object - the object
      strategy - the strategy
      Returns:
      the field
    • assertObjectInGetInternalStateIsNotNull

      private static void assertObjectInGetInternalStateIsNotNull(Object object)
      Assert object in get internal state is not null.
      Parameters:
      object - the object
    • findSingleFieldUsingStrategy

      private static Field findSingleFieldUsingStrategy(FieldMatcherStrategy strategy, Object object, boolean checkHierarchy, Class<?> startClass)
      Find single field using strategy.
      Parameters:
      strategy - the strategy
      object - the object
      checkHierarchy - the check hierarchy
      startClass - the start class
      Returns:
      the field
    • hasFieldProperModifier

      private static boolean hasFieldProperModifier(Object object, Field field)
      Checks for field proper modifier.
      Parameters:
      object - the object
      field - the field
      Returns:
      true, if successful
    • getInternalState

      public static <T> T getInternalState(Object object, Class<T> fieldType)
      Get the value of a field using reflection. This method will traverse the super class hierarchy until the first field of type fieldType is found. The value of this field will be returned.
      Type Parameters:
      T - the generic type
      Parameters:
      object - the object to modify
      fieldType - the type of the field
      Returns:
      the internal state
    • throwExceptionIfFieldWasNotFound

      public static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field)
      Throw exception if field was not found.
      Parameters:
      type - the type
      fieldName - the field name
      field - the field
    • getType

      public static Class<?> getType(Object object)
      Gets the type.
      Parameters:
      object - the object
      Returns:
      The type of the of an object.
    • getFieldAnnotatedWith

      public static Field getFieldAnnotatedWith(Object object, Class<? extends Annotation> annotationType)
      Get field annotated with a particular annotation. This method traverses the class hierarchy when checking for the annotation.
      Parameters:
      object - The object to look for annotations. Note that if're you're passing an object only instance fields are checked, passing a class will only check static fields.
      annotationType - The annotation types to look for
      Returns:
      A set of all fields containing the particular annotation(s).
      Since:
      1.3
    • isClass

      public static boolean isClass(Object argument)
      Checks if is class.
      Parameters:
      argument - the argument
      Returns:
      a boolean.
    • getByNameAndType

      public static <T> T getByNameAndType(Object object, String fieldName, Class<T> expectedFieldType)

      getByNameAndType.

      Type Parameters:
      T - a T object.
      Parameters:
      object - a Object object.
      fieldName - a String object.
      expectedFieldType - a Class object.
      Returns:
      a T object.