Class Whitebox


  • public class Whitebox
    extends java.lang.Object
    Various utilities for accessing internals of a class. Basically a simplified reflection utility intended for tests.
    • Constructor Summary

      Constructors 
      Constructor Description
      Whitebox()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.util.Set<java.lang.reflect.Field> getAllInstanceFields​(java.lang.Object object)
      Get all instance fields for a particular object.
      static java.util.Set<java.lang.reflect.Field> getAllStaticFields​(java.lang.Class<?> type)
      Get all static fields for a particular type.
      static java.lang.Class<java.lang.Object> getAnonymousInnerClassType​(java.lang.Class<?> declaringClass, int occurrence)
      Get the type of an anonymous inner class.
      static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<?> type, java.lang.Class<?>... parameterTypes)
      Convenience method to get a (declared) constructor from a class type without having to catch the checked exceptions otherwise required.
      static java.lang.reflect.Field getField​(java.lang.Class<?> type, java.lang.String fieldName)
      Convenience method to get a field from a class type.
      static java.lang.reflect.Field[] getFields​(java.lang.Class<?> clazz, java.lang.String... fieldNames)
      Get an array of Field's that matches the supplied list of field names.
      static java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith​(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.Annotation>[] annotationTypes)
      Get all fields annotated with a particular annotation.
      static java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith​(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.Class<? extends java.lang.annotation.Annotation>... additionalAnnotations)
      Get all fields annotated with a particular annotation.
      static java.util.Set<java.lang.reflect.Field> getFieldsOfType​(java.lang.Object object, java.lang.Class<?> type)
      Get all fields assignable from a particular type.
      static java.lang.reflect.Constructor<?> getFirstParentConstructor​(java.lang.Class<?> klass)
      Get the first parent constructor defined in a super class of klass.
      static java.lang.Class<java.lang.Object> getInnerClassType​(java.lang.Class<?> declaringClass, java.lang.String name)
      Get an inner class type
      static <T> T getInternalState​(java.lang.Object object, java.lang.Class<T> fieldType)
      Get the value of a field using reflection based on the fields type.
      static <T> T getInternalState​(java.lang.Object object, java.lang.Class<T> fieldType, java.lang.Class<?> where)
      Get the value of a field using reflection based on the field type.
      static <T> T getInternalState​(java.lang.Object object, java.lang.String fieldName)
      Get the value of a field using reflection.
      static <T> T getInternalState​(java.lang.Object object, java.lang.String fieldName, java.lang.Class<?> where)
      Get the value of a field using reflection.
      static <T> T getInternalState​(java.lang.Object object, java.lang.String fieldName, java.lang.Class<?> where, java.lang.Class<T> type)
      Deprecated.
      static java.lang.Class<java.lang.Object> getLocalClassType​(java.lang.Class<?> declaringClass, int occurrence, java.lang.String name)
      Get the type of a local inner class.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> type, java.lang.Class<?>... parameterTypes)
      Convenience method to get a method from a class type without having to catch the checked exceptions otherwise required.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Convenience method to get a method from a class type without having to catch the checked exceptions otherwise required.
      static java.lang.reflect.Method[] getMethods​(java.lang.Class<?> clazz, java.lang.String... methodNames)
      Get an array of Method's that matches the supplied list of method names.
      static java.lang.Class<?> getType​(java.lang.Object object)  
      static <T> T invokeConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Class<?>[] parameterTypes, java.lang.Object[] arguments)
      Invoke a constructor.
      static <T> T invokeConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Object... arguments)
      Invoke a constructor.
      static <T> T invokeMethod​(java.lang.Class<?> klass, java.lang.Object... arguments)
      Invoke a private or inner class static method without the need to specify the method name.
      static <T> T invokeMethod​(java.lang.Class<?> clazz, java.lang.String methodToExecute, java.lang.Object... arguments)
      Invoke a static private or inner class method.
      static <T> T invokeMethod​(java.lang.Object object, java.lang.Class<?> declaringClass, java.lang.String methodToExecute, java.lang.Class<?>[] parameterTypes, java.lang.Object... arguments)
      Invoke a private or inner class method in that is located in a subclass of the instance.
      static <T> T invokeMethod​(java.lang.Object instance, java.lang.Class<?> declaringClass, java.lang.String methodToExecute, java.lang.Object... arguments)
      Invoke a private or inner class method in that is located in a subclass of the instance.
      static <T> T invokeMethod​(java.lang.Object instance, java.lang.Object... arguments)
      Invoke a private or inner class method without the need to specify the method name.
      static <T> T invokeMethod​(java.lang.Object instance, java.lang.String methodToExecute, java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments)
      Invoke a private or inner class method in cases where PowerMock cannot automatically determine the type of the parameters, for example when mixing primitive types and wrapper types in the same method.
      static <T> T invokeMethod​(java.lang.Object instance, java.lang.String methodToExecute, java.lang.Class<?> definedIn, java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments)
      Invoke a private or inner class method in a subclass (defined by definedIn) in cases where PowerMock cannot automatically determine the type of the parameters, for example when mixing primitive types and wrapper types in the same method.
      static <T> T invokeMethod​(java.lang.Object instance, java.lang.String methodToExecute, java.lang.Object... arguments)
      Invoke a private or inner class method.
      static <T> T newInstance​(java.lang.Class<T> classToInstantiate)
      Create a new instance of a class without invoking its constructor.
      static void setInternalState​(java.lang.Object object, java.lang.Class<?> fieldType, java.lang.Object value)
      Set the value of a field using reflection.
      static void setInternalState​(java.lang.Object object, java.lang.Class<?> fieldType, java.lang.Object value, java.lang.Class<?> where)
      Set the value of a field using reflection at a specific location ( where) in the class hierarchy.
      static void setInternalState​(java.lang.Object object, java.lang.Object value, java.lang.Class<?> where)
      Set the value of a field using reflection at at specific place in the class hierarchy (where).
      static void setInternalState​(java.lang.Object object, java.lang.Object value, java.lang.Object... additionalValues)
      Set the value of a field using reflection.
      static void setInternalState​(java.lang.Object object, java.lang.String fieldName, java.lang.Object value)
      Set the value of a field using reflection.
      static void setInternalState​(java.lang.Object object, java.lang.String fieldName, java.lang.Object[] value)
      Set an array value of a field using reflection.
      static void setInternalState​(java.lang.Object object, java.lang.String fieldName, java.lang.Object value, java.lang.Class<?> where)
      Set the value of a field using reflection.
      static void setInternalStateFromContext​(java.lang.Object classOrInstance, java.lang.Class<?> context, java.lang.Class<?>... additionalContexts)
      Set the values of multiple static fields defined in a context using reflection.
      static void setInternalStateFromContext​(java.lang.Object instance, java.lang.Class<?> context, FieldMatchingStrategy strategy)
      Set the values of multiple static fields defined in a context using reflection and using an explicit FieldMatchingStrategy.
      static void setInternalStateFromContext​(java.lang.Object instance, java.lang.Object context, java.lang.Object... additionalContexts)
      Set the values of multiple instance fields defined in a context using reflection.
      static void setInternalStateFromContext​(java.lang.Object instance, java.lang.Object context, FieldMatchingStrategy strategy)
      Set the values of multiple instance fields defined in a context using reflection and using an explicit FieldMatchingStrategy.
      • Methods inherited from class java.lang.Object

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

      • Whitebox

        public Whitebox()
    • Method Detail

      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class<?> type,
                                                       java.lang.String fieldName)
        Convenience method to get a field from a class type.

        The method will first try to look for a declared field in the same class. If the field is not declared in this class it will look for the field in the super class. This will continue throughout the whole class hierarchy. If the field is not found an IllegalArgumentException is thrown.

        Parameters:
        type - The type of the class where the method is located.
        fieldName - The method names.
        Returns:
        A java.lang.reflect.Field.
        Throws:
        FieldNotFoundException - If a field cannot be found in the hierarchy.
      • getFields

        public static java.lang.reflect.Field[] getFields​(java.lang.Class<?> clazz,
                                                          java.lang.String... fieldNames)
        Get an array of Field's that matches the supplied list of field names.
        Parameters:
        clazz - The class that should contain the fields.
        fieldNames - The names of the fields that will be returned.
        Returns:
        An array of Field's. May be of length 0 but not null
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> type,
                                                         java.lang.String methodName,
                                                         java.lang.Class<?>... parameterTypes)
        Convenience method to get a method from a class type without having to catch the checked exceptions otherwise required. These exceptions are wrapped as runtime exceptions.

        The method will first try to look for a declared method in the same class. If the method is not declared in this class it will look for the method in the super class. This will continue throughout the whole class hierarchy. If the method is not found an IllegalArgumentException is thrown.

        Parameters:
        type - The type of the class where the method is located.
        methodName - The method names.
        parameterTypes - All parameter types of the method (may be null).
        Returns:
        A java.lang.reflect.Method.
        Throws:
        MethodNotFoundException - If a method cannot be found in the hierarchy.
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> type,
                                                         java.lang.Class<?>... parameterTypes)
        Convenience method to get a method from a class type without having to catch the checked exceptions otherwise required. These exceptions are wrapped as runtime exceptions.

        The method will first try to look for a declared method in the same class. If the method is not declared in this class it will look for the method in the super class. This will continue throughout the whole class hierarchy. If the method is not found an MethodNotFoundException is thrown. Since the method name is not specified an TooManyMethodsFoundException is thrown if two or more methods matches the same parameter types in the same class.

        Parameters:
        type - The type of the class where the method is located.
        parameterTypes - All parameter types of the method (may be null).
        Returns:
        A java.lang.reflect.Method.
        Throws:
        MethodNotFoundException - If a method cannot be found in the hierarchy.
        TooManyMethodsFoundException - If several methods were found.
      • newInstance

        public static <T> T newInstance​(java.lang.Class<T> classToInstantiate)
        Create a new instance of a class without invoking its constructor.

        No byte-code manipulation is needed to perform this operation and thus it's not necessary use the PowerMockRunner or PrepareForTest annotation to use this functionality.

        Type Parameters:
        T - The type of the instance to create.
        Parameters:
        classToInstantiate - The type of the instance to create.
        Returns:
        A new instance of type T, created without invoking the constructor.
      • getConstructor

        public static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<?> type,
                                                                          java.lang.Class<?>... parameterTypes)
        Convenience method to get a (declared) constructor from a class type without having to catch the checked exceptions otherwise required. These exceptions are wrapped as runtime exceptions. The constructor is also set to accessible.
        Parameters:
        type - The type of the class where the constructor is located.
        parameterTypes - All parameter types of the constructor (may be null).
        Returns:
        A java.lang.reflect.Constructor.
        Throws:
        ConstructorNotFoundException - if the constructor cannot be found.
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.String fieldName,
                                            java.lang.Object value)
        Set the value of a field using reflection. This method will traverse the super class hierarchy until a field with name fieldName is found.
        Parameters:
        object - the object whose field to modify
        fieldName - the name of the field
        value - the new value of the field
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.String fieldName,
                                            java.lang.Object[] value)
        Set an array value of a field using reflection. This method will traverse the super class hierarchy until a field with name fieldName is found.
        Parameters:
        object - the object to modify
        fieldName - the name of the field
        value - the new value of the field
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.Object value,
                                            java.lang.Object... additionalValues)
        Set the value of a field using reflection. This method will traverse the super class hierarchy until the first field assignable to the value type is found. The value (or additionaValues if present) will then be assigned to this field.
        Parameters:
        object - the object to modify
        value - the new value of the field
        additionalValues - Additional values to set on the object
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.Object value,
                                            java.lang.Class<?> where)
        Set the value of a field using reflection at at specific place in the class hierarchy (where). This first field assignable to object will then be set to value.
        Parameters:
        object - the object to modify
        value - the new value of the field
        where - the class in the hierarchy where the field is defined
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.String fieldName,
                                            java.lang.Object value,
                                            java.lang.Class<?> where)
        Set the value of a field using reflection. Use this method when you need to specify in which class the field is declared. This might be useful when you have mocked the instance you are trying to modify.
        Parameters:
        object - the object to modify
        fieldName - the name of the field
        value - the new value of the field
        where - the class in the hierarchy where the field is defined
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.Class<?> fieldType,
                                            java.lang.Object value)
        Set 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 will then be assigned to this field.
        Parameters:
        object - the object to modify
        fieldType - the type of the field
        value - the new value of the field
      • setInternalState

        public static void setInternalState​(java.lang.Object object,
                                            java.lang.Class<?> fieldType,
                                            java.lang.Object value,
                                            java.lang.Class<?> where)
        Set the value of a field using reflection at a specific location ( where) in the class hierarchy. The value will then be assigned to this field. The first field matching the fieldType in the hierarchy will be set.
        Parameters:
        object - the object to modify
        fieldType - the type of the field the should be set.
        value - the new value of the field
        where - which class in the hierarchy defining the field
      • 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 getInternalState(Object, String, Class).
        Parameters:
        object - the object to modify
        fieldName - the name of the field
      • getInternalState

        public static <T> T getInternalState​(java.lang.Object object,
                                             java.lang.String fieldName,
                                             java.lang.Class<?> where)
        Get the value of a field using reflection. Use this method when you need to specify in which class the field is declared. This might be useful when you have mocked the instance you are trying to access.
        Parameters:
        object - the object to modify
        fieldName - the name of the field
        where - which class the field is defined
      • getInternalState

        @Deprecated
        public static <T> T getInternalState​(java.lang.Object object,
                                             java.lang.String fieldName,
                                             java.lang.Class<?> where,
                                             java.lang.Class<T> type)
        Deprecated.
        Get the value of a field using reflection. Use this method when you need to specify in which class the field is declared. This might be useful when you have mocked the instance you are trying to access. Use this method to avoid casting.
        Type Parameters:
        T - the expected type of the field
        Parameters:
        object - the object to modify
        fieldName - the name of the field
        where - which class the field is defined
        type - the expected type of the field
      • getInternalState

        public static <T> T getInternalState​(java.lang.Object object,
                                             java.lang.Class<T> fieldType)
        Get the value of a field using reflection based on the fields type. 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.
        Parameters:
        object - the object to modify
        fieldType - the type of the field
      • getInternalState

        public static <T> T getInternalState​(java.lang.Object object,
                                             java.lang.Class<T> fieldType,
                                             java.lang.Class<?> where)
        Get the value of a field using reflection based on the field type. Use this method when you need to specify in which class the field is declared. The first field matching the fieldType in where is the field whose value will be returned.
        Type Parameters:
        T - the expected type of the field
        Parameters:
        object - the object to modify
        fieldType - the type of the field
        where - which class the field is defined
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object instance,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class method without the need to specify the method name. This is thus a more refactor friendly version of the invokeMethod(Object, String, Object...) method and is recommend over this method for that reason. This method might be useful to test private methods.
        Throws:
        java.lang.Throwable
        java.lang.Exception
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Class<?> klass,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class static method without the need to specify the method name. This is thus a more refactor friendly version of the invokeMethod(Class, String, Object...) method and is recommend over this method for that reason. This method might be useful to test private methods.
        Throws:
        java.lang.Exception
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object instance,
                                         java.lang.String methodToExecute,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class method. This might be useful to test private methods.
        Throws:
        java.lang.Exception
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object instance,
                                         java.lang.String methodToExecute,
                                         java.lang.Class<?>[] argumentTypes,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class method in cases where PowerMock cannot automatically determine the type of the parameters, for example when mixing primitive types and wrapper types in the same method. For most situations use invokeMethod(Object, Object...) instead.
        Throws:
        java.lang.Exception - Exception that may occur when invoking this method.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object instance,
                                         java.lang.String methodToExecute,
                                         java.lang.Class<?> definedIn,
                                         java.lang.Class<?>[] argumentTypes,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class method in a subclass (defined by definedIn) in cases where PowerMock cannot automatically determine the type of the parameters, for example when mixing primitive types and wrapper types in the same method. For most situations use invokeMethod(Object, Object...) instead.
        Throws:
        java.lang.Exception - Exception that may occur when invoking this method.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object instance,
                                         java.lang.Class<?> declaringClass,
                                         java.lang.String methodToExecute,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class method in that is located in a subclass of the instance. This might be useful to test private methods.
        Throws:
        java.lang.Exception - Exception that may occur when invoking this method.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object object,
                                         java.lang.Class<?> declaringClass,
                                         java.lang.String methodToExecute,
                                         java.lang.Class<?>[] parameterTypes,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a private or inner class method in that is located in a subclass of the instance. This might be useful to test private methods.

        Use this for overloaded methods.

        Throws:
        java.lang.Exception - Exception that may occur when invoking this method.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Class<?> clazz,
                                         java.lang.String methodToExecute,
                                         java.lang.Object... arguments)
                                  throws java.lang.Exception
        Invoke a static private or inner class method. This may be useful to test private methods.
        Throws:
        java.lang.Exception
      • invokeConstructor

        public static <T> T invokeConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest,
                                              java.lang.Class<?>[] parameterTypes,
                                              java.lang.Object[] arguments)
                                       throws java.lang.Exception
        Invoke a constructor. Useful for testing classes with a private constructor when PowerMock cannot determine which constructor to invoke. This only happens if you have two constructors with the same number of arguments where one is using primitive data types and the other is using the wrapped counter part. For example:
         public class MyClass {
             private MyClass(Integer i) {
                 ...
             } 
         
             private MyClass(int i) {
                 ...
             }
         
        This ought to be a really rare case. So for most situation, use invokeConstructor(Class, Object...) instead.
        Returns:
        The object created after the constructor has been invoked.
        Throws:
        java.lang.Exception - If an exception occur when invoking the constructor.
      • invokeConstructor

        public static <T> T invokeConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest,
                                              java.lang.Object... arguments)
                                       throws java.lang.Exception
        Invoke a constructor. Useful for testing classes with a private constructor.
        Returns:
        The object created after the constructor has been invoked.
        Throws:
        java.lang.Exception - If an exception occur when invoking the constructor.
      • getFirstParentConstructor

        public static java.lang.reflect.Constructor<?> getFirstParentConstructor​(java.lang.Class<?> klass)
        Get the first parent constructor defined in a super class of klass.
        Parameters:
        klass - The class where the constructor is located. null ).
        Returns:
        A java.lang.reflect.Constructor.
      • getMethods

        public static java.lang.reflect.Method[] getMethods​(java.lang.Class<?> clazz,
                                                            java.lang.String... methodNames)
        Get an array of Method's that matches the supplied list of method names. Both instance and static methods are taken into account.
        Parameters:
        clazz - The class that should contain the methods.
        methodNames - Names of the methods that will be returned.
        Returns:
        An array of Method's.
        Throws:
        MethodNotFoundException - If no method was found.
      • getType

        public static java.lang.Class<?> getType​(java.lang.Object object)
        Returns:
        The type of the of an object.
      • getFieldsAnnotatedWith

        public static java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith​(java.lang.Object object,
                                                                                    java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
                                                                                    java.lang.Class<? extends java.lang.annotation.Annotation>... additionalAnnotations)
        Get all fields 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.
        annotation - The annotation type to look for.
        additionalAnnotations - Optionally more annotations to look for. If any of the annotations are associated with a particular field it will be added to the resulting Set.
        Returns:
        A set of all fields containing the particular annotation.
      • getFieldsAnnotatedWith

        public static java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith​(java.lang.Object object,
                                                                                    java.lang.Class<? extends java.lang.annotation.Annotation>[] annotationTypes)
        Get all fields 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.
        annotationTypes - The annotation types to look for
        Returns:
        A set of all fields containing the particular annotation(s).
        Since:
        1.3
      • getAllInstanceFields

        public static java.util.Set<java.lang.reflect.Field> getAllInstanceFields​(java.lang.Object object)
        Get all instance fields for a particular object. It returns all fields regardless of the field modifier and regardless of where in the class hierarchy a field is located.
        Parameters:
        object - The object whose instance fields to get.
        Returns:
        All instance fields in the hierarchy. All fields are set to accessible
      • getAllStaticFields

        public static java.util.Set<java.lang.reflect.Field> getAllStaticFields​(java.lang.Class<?> type)
        Get all static fields for a particular type.
        Parameters:
        type - The class whose static fields to get.
        Returns:
        All static fields in type. All fields are set to accessible.
      • getFieldsOfType

        public static java.util.Set<java.lang.reflect.Field> getFieldsOfType​(java.lang.Object object,
                                                                             java.lang.Class<?> type)
        Get all fields assignable from a particular type. This method traverses the class hierarchy when checking for the type.
        Parameters:
        object - The object to look for type. Note that if're you're passing an object only instance fields are checked, passing a class will only check static fields.
        type - The type to look for.
        Returns:
        A set of all fields of the particular type.
      • getInnerClassType

        public static java.lang.Class<java.lang.Object> getInnerClassType​(java.lang.Class<?> declaringClass,
                                                                          java.lang.String name)
                                                                   throws java.lang.ClassNotFoundException
        Get an inner class type
        Parameters:
        declaringClass - The class in which the inner class is declared.
        name - The unqualified name (simple name) of the inner class.
        Returns:
        The type.
        Throws:
        java.lang.ClassNotFoundException
      • getLocalClassType

        public static java.lang.Class<java.lang.Object> getLocalClassType​(java.lang.Class<?> declaringClass,
                                                                          int occurrence,
                                                                          java.lang.String name)
                                                                   throws java.lang.ClassNotFoundException
        Get the type of a local inner class.
        Parameters:
        declaringClass - The class in which the local inner class is declared.
        occurrence - The occurrence of the local class. For example if you have two local classes in the declaringClass you must pass in 1 if you want to get the type for the first one or 2 if you want the second one.
        name - The unqualified name (simple name) of the local class.
        Returns:
        The type.
        Throws:
        java.lang.ClassNotFoundException
      • getAnonymousInnerClassType

        public static java.lang.Class<java.lang.Object> getAnonymousInnerClassType​(java.lang.Class<?> declaringClass,
                                                                                   int occurrence)
                                                                            throws java.lang.ClassNotFoundException
        Get the type of an anonymous inner class.
        Parameters:
        declaringClass - The class in which the anonymous inner class is declared.
        occurrence - The occurrence of the anonymous inner class. For example if you have two anonymous inner classes classes in the declaringClass you must pass in 1 if you want to get the type for the first one or 2 if you want the second one.
        Returns:
        The type.
        Throws:
        java.lang.ClassNotFoundException
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object instance,
                                                       java.lang.Object context,
                                                       java.lang.Object... additionalContexts)
        Set the values of multiple instance fields defined in a context using reflection. The values in the context will be assigned to values on the instance. This method will traverse the class hierarchy when searching for the fields. Example usage:

        Given:

         public class MyContext {
                private String myString = "myString";
                protected int myInt = 9;
         }
         
         public class MyInstance {
                private String myInstanceString;
                private int myInstanceInt;
         
         }
         
        then
         Whitebox.setInternalStateFromContext(new MyInstance(), new MyContext());
         
        will set the instance variables of myInstance to the values specified in MyContext.

        By default the FieldMatchingStrategy.MATCHING strategy is used which means that the fields defined in the context but not found in the classOrInstance are silently ignored.

        Parameters:
        instance - the object whose fields to modify.
        context - The context where the fields are defined.
        additionalContexts - Optionally more additional contexts.
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object classOrInstance,
                                                       java.lang.Class<?> context,
                                                       java.lang.Class<?>... additionalContexts)
        Set the values of multiple static fields defined in a context using reflection. The values in the context will be assigned to values on the classOrInstance. This method will traverse the class hierarchy when searching for the fields. Example usage:

        Given:

         public class MyContext {
                private static String myString = "myString";
                protected static int myInt = 9;
         }
         
         public class MyInstance {
                private static String myInstanceString;
                private static int myInstanceInt;
         
         }
         
        then
         Whitebox.setInternalStateFromContext(MyInstance.class, MyContext.class);
         
        will set the static variables of MyInstance to the values specified in MyContext.

        By default the FieldMatchingStrategy.MATCHING strategy is used which means that the fields defined in the context but not found in the classOrInstance are silently ignored.

        Parameters:
        classOrInstance - The object or class whose fields to set.
        context - The context where the fields are defined.
        additionalContexts - Optionally more additional contexts.
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object instance,
                                                       java.lang.Object context,
                                                       FieldMatchingStrategy strategy)
        Set the values of multiple instance fields defined in a context using reflection and using an explicit FieldMatchingStrategy. The values in the context will be assigned to values on the instance. This method will traverse the class hierarchy when searching for the fields. Example usage:

        Given:

         public class MyContext {
                private String myString = "myString";
                protected int myInt = 9;
         }
         
         public class MyInstance {
                private String myInstanceString;
                private int myInstanceInt;
         
         }
         
        then
         Whitebox.setInternalStateFromContext(new MyInstance(), new MyContext());
         
        will set the instance variables of myInstance to the values specified in MyContext.
        Parameters:
        instance - the object whose fields to modify.
        context - The context where the fields are defined.
        strategy - Which field matching strategy to use.
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object instance,
                                                       java.lang.Class<?> context,
                                                       FieldMatchingStrategy strategy)
        Set the values of multiple static fields defined in a context using reflection and using an explicit FieldMatchingStrategy. The values in the context will be assigned to values on the classOrInstance. This method will traverse the class hierarchy when searching for the fields. Example usage:

        Given:

         public class MyContext {
                private static String myString = "myString";
                protected static int myInt = 9;
         }
         
         public class MyInstance {
                private static String myInstanceString;
                private static int myInstanceInt;
         
         }
         
        then
         Whitebox.setInternalStateFromContext(MyInstance.class, MyContext.class);
         
        will set the static variables of MyInstance to the values specified in MyContext.

        Parameters:
        instance - The object or class whose fields to set.
        context - The context where the fields are defined.
        strategy - Which field matching strategy to use.