Class WhiteboxImpl


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

      Fields 
      Modifier and Type Field Description
      private static java.util.concurrent.ConcurrentMap<java.lang.Class,​java.lang.reflect.Method[]> allClassMethodsCache
      "Strong" map prevent class and method objects from being GCed and unloaded.
      private static ProxyFrameworks proxyFrameworks
      The proxy framework.
    • Constructor Summary

      Constructors 
      Constructor Description
      WhiteboxImpl()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void appendArgument​(java.lang.StringBuilder argumentsAsString, int index, java.lang.String argumentName, java.lang.Object[] arguments)
      Append argument.
      (package private) static boolean areAllArgumentsOfSameType​(java.lang.Object[] arguments)
      Check if all arguments are of the same type.
      static boolean areAllMethodsStatic​(java.lang.reflect.Method... methods)
      Are all methods static.
      private static boolean argumentTypesEqualsPrimitiveTypes​(java.lang.Class<?>[] argumentTypes, java.lang.Class<?>[] primitiveType)  
      private static void assertObjectInGetInternalStateIsNotNull​(java.lang.Object object)
      Assert object in get internal state is not null.
      (package private) static boolean checkArgumentTypesMatchParameterTypes​(boolean isVarArgs, java.lang.Class<?>[] parameterTypes, java.lang.Object[] arguments)
      Check argument types match parameter types.
      static boolean checkIfParameterTypesAreSame​(boolean isVarArgs, java.lang.Class<?>[] expectedParameterTypes, java.lang.Class<?>[] actualParameterTypes)
      Check if parameter types are same.
      private static java.lang.String concatenateStrings​(java.lang.String... stringsToConcatenate)
      Concatenate strings.
      private static java.lang.Class<?>[] convertArgumentTypesToPrimitive​(java.lang.Class<?>[] paramTypes, java.lang.Object[] arguments)
      Convert argument types to primitive.
      private static java.lang.Class<?>[] convertParameterTypesToPrimitive​(java.lang.Class<?>[] parameterTypes)
      Convert parameter types to primitive.
      private static <T> void copy​(T from, T to, java.lang.Class<?> fromClazz)  
      (package private) static void copyState​(java.lang.Object object, java.lang.Object context, FieldMatchingStrategy strategy)
      Copy state.
      static <T> void copyToMock​(T from, T mock)  
      static <T> void copyToRealObject​(T from, T to)  
      private static <T> void copyValue​(T from, T to, java.lang.reflect.Field field)  
      private static <T> void copyValues​(T from, T mock, java.lang.Class<?> classFrom)  
      private static java.lang.Object createAndPopulateVarArgsArray​(java.lang.Class<?> varArgsType, int varArgsStartPosition, java.lang.Object... arguments)
      Creates the and populate var args array.
      private static <T> T createInstance​(java.lang.reflect.Constructor<T> constructor, java.lang.Object... arguments)
      Creates the instance.
      (package private) static boolean doesParameterTypesMatchForVarArgsInvocation​(boolean isVarArgs, java.lang.Class<?>[] parameterTypes, java.lang.Object[] arguments)
      Does parameter types match for var args invocation.
      private static java.lang.reflect.Method[] doGetAllMethods​(java.lang.Class<?> clazz)  
      private static <T> T doInvokeMethod​(java.lang.Object tested, java.lang.Class<?> declaringClass, java.lang.String methodToExecute, java.lang.Object... arguments)
      Do invoke method.
      (package private) static java.lang.reflect.Constructor<?>[] filterPowerMockConstructor​(java.lang.reflect.Constructor<?>[] declaredConstructors)
      Filter power mock constructor.
      private static java.util.Set<java.lang.reflect.Field> findAllFieldsUsingStrategy​(FieldMatcherStrategy strategy, java.lang.Object object, boolean checkHierarchy, boolean onlyInstanceFields, java.lang.Class<?> startClass)
      Find all fields using strategy.
      static java.lang.reflect.Constructor<?> findConstructorOrThrowException​(java.lang.Class<?> type)
      Finds and returns any constructor.
      static java.lang.reflect.Constructor<?> findDefaultConstructorOrThrowException​(java.lang.Class<?> type)
      Finds and returns the default constructor.
      private static java.lang.reflect.Field findField​(java.lang.Object object, FieldMatcherStrategy strategy, java.lang.Class<?> where)
      Find field.
      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 findFieldOrThrowException​(java.lang.Class<?> fieldType, java.lang.Class<?> where)
      Find field or throw exception.
      static <T> java.lang.reflect.Method findMethod​(java.lang.Class<T> type, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Finds and returns a method based on the input parameters.
      static java.lang.reflect.Method findMethodOrThrowException​(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Find method or throw exception.
      static java.lang.reflect.Method findMethodOrThrowException​(java.lang.Object tested, java.lang.Class<?> declaringClass, java.lang.String methodToExecute, java.lang.Object[] arguments)
      Finds and returns a certain method.
      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 java.lang.reflect.Constructor<?> findUniqueConstructorOrThrowException​(java.lang.Class<?> type, java.lang.Object... arguments)
      Finds and returns a certain constructor.
      static java.lang.reflect.Constructor<?>[] getAllConstructors​(java.lang.Class<?> clazz)
      Get all declared constructors in the class and set accessible to true.
      static java.lang.reflect.Field[] getAllFields​(java.lang.Class<?> clazz)
      Get all fields in a class hierarchy! Both declared an non-declared (no duplicates).
      static java.util.Set<java.lang.reflect.Field> getAllInstanceFields​(java.lang.Object object)
      Get all instance fields for a particular object.
      static <T> java.lang.reflect.Method[] getAllMethodExcept​(java.lang.Class<T> type, java.lang.String... methodNames)
      Gets the all method except.
      static java.lang.reflect.Method[] getAllMethods​(java.lang.Class<?> clazz)
      Get all methods in a class hierarchy! Both declared an non-declared (no duplicates).
      static <T> java.lang.reflect.Method[] getAllMethodsExcept​(java.lang.Class<T> type, java.lang.String methodNameToExclude, java.lang.Class<?>[] argumentTypes)
      Gets the all metods except.
      private static java.lang.reflect.Method[] getAllPublicMethods​(java.lang.Class<?> clazz)
      Get all public methods for a class (no duplicates)! Note that the class-hierarchy will not be traversed.
      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.
      (package private) static java.lang.String getArgumentTypesAsString​(java.lang.Object... arguments)
      Gets the argument types as string.
      private static <T> java.lang.reflect.Constructor<T> getBestCandidateConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Class<?>[] argumentTypes, java.lang.Object[] arguments)  
      static java.lang.reflect.Method getBestMethodCandidate​(java.lang.Class<?> cls, java.lang.String methodName, java.lang.Class<?>[] signature, boolean exactParameterTypeMatch)
      Gets the best method candidate.
      private static java.lang.Class<?> getComponentType​(java.lang.Class<?> type)  
      static java.lang.reflect.Constructor<?> 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.
      private static java.lang.reflect.Field getField​(java.lang.String fieldName, java.lang.Class<?> where)
      Gets the field.
      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.
      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.
      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 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.reflect.Method[] getMethods​(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>[] expectedTypes, boolean exactParameterTypeMatch)
      Get an array of Method's that matches the method name and whose argument types are assignable from expectedTypes.
      private static java.lang.reflect.Method getMethodWithMostSpecificParameterTypes​(java.lang.reflect.Method firstMethodCandidate, java.lang.reflect.Method secondMethodCandidate)
      Find the method whose parameter types most closely matches the types.
      static <T> java.lang.Class<?> getOriginalUnmockedType​(java.lang.Class<T> type)
      Gets the unmocked type.
      private static <T> java.lang.reflect.Constructor<T> getPotentialConstructorPrimitive​(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Class<?>[] argumentTypes)  
      private static <T> java.lang.reflect.Constructor<T> getPotentialConstructorWrapped​(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Class<?>[] argumentTypes)  
      private static <T> java.lang.reflect.Constructor<T> getPotentialVarArgsConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Object... arguments)
      Gets the potential var args constructor.
      static java.lang.Class<?> getType​(java.lang.Object object)
      Gets the type.
      (package private) static java.lang.Class<?> getTypeAsPrimitiveIfWrapped​(java.lang.Object object)
      Get the type of an object and convert it to primitive if the type has a primitive counter-part.
      private static java.lang.Class<?>[] getTypes​(java.lang.Object[] arguments)
      Gets the types.
      static <T> UnproxiedType getUnproxiedType​(java.lang.Class<T> type)  
      static java.lang.Class<?> getUnproxyType​(java.lang.Object object)
      Gets the type.
      private static sun.misc.Unsafe getUnsafe()  
      private static boolean hasFieldProperModifier​(java.lang.Object object, java.lang.reflect.Field field, boolean onlyInstanceFields)
      Checks for field proper modifier.
      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<?> tested, 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.Class<?> clazz, java.lang.String methodToExecute, java.lang.Object... arguments)
      Invoke a 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 method in that is located in a subclass of an instance.
      static <T> T invokeMethod​(java.lang.Object tested, 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 tested instance.
      static <T> T invokeMethod​(java.lang.Object tested, 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 tested, java.lang.String methodToExecute, java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments)
      Invoke a private or inner class method in cases where power mock 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 tested, 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 power mock 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 tested, java.lang.String methodToExecute, java.lang.Object... arguments)
      Invoke a private or inner class method.
      (package private) static boolean isAssignableFrom​(java.lang.Class<?> type, java.lang.Class<?> from)  
      static boolean isClass​(java.lang.Object argument)
      Checks if is class.
      private static boolean isPotentialVarArgsMethod​(java.lang.reflect.Method method, java.lang.Object[] arguments)
      Checks if is potential var args method.
      static <T> T newInstance​(java.lang.Class<T> classToInstantiate)
      Create a new instance of a class without invoking its constructor.
      static <T> T performMethodInvocation​(java.lang.Object tested, java.lang.reflect.Method methodToInvoke, java.lang.Object... arguments)
      Perform method invocation.
      private static void setField​(java.lang.Object object, java.lang.Object value, java.lang.reflect.Field foundField)
      Sets the field.
      private static void setFieldUsingUnsafe​(java.lang.Object base, java.lang.Class type, long offset, java.lang.Object newValue, sun.misc.Unsafe unsafe)  
      private static void setFieldUsingUnsafe​(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object newValue)  
      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 the 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 object, 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 object, 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 object, java.lang.Object context, FieldMatchingStrategy strategy)  
      private static void setStaticFieldUsingUnsafe​(java.lang.reflect.Field field, java.lang.Object newValue)  
      (package private) static void throwExceptionIfConstructorWasNotFound​(java.lang.Class<?> type, java.lang.reflect.Constructor<?> potentialConstructor, java.lang.Object... arguments)
      Throw exception if constructor was not found.
      static void throwExceptionIfFieldWasNotFound​(java.lang.Class<?> type, java.lang.String fieldName, java.lang.reflect.Field field)
      Throw exception if field was not found.
      static void throwExceptionIfMethodWasNotFound​(java.lang.Class<?> type, java.lang.String methodName, java.lang.reflect.Method methodToMock, java.lang.Object... arguments)
      Throw exception if method was not found.
      (package private) static void throwExceptionWhenMultipleConstructorMatchesFound​(java.lang.reflect.Constructor<?>[] constructors)
      Throw exception when multiple constructor matches found.
      (package private) static void throwExceptionWhenMultipleMethodMatchesFound​(java.lang.String helpInfo, java.lang.reflect.Method[] methods)
      Throw exception when multiple method matches found.
      private static java.lang.Class<?> toBoxedIfPrimitive​(java.lang.Class<?> type)  
      • Methods inherited from class java.lang.Object

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

      • proxyFrameworks

        private static ProxyFrameworks proxyFrameworks
        The proxy framework.
      • allClassMethodsCache

        private static java.util.concurrent.ConcurrentMap<java.lang.Class,​java.lang.reflect.Method[]> allClassMethodsCache
        "Strong" map prevent class and method objects from being GCed and unloaded. TODO replace with ClassValue when Powermock drops Java 6 support.
    • Constructor Detail

      • WhiteboxImpl

        public WhiteboxImpl()
    • Method Detail

      • 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
        Parameters:
        type - The type of the class where the method is located.
        parameterTypes - All parameter types of the method (may be null).
        Returns:
        A . TooManyMethodsFoundException is thrown if two or more methods matches the same parameter types in the same class.
      • 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 .
      • 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 method 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 .
      • 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 java.lang.reflect.Constructor<?> 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 .
      • 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 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 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.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.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.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.
        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
      • 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 is useful if you have two fields in a class hierarchy that has the same name but you like to modify the latter.
        Parameters:
        object - the object to modify
        fieldName - the name of the field
        value - the new value of the field
        where - which class the field is defined
      • 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 getInternalState(Object, String, Class).
      • 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
      • findField

        private static java.lang.reflect.Field findField​(java.lang.Object object,
                                                         FieldMatcherStrategy strategy,
                                                         java.lang.Class<?> where)
        Find field.
        Parameters:
        object - the object
        strategy - the strategy
        where - the where
        Returns:
        the field
      • 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
      • findAllFieldsUsingStrategy

        private static java.util.Set<java.lang.reflect.Field> findAllFieldsUsingStrategy​(FieldMatcherStrategy strategy,
                                                                                         java.lang.Object object,
                                                                                         boolean checkHierarchy,
                                                                                         boolean onlyInstanceFields,
                                                                                         java.lang.Class<?> startClass)
        Find all fields using strategy.
        Parameters:
        strategy - the strategy
        object - the object
        checkHierarchy - the check hierarchy
        onlyInstanceFields - whether to only allow instance fields
        startClass - the start class
        Returns:
        the set
      • hasFieldProperModifier

        private static boolean hasFieldProperModifier​(java.lang.Object object,
                                                      java.lang.reflect.Field field,
                                                      boolean onlyInstanceFields)
        Checks for field proper modifier.
        Parameters:
        object - the object
        field - the field
        onlyInstanceFields - whether to only allow instance fields
        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
      • 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. Use this method when you need to specify in which class the field is declared. The first field matching the fieldType in where will 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
        Returns:
        the internal state
      • 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. 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
        Returns:
        the internal state
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object tested,
                                         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
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception invokeMethod(Object, String, Object...) method and is recommend over this method for that reason. This method might be useful to test private methods.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Class<?> tested,
                                         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
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception invokeMethod(Object, String, Object...) method and is recommend over this method for that reason. This method might be useful to test private methods.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object tested,
                                         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.
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        methodToExecute - the method to execute
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object tested,
                                         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 power mock 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(Class, String, Object...) instead.
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        methodToExecute - the method to execute
        argumentTypes - the argument types
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - Exception that may occur when invoking this method.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object tested,
                                         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 power mock 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
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        methodToExecute - the method to execute
        definedIn - the defined in
        argumentTypes - the argument types
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - Exception that may occur when invoking this method. invokeMethod(Class, String, Object...) instead.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object tested,
                                         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 tested instance. This might be useful to test private methods.
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        declaringClass - the declaring class
        methodToExecute - the method to execute
        arguments - the arguments
        Returns:
        the t
        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 method in that is located in a subclass of an instance. This might be useful to test overloaded private methods. Use this for overloaded methods only, if possible use
        Type Parameters:
        T - the generic type
        Parameters:
        object - the object
        declaringClass - the declaring class
        methodToExecute - the method to execute
        parameterTypes - the parameter types
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - Exception that may occur when invoking this method. invokeMethod(Object, Object...) or invokeMethod(Object, String, Object...) instead.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Class<?> clazz,
                                         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.
        Type Parameters:
        T - the generic type
        Parameters:
        clazz - the clazz
        methodToExecute - the method to execute
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception
      • doInvokeMethod

        private static <T> T doInvokeMethod​(java.lang.Object tested,
                                            java.lang.Class<?> declaringClass,
                                            java.lang.String methodToExecute,
                                            java.lang.Object... arguments)
                                     throws java.lang.Exception
        Do invoke method.
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        declaringClass - the declaring class
        methodToExecute - the method to execute
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception
      • findMethodOrThrowException

        public static java.lang.reflect.Method findMethodOrThrowException​(java.lang.Object tested,
                                                                          java.lang.Class<?> declaringClass,
                                                                          java.lang.String methodToExecute,
                                                                          java.lang.Object[] arguments)
        Finds and returns a certain method. If the method couldn't be found this method delegates to
        Parameters:
        tested - The instance or class containing the method.
        declaringClass - The class where the method is supposed to be declared (may be null).
        methodToExecute - The method name. If null then method will be looked up based on the argument types only.
        arguments - The arguments of the methods.
        Returns:
        A single method.
        Throws:
        MethodNotFoundException - if no method was found.
        TooManyMethodsFoundException - if too methods matched.
        java.lang.IllegalArgumentException - if tested is null.
      • getMethodWithMostSpecificParameterTypes

        private static java.lang.reflect.Method getMethodWithMostSpecificParameterTypes​(java.lang.reflect.Method firstMethodCandidate,
                                                                                        java.lang.reflect.Method secondMethodCandidate)
        Find the method whose parameter types most closely matches the types.
        Parameters:
        firstMethodCandidate - The first method candidate
        secondMethodCandidate - The second method candidate
        Returns:
        The method that most closely matches the provided types or null if no method match.
      • toBoxedIfPrimitive

        private static java.lang.Class<?> toBoxedIfPrimitive​(java.lang.Class<?> type)
      • getTypes

        private static java.lang.Class<?>[] getTypes​(java.lang.Object[] arguments)
        Gets the types.
        Parameters:
        arguments - the arguments
        Returns:
        the types
      • getBestMethodCandidate

        public static java.lang.reflect.Method getBestMethodCandidate​(java.lang.Class<?> cls,
                                                                      java.lang.String methodName,
                                                                      java.lang.Class<?>[] signature,
                                                                      boolean exactParameterTypeMatch)
        Gets the best method candidate.
        Parameters:
        cls - the cls
        methodName - the method name
        signature - the signature
        exactParameterTypeMatch - true if the expectedTypes must match the parameter types must match exactly, false if the expectedTypes are allowed to be converted into primitive types if they are of a wrapped type and still match.
        Returns:
        the best method candidate
      • filterPowerMockConstructor

        static java.lang.reflect.Constructor<?>[] filterPowerMockConstructor​(java.lang.reflect.Constructor<?>[] declaredConstructors)
        Filter power mock constructor.
        Parameters:
        declaredConstructors - the declared constructors
        Returns:
        the constructor[]
      • findUniqueConstructorOrThrowException

        public static java.lang.reflect.Constructor<?> findUniqueConstructorOrThrowException​(java.lang.Class<?> type,
                                                                                             java.lang.Object... arguments)
        Finds and returns a certain constructor. If the constructor couldn't be found this method delegates to
        Parameters:
        type - The type where the constructor should be located.
        arguments - The arguments passed to the constructor.
        Returns:
        The found constructor.
        Throws:
        ConstructorNotFoundException - if no constructor was found.
        TooManyConstructorsFoundException - if too constructors matched.
        java.lang.IllegalArgumentException - if type is null.
      • convertArgumentTypesToPrimitive

        private static java.lang.Class<?>[] convertArgumentTypesToPrimitive​(java.lang.Class<?>[] paramTypes,
                                                                            java.lang.Object[] arguments)
        Convert argument types to primitive.
        Parameters:
        paramTypes - the param types
        arguments - the arguments
        Returns:
        the class[]
      • throwExceptionIfMethodWasNotFound

        public static void throwExceptionIfMethodWasNotFound​(java.lang.Class<?> type,
                                                             java.lang.String methodName,
                                                             java.lang.reflect.Method methodToMock,
                                                             java.lang.Object... arguments)
        Throw exception if method was not found.
        Parameters:
        type - the type
        methodName - the method name
        methodToMock - the method to mock
        arguments - the arguments
      • 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
      • throwExceptionIfConstructorWasNotFound

        static void throwExceptionIfConstructorWasNotFound​(java.lang.Class<?> type,
                                                           java.lang.reflect.Constructor<?> potentialConstructor,
                                                           java.lang.Object... arguments)
        Throw exception if constructor was not found.
        Parameters:
        type - the type
        potentialConstructor - the potential constructor
        arguments - the arguments
      • getArgumentTypesAsString

        static java.lang.String getArgumentTypesAsString​(java.lang.Object... arguments)
        Gets the argument types as string.
        Parameters:
        arguments - the arguments
        Returns:
        the argument types as string
      • appendArgument

        private static void appendArgument​(java.lang.StringBuilder argumentsAsString,
                                           int index,
                                           java.lang.String argumentName,
                                           java.lang.Object[] arguments)
        Append argument.
        Parameters:
        argumentsAsString - the arguments as string
        index - the index
        argumentName - the argument name
        arguments - the arguments
      • 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
        Type Parameters:
        T - the generic type
        Parameters:
        classThatContainsTheConstructorToTest - the class that contains the constructor to test
        parameterTypes - the parameter types
        arguments - the arguments
        Returns:
        The object created after the constructor has been invoked.
        Throws:
        java.lang.Exception - If an exception occur when invoking the constructor. invokeConstructor(Class, Object...) instead.
      • 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.
        Type Parameters:
        T - the generic type
        Parameters:
        classThatContainsTheConstructorToTest - the class that contains the constructor to test
        arguments - the arguments
        Returns:
        The object created after the constructor has been invoked.
        Throws:
        java.lang.Exception - If an exception occur when invoking the constructor.
      • getBestCandidateConstructor

        private static <T> java.lang.reflect.Constructor<T> getBestCandidateConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest,
                                                                                        java.lang.Class<?>[] argumentTypes,
                                                                                        java.lang.Object[] arguments)
      • getPotentialConstructorWrapped

        private static <T> java.lang.reflect.Constructor<T> getPotentialConstructorWrapped​(java.lang.Class<T> classThatContainsTheConstructorToTest,
                                                                                           java.lang.Class<?>[] argumentTypes)
      • getPotentialConstructorPrimitive

        private static <T> java.lang.reflect.Constructor<T> getPotentialConstructorPrimitive​(java.lang.Class<T> classThatContainsTheConstructorToTest,
                                                                                             java.lang.Class<?>[] argumentTypes)
      • argumentTypesEqualsPrimitiveTypes

        private static boolean argumentTypesEqualsPrimitiveTypes​(java.lang.Class<?>[] argumentTypes,
                                                                 java.lang.Class<?>[] primitiveType)
      • getPotentialVarArgsConstructor

        private static <T> java.lang.reflect.Constructor<T> getPotentialVarArgsConstructor​(java.lang.Class<T> classThatContainsTheConstructorToTest,
                                                                                           java.lang.Object... arguments)
        Gets the potential var args constructor.
        Type Parameters:
        T - the generic type
        Parameters:
        classThatContainsTheConstructorToTest - the class that contains the constructor to test
        arguments - the arguments
        Returns:
        the potential var args constructor
      • createInstance

        private static <T> T createInstance​(java.lang.reflect.Constructor<T> constructor,
                                            java.lang.Object... arguments)
                                     throws java.lang.Exception
        Creates the instance.
        Type Parameters:
        T - the generic type
        Parameters:
        constructor - the constructor
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception
      • createAndPopulateVarArgsArray

        private static java.lang.Object createAndPopulateVarArgsArray​(java.lang.Class<?> varArgsType,
                                                                      int varArgsStartPosition,
                                                                      java.lang.Object... arguments)
        Creates the and populate var args array.
        Parameters:
        varArgsType - the var args type
        varArgsStartPosition - the var args start position
        arguments - the arguments
        Returns:
        the object
      • getAllConstructors

        public static java.lang.reflect.Constructor<?>[] getAllConstructors​(java.lang.Class<?> clazz)
        Get all declared constructors in the class and set accessible to true.
        Parameters:
        clazz - The class whose constructors to get.
        Returns:
        All constructors declared in this class hierarchy.
      • getAllMethods

        public static java.lang.reflect.Method[] getAllMethods​(java.lang.Class<?> clazz)
        Get all methods in a class hierarchy! Both declared an non-declared (no duplicates).
        Parameters:
        clazz - The class whose methods to get.
        Returns:
        All methods declared in this class hierarchy.
      • doGetAllMethods

        private static java.lang.reflect.Method[] doGetAllMethods​(java.lang.Class<?> clazz)
      • getAllPublicMethods

        private static java.lang.reflect.Method[] getAllPublicMethods​(java.lang.Class<?> clazz)
        Get all public methods for a class (no duplicates)! Note that the class-hierarchy will not be traversed.
        Parameters:
        clazz - The class whose methods to get.
        Returns:
        All public methods declared in class.
      • getAllFields

        public static java.lang.reflect.Field[] getAllFields​(java.lang.Class<?> clazz)
        Get all fields in a class hierarchy! Both declared an non-declared (no duplicates).
        Parameters:
        clazz - The class whose fields to get.
        Returns:
        All fields declared in this class hierarchy.
      • 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 .
      • findMethod

        public static <T> java.lang.reflect.Method findMethod​(java.lang.Class<T> type,
                                                              java.lang.String methodName,
                                                              java.lang.Class<?>... parameterTypes)
        Finds and returns a method based on the input parameters. If no parameterTypes are present the method will return the first method with name methodNameToMock. If no method was found, null will be returned. If no methodName is specified the method will be found based on the parameter types. If neither method name nor parameters are specified an
        Type Parameters:
        T - the generic type
        Parameters:
        type - the type
        methodName - the method name
        parameterTypes - the parameter types
        Returns:
        the method IllegalArgumentException will be thrown.
      • getOriginalUnmockedType

        public static <T> java.lang.Class<?> getOriginalUnmockedType​(java.lang.Class<T> type)
        Gets the unmocked type.
        Type Parameters:
        T - the generic type
        Parameters:
        type - the type
        Returns:
        the unmocked type
      • getUnproxiedType

        public static <T> UnproxiedType getUnproxiedType​(java.lang.Class<T> type)
      • throwExceptionWhenMultipleMethodMatchesFound

        static void throwExceptionWhenMultipleMethodMatchesFound​(java.lang.String helpInfo,
                                                                 java.lang.reflect.Method[] methods)
        Throw exception when multiple method matches found.
        Parameters:
        helpInfo - the help info
        methods - the methods
      • throwExceptionWhenMultipleConstructorMatchesFound

        static void throwExceptionWhenMultipleConstructorMatchesFound​(java.lang.reflect.Constructor<?>[] constructors)
        Throw exception when multiple constructor matches found.
        Parameters:
        constructors - the constructors
      • findMethodOrThrowException

        public static java.lang.reflect.Method findMethodOrThrowException​(java.lang.Class<?> type,
                                                                          java.lang.String methodName,
                                                                          java.lang.Class<?>... parameterTypes)
        Find method or throw exception.
        Parameters:
        type - the type
        methodName - the method name
        parameterTypes - the parameter types
        Returns:
        the method
      • 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.
      • getMethods

        public static java.lang.reflect.Method[] getMethods​(java.lang.Class<?> clazz,
                                                            java.lang.String methodName,
                                                            java.lang.Class<?>[] expectedTypes,
                                                            boolean exactParameterTypeMatch)
        Get an array of Method's that matches the method name and whose argument types are assignable from expectedTypes. Both instance and static methods are taken into account.
        Parameters:
        clazz - The class that should contain the methods.
        methodName - Names of the methods that will be returned.
        expectedTypes - The methods must match
        exactParameterTypeMatch - true if the expectedTypes must match the parameter types must match exactly, false if the expectedTypes are allowed to be converted into primitive types if they are of a wrapped type and still match.
        Returns:
        An array of Method's.
      • 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. Both instance and static fields are taken into account.
        Parameters:
        clazz - The class that should contain the fields.
        fieldNames - Names of the fields that will be returned.
        Returns:
        An array of Field's. May be of length 0 but not .
      • performMethodInvocation

        public static <T> T performMethodInvocation​(java.lang.Object tested,
                                                    java.lang.reflect.Method methodToInvoke,
                                                    java.lang.Object... arguments)
                                             throws java.lang.Exception
        Perform method invocation.
        Type Parameters:
        T - the generic type
        Parameters:
        tested - the tested
        methodToInvoke - the method to invoke
        arguments - the arguments
        Returns:
        the t
        Throws:
        java.lang.Exception - the exception
      • getAllMethodExcept

        public static <T> java.lang.reflect.Method[] getAllMethodExcept​(java.lang.Class<T> type,
                                                                        java.lang.String... methodNames)
        Gets the all method except.
        Type Parameters:
        T - the generic type
        Parameters:
        type - the type
        methodNames - the method names
        Returns:
        the all method except
      • getAllMethodsExcept

        public static <T> java.lang.reflect.Method[] getAllMethodsExcept​(java.lang.Class<T> type,
                                                                         java.lang.String methodNameToExclude,
                                                                         java.lang.Class<?>[] argumentTypes)
        Gets the all metods except.
        Type Parameters:
        T - the generic type
        Parameters:
        type - the type
        methodNameToExclude - the method name to exclude
        argumentTypes - the argument types
        Returns:
        the all metods except
      • areAllMethodsStatic

        public static boolean areAllMethodsStatic​(java.lang.reflect.Method... methods)
        Are all methods static.
        Parameters:
        methods - the methods
        Returns:
        true, if successful
      • areAllArgumentsOfSameType

        static boolean areAllArgumentsOfSameType​(java.lang.Object[] arguments)
        Check if all arguments are of the same type.
        Parameters:
        arguments - the arguments
        Returns:
        true, if successful
      • checkArgumentTypesMatchParameterTypes

        static boolean checkArgumentTypesMatchParameterTypes​(boolean isVarArgs,
                                                             java.lang.Class<?>[] parameterTypes,
                                                             java.lang.Object[] arguments)
        Check argument types match parameter types.
        Parameters:
        isVarArgs - If the last parameter is a var args.
        parameterTypes - the parameter types
        arguments - the arguments
        Returns:
        if all actual parameter types are assignable from the expected arguments, otherwise.
      • isAssignableFrom

        static boolean isAssignableFrom​(java.lang.Class<?> type,
                                        java.lang.Class<?> from)
      • getComponentType

        private static java.lang.Class<?> getComponentType​(java.lang.Class<?> type)
      • 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.
      • getUnproxyType

        public static java.lang.Class<?> getUnproxyType​(java.lang.Object object)
        Gets the type.
        Parameters:
        object - the object
        Returns:
        The type of the of an object.
      • 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 - the class not found exception
      • 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 - the class not found exception
      • 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 - the class not found exception
      • 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
      • 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.
      • 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. All fields are set to accessible.
      • isClass

        public static boolean isClass​(java.lang.Object argument)
        Checks if is class.
        Parameters:
        argument - the argument
        Returns:
        true, if is class
      • checkIfParameterTypesAreSame

        public static boolean checkIfParameterTypesAreSame​(boolean isVarArgs,
                                                           java.lang.Class<?>[] expectedParameterTypes,
                                                           java.lang.Class<?>[] actualParameterTypes)
        Check if parameter types are same.
        Parameters:
        isVarArgs - Whether or not the method or constructor contains var args.
        expectedParameterTypes - the expected parameter types
        actualParameterTypes - the actual parameter types
        Returns:
        if all actual parameter types are assignable from the expected parameter types, otherwise.
      • getField

        private static java.lang.reflect.Field getField​(java.lang.String fieldName,
                                                        java.lang.Class<?> where)
        Gets the field.
        Parameters:
        fieldName - the field name
        where - the where
        Returns:
        the field
      • findFieldOrThrowException

        private static java.lang.reflect.Field findFieldOrThrowException​(java.lang.Class<?> fieldType,
                                                                         java.lang.Class<?> where)
        Find field or throw exception.
        Parameters:
        fieldType - the field type
        where - the where
        Returns:
        the field
      • setField

        private static void setField​(java.lang.Object object,
                                     java.lang.Object value,
                                     java.lang.reflect.Field foundField)
        Sets the field.
        Parameters:
        object - the object
        value - the value
        foundField - the found field
      • setStaticFieldUsingUnsafe

        private static void setStaticFieldUsingUnsafe​(java.lang.reflect.Field field,
                                                      java.lang.Object newValue)
      • setFieldUsingUnsafe

        private static void setFieldUsingUnsafe​(java.lang.reflect.Field field,
                                                java.lang.Object object,
                                                java.lang.Object newValue)
      • getUnsafe

        private static sun.misc.Unsafe getUnsafe()
                                          throws java.lang.IllegalArgumentException,
                                                 java.lang.IllegalAccessException,
                                                 java.lang.NoSuchFieldException,
                                                 java.lang.SecurityException
        Throws:
        java.lang.IllegalArgumentException
        java.lang.IllegalAccessException
        java.lang.NoSuchFieldException
        java.lang.SecurityException
      • setFieldUsingUnsafe

        private static void setFieldUsingUnsafe​(java.lang.Object base,
                                                java.lang.Class type,
                                                long offset,
                                                java.lang.Object newValue,
                                                sun.misc.Unsafe unsafe)
      • concatenateStrings

        private static java.lang.String concatenateStrings​(java.lang.String... stringsToConcatenate)
        Concatenate strings.
        Parameters:
        stringsToConcatenate - the strings to concatenate
        Returns:
        the string
      • isPotentialVarArgsMethod

        private static boolean isPotentialVarArgsMethod​(java.lang.reflect.Method method,
                                                        java.lang.Object[] arguments)
        Checks if is potential var args method.
        Parameters:
        method - the method
        arguments - the arguments
        Returns:
        true, if is potential var args method
      • doesParameterTypesMatchForVarArgsInvocation

        static boolean doesParameterTypesMatchForVarArgsInvocation​(boolean isVarArgs,
                                                                   java.lang.Class<?>[] parameterTypes,
                                                                   java.lang.Object[] arguments)
        Does parameter types match for var args invocation.
        Parameters:
        isVarArgs - the is var args
        parameterTypes - the parameter types
        arguments - the arguments
        Returns:
        true, if successful
      • getTypeAsPrimitiveIfWrapped

        static java.lang.Class<?> getTypeAsPrimitiveIfWrapped​(java.lang.Object object)
        Get the type of an object and convert it to primitive if the type has a primitive counter-part. E.g. if object is an instance of java.lang.Integer this method will return int.class.
        Parameters:
        object - The object whose type to get.
        Returns:
        the type as primitive if wrapped
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object object,
                                                       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.
        Parameters:
        object - the object
        context - The context where the fields are defined.
        additionalContexts - Optionally more additional contexts.
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object object,
                                                       java.lang.Object context,
                                                       FieldMatchingStrategy strategy)
      • setInternalStateFromContext

        public static void setInternalStateFromContext​(java.lang.Object object,
                                                       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.
        Parameters:
        object - the object
        context - The context where the fields are defined.
        additionalContexts - Optionally more additional contexts.
      • copyState

        static void copyState​(java.lang.Object object,
                              java.lang.Object context,
                              FieldMatchingStrategy strategy)
        Copy state.
        Parameters:
        object - the object
        context - the context
        strategy - The field matching strategy.
      • assertObjectInGetInternalStateIsNotNull

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

        private static java.lang.Class<?>[] convertParameterTypesToPrimitive​(java.lang.Class<?>[] parameterTypes)
        Convert parameter types to primitive.
        Parameters:
        parameterTypes - the parameter types
        Returns:
        the class[]
      • copyToMock

        public static <T> void copyToMock​(T from,
                                          T mock)
      • copyToRealObject

        public static <T> void copyToRealObject​(T from,
                                                T to)
      • copy

        private static <T> void copy​(T from,
                                     T to,
                                     java.lang.Class<?> fromClazz)
      • copyValues

        private static <T> void copyValues​(T from,
                                           T mock,
                                           java.lang.Class<?> classFrom)
      • copyValue

        private static <T> void copyValue​(T from,
                                          T to,
                                          java.lang.reflect.Field field)
                                   throws java.lang.IllegalAccessException
        Throws:
        java.lang.IllegalAccessException