Package org.awaitility.reflect
Class WhiteboxImpl
- java.lang.Object
-
- org.awaitility.reflect.WhiteboxImpl
-
public class WhiteboxImpl extends java.lang.ObjectVarious utilities for accessing internals of a class. Basically a simplified reflection utility. Copied with permission from PowerMock project.
-
-
Constructor Summary
Constructors Constructor Description WhiteboxImpl()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static voidassertObjectInGetInternalStateIsNotNull(java.lang.Object object)Assert object in get internal state is not null.private static java.lang.reflect.FieldfindFieldInHierarchy(java.lang.Object object, java.lang.String fieldName)Find field in hierarchy.private static java.lang.reflect.FieldfindFieldInHierarchy(java.lang.Object object, FieldMatcherStrategy strategy)Find field in hierarchy.private static java.lang.reflect.FieldfindSingleFieldUsingStrategy(FieldMatcherStrategy strategy, java.lang.Object object, boolean checkHierarchy, java.lang.Class<?> startClass)Find single field using strategy.static <T> TgetByNameAndType(java.lang.Object object, java.lang.String fieldName, java.lang.Class<T> expectedFieldType)getByNameAndType.static java.lang.reflect.FieldgetFieldAnnotatedWith(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)Get field annotated with a particular annotation.static <T> TgetInternalState(java.lang.Object object, java.lang.Class<T> fieldType)Get the value of a field using reflection.static <T> TgetInternalState(java.lang.Object object, java.lang.String fieldName)Get the value of a field using reflection.static java.lang.Class<?>getType(java.lang.Object object)Gets the type.private static booleanhasFieldProperModifier(java.lang.Object object, java.lang.reflect.Field field)Checks for field proper modifier.static booleanisClass(java.lang.Object argument)Checks if is class.static voidthrowExceptionIfFieldWasNotFound(java.lang.Class<?> type, java.lang.String fieldName, java.lang.reflect.Field field)Throw exception if field was not found.
-
-
-
Method Detail
-
getInternalState
public static <T> T getInternalState(java.lang.Object object, java.lang.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 modifyfieldName- the name of the field- Returns:
- the internal state.
-
findFieldInHierarchy
private static java.lang.reflect.Field findFieldInHierarchy(java.lang.Object object, java.lang.String fieldName)Find field in hierarchy.- Parameters:
object- the objectfieldName- the field name- Returns:
- the field
-
findFieldInHierarchy
private static java.lang.reflect.Field findFieldInHierarchy(java.lang.Object object, FieldMatcherStrategy strategy)Find field in hierarchy.- Parameters:
object- the objectstrategy- the strategy- Returns:
- the field
-
assertObjectInGetInternalStateIsNotNull
private static void assertObjectInGetInternalStateIsNotNull(java.lang.Object object)
Assert object in get internal state is not null.- Parameters:
object- the object
-
findSingleFieldUsingStrategy
private static java.lang.reflect.Field findSingleFieldUsingStrategy(FieldMatcherStrategy strategy, java.lang.Object object, boolean checkHierarchy, java.lang.Class<?> startClass)
Find single field using strategy.- Parameters:
strategy- the strategyobject- the objectcheckHierarchy- the check hierarchystartClass- the start class- Returns:
- the field
-
hasFieldProperModifier
private static boolean hasFieldProperModifier(java.lang.Object object, java.lang.reflect.Field field)Checks for field proper modifier.- Parameters:
object- the objectfield- the field- Returns:
- true, if successful
-
getInternalState
public static <T> T getInternalState(java.lang.Object object, java.lang.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 modifyfieldType- the type of the field- Returns:
- the internal state
-
throwExceptionIfFieldWasNotFound
public static void throwExceptionIfFieldWasNotFound(java.lang.Class<?> type, java.lang.String fieldName, java.lang.reflect.Field field)Throw exception if field was not found.- Parameters:
type- the typefieldName- the field namefield- the field
-
getType
public static java.lang.Class<?> getType(java.lang.Object object)
Gets the type.- Parameters:
object- the object- Returns:
- The type of the of an object.
-
getFieldAnnotatedWith
public static java.lang.reflect.Field getFieldAnnotatedWith(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.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(java.lang.Object argument)
Checks if is class.- Parameters:
argument- the argument- Returns:
- a boolean.
-
getByNameAndType
public static <T> T getByNameAndType(java.lang.Object object, java.lang.String fieldName, java.lang.Class<T> expectedFieldType)getByNameAndType.
- Type Parameters:
T- a T object.- Parameters:
object- aObjectobject.fieldName- aStringobject.expectedFieldType- aClassobject.- Returns:
- a T object.
-
-