Class WhiteboxImpl


  • public class WhiteboxImpl
    extends java.lang.Object
    Various 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 void assertObjectInGetInternalStateIsNotNull​(java.lang.Object object)
      Assert object in get internal state is not null.
      private static java.lang.reflect.Field findFieldInHierarchy​(java.lang.Object object, java.lang.String fieldName)
      Find field in hierarchy.
      private static java.lang.reflect.Field findFieldInHierarchy​(java.lang.Object object, FieldMatcherStrategy strategy)
      Find field in hierarchy.
      private static java.lang.reflect.Field findSingleFieldUsingStrategy​(FieldMatcherStrategy strategy, java.lang.Object object, boolean checkHierarchy, java.lang.Class<?> startClass)
      Find single field using strategy.
      static <T> T getByNameAndType​(java.lang.Object object, java.lang.String fieldName, java.lang.Class<T> expectedFieldType)
      getByNameAndType.
      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.
      static <T> T getInternalState​(java.lang.Object object, java.lang.Class<T> fieldType)
      Get the value of a field using reflection.
      static <T> T getInternalState​(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 boolean hasFieldProperModifier​(java.lang.Object object, java.lang.reflect.Field field)
      Checks for field proper modifier.
      static boolean isClass​(java.lang.Object argument)
      Checks if is class.
      static void throwExceptionIfFieldWasNotFound​(java.lang.Class<?> type, java.lang.String fieldName, java.lang.reflect.Field field)
      Throw exception if field was not found.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • WhiteboxImpl

        public WhiteboxImpl()
    • 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 modify
        fieldName - 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 object
        fieldName - 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 object
        strategy - 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 strategy
        object - the object
        checkHierarchy - the check hierarchy
        startClass - 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 object
        field - 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 modify
        fieldType - 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 type
        fieldName - the field name
        field - 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 - a Object object.
        fieldName - a String object.
        expectedFieldType - a Class object.
        Returns:
        a T object.