public class WhiteboxImpl extends Object
Constructor and Description |
---|
WhiteboxImpl() |
Modifier and Type | Method and Description |
---|---|
static boolean |
areAllMethodsStatic(Method... methods)
Are all methods static.
|
static boolean |
checkIfParameterTypesAreSame(boolean isVarArgs,
Class<?>[] expectedParameterTypes,
Class<?>[] actualParameterTypes)
Check if parameter types are same.
|
static Constructor<?> |
findConstructorOrThrowException(Class<?> type)
Finds and returns any constructor.
|
static Constructor<?> |
findDefaultConstructorOrThrowException(Class<?> type)
Finds and returns the default constructor.
|
static <T> Method |
findMethod(Class<T> type,
String methodName,
Class<?>... parameterTypes)
Finds and returns a method based on the input parameters.
|
static Method |
findMethodOrThrowException(Class<?> type,
String methodName,
Class<?>... parameterTypes)
Find method or throw exception.
|
static Method |
findMethodOrThrowException(Object tested,
Class<?> declaringClass,
String methodToExecute,
Object[] arguments)
Finds and returns a certain method.
|
static Constructor<?> |
findUniqueConstructorOrThrowException(Class<?> type,
Object... arguments)
Finds and returns a certain constructor.
|
static Constructor<?>[] |
getAllConstructors(Class<?> clazz)
Get all declared constructors in the class and set accessible to
true . |
static Field[] |
getAllFields(Class<?> clazz)
Get all fields in a class hierarchy! Both declared an non-declared (no
duplicates).
|
static Set<Field> |
getAllInstanceFields(Object object)
Get all instance fields for a particular object.
|
static <T> Method[] |
getAllMethodExcept(Class<T> type,
String... methodNames)
Gets the all method except.
|
static Method[] |
getAllMethods(Class<?> clazz)
Get all methods in a class hierarchy! Both declared an non-declared (no
duplicates).
|
static <T> Method[] |
getAllMetodsExcept(Class<T> type,
String methodNameToExclude,
Class<?>[] argumentTypes)
Gets the all metods except.
|
static Set<Field> |
getAllStaticFields(Class<?> type)
Get all static fields for a particular type.
|
static Class<Object> |
getAnonymousInnerClassType(Class<?> declaringClass,
int occurrence)
Get the type of an anonymous inner class.
|
static Method |
getBestMethodCandidate(Class<?> cls,
String methodName,
Class<?>[] signature,
boolean exactParameterTypeMatch)
Gets the best method candidate.
|
static Constructor<?> |
getConstructor(Class<?> type,
Class<?>... parameterTypes)
Convenience method to get a (declared) constructor from a class type
without having to catch the checked exceptions otherwise required.
|
static Field |
getField(Class<?> type,
String fieldName)
Convenience method to get a field from a class type.
|
static Field[] |
getFields(Class<?> clazz,
String... fieldNames)
Get an array of
Field 's that matches the supplied list of field
names. |
static Set<Field> |
getFieldsAnnotatedWith(Object object,
Class<? extends Annotation>[] annotationTypes)
Get all fields annotated with a particular annotation.
|
static Set<Field> |
getFieldsAnnotatedWith(Object object,
Class<? extends Annotation> annotation,
Class<? extends Annotation>... additionalAnnotations)
Get all fields annotated with a particular annotation.
|
static Set<Field> |
getFieldsOfType(Object object,
Class<?> type)
Get all fields assignable from a particular type.
|
static Constructor<?> |
getFirstParentConstructor(Class<?> klass)
Get the first parent constructor defined in a super class of
klass . |
static Class<Object> |
getInnerClassType(Class<?> declaringClass,
String name)
Get an inner class type.
|
static <T> T |
getInternalState(Object object,
Class<T> fieldType)
Get the value of a field using reflection.
|
static <T> T |
getInternalState(Object object,
Class<T> fieldType,
Class<?> where)
Get the value of a field using reflection.
|
static <T> T |
getInternalState(Object object,
String fieldName)
Get the value of a field using reflection.
|
static <T> T |
getInternalState(Object object,
String fieldName,
Class<?> where)
Get the value of a field using reflection.
|
static Class<Object> |
getLocalClassType(Class<?> declaringClass,
int occurrence,
String name)
Get the type of a local inner class.
|
static Method |
getMethod(Class<?> type,
Class<?>... parameterTypes)
Convenience method to get a method from a class type without having to
catch the checked exceptions otherwise required.
|
static Method |
getMethod(Class<?> type,
String methodName,
Class<?>... parameterTypes)
Convenience method to get a method from a class type without having to
catch the checked exceptions otherwise required.
|
static Method[] |
getMethods(Class<?> clazz,
String... methodNames)
Get an array of
Method 's that matches the supplied list of method
names. |
static Method[] |
getMethods(Class<?> clazz,
String methodName,
Class<?>[] expectedTypes,
boolean exactParameterTypeMatch)
Get an array of
Method 's that matches the method name and whose
argument types are assignable from expectedTypes . |
static Class<?> |
getType(Object object)
Gets the type.
|
static <T> Class<?> |
getUnmockedType(Class<T> type)
Gets the unmocked type.
|
static <T> T |
invokeConstructor(Class<T> classThatContainsTheConstructorToTest,
Class<?>[] parameterTypes,
Object[] arguments)
Invoke a constructor.
|
static <T> T |
invokeConstructor(Class<T> classThatContainsTheConstructorToTest,
Object... arguments)
Invoke a constructor.
|
static <T> T |
invokeMethod(Class<?> tested,
Object... arguments)
Invoke a private or inner class method without the need to specify the
method name.
|
static <T> T |
invokeMethod(Class<?> clazz,
String methodToExecute,
Object... arguments)
Invoke a private or inner class method.
|
static <T> T |
invokeMethod(Object object,
Class<?> declaringClass,
String methodToExecute,
Class<?>[] parameterTypes,
Object... arguments)
Invoke a private method in that is located in a subclass of an instance.
|
static <T> T |
invokeMethod(Object tested,
Class<?> declaringClass,
String methodToExecute,
Object... arguments)
Invoke a private or inner class method in that is located in a subclass
of the tested instance.
|
static <T> T |
invokeMethod(Object tested,
Object... arguments)
Invoke a private or inner class method without the need to specify the
method name.
|
static <T> T |
invokeMethod(Object tested,
String methodToExecute,
Class<?>[] argumentTypes,
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(Object tested,
String methodToExecute,
Class<?> definedIn,
Class<?>[] argumentTypes,
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(Object tested,
String methodToExecute,
Object... arguments)
Invoke a private or inner class method.
|
static boolean |
isClass(Object argument)
Checks if is class.
|
static boolean |
isProxy(Class<?> type)
Checks if is proxy.
|
static <T> T |
newInstance(Class<T> classToInstantiate)
Create a new instance of a class without invoking its constructor.
|
static <T> T |
performMethodInvocation(Object tested,
Method methodToInvoke,
Object... arguments)
Perform method invocation.
|
static void |
setInternalState(Object object,
Class<?> fieldType,
Object value)
Set the value of a field using reflection.
|
static void |
setInternalState(Object object,
Class<?> fieldType,
Object value,
Class<?> where)
Set the value of a field using reflection at a specific location (
where) in the class hierarchy.
|
static void |
setInternalState(Object object,
Object value,
Class<?> where)
Set the value of a field using reflection at at specific place in the
class hierarchy (where).
|
static void |
setInternalState(Object object,
Object value,
Object... additionalValues)
Set the value of a field using reflection.
|
static void |
setInternalState(Object object,
String fieldName,
Object value)
Set the value of a field using reflection.
|
static void |
setInternalState(Object object,
String fieldName,
Object[] value)
Set the value of a field using reflection.
|
static void |
setInternalState(Object object,
String fieldName,
Object value,
Class<?> where)
Set the value of a field using reflection.
|
static void |
setInternalStateFromContext(Object object,
Class<?> context,
Class<?>[] additionalContexts)
Set the values of multiple static fields defined in a context using
reflection.
|
static void |
setInternalStateFromContext(Object object,
Object context,
FieldMatchingStrategy strategy) |
static void |
setInternalStateFromContext(Object object,
Object context,
Object[] additionalContexts)
Set the values of multiple instance fields defined in a context using
reflection.
|
static void |
throwExceptionIfFieldWasNotFound(Class<?> type,
String fieldName,
Field field)
Throw exception if field was not found.
|
static void |
throwExceptionIfMethodWasNotFound(Class<?> type,
String methodName,
Method methodToMock,
Object... arguments)
Throw exception if method was not found.
|
public static Method getMethod(Class<?> type, Class<?>... parameterTypes)
MethodNotFoundException
is thrown. Since the method name is not specified antype
- The type of the class where the method is located.parameterTypes
- All parameter types of the method (may be null
).TooManyMethodsFoundException
is thrown if two or more
methods matches the same parameter types in the same class.public static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes)
IllegalArgumentException
is thrown.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
).public static Field getField(Class<?> type, String fieldName)
IllegalArgumentException
is thrown.type
- The type of the class where the method is located.fieldName
- The method names.public static <T> T newInstance(Class<T> classToInstantiate)
PowerMockRunner
or
PrepareForTest
annotation to use this functionality.T
- The type of the instance to create.classToInstantiate
- The type of the instance to create.public static Constructor<?> getConstructor(Class<?> type, Class<?>... parameterTypes)
type
- The type of the class where the constructor is located.parameterTypes
- All parameter types of the constructor (may be
null
).public static void setInternalState(Object object, String fieldName, Object value)
object
- the object whose field to modifyfieldName
- the name of the fieldvalue
- the new value of the fieldpublic static void setInternalState(Object object, String fieldName, Object[] value)
object
- the object to modifyfieldName
- the name of the fieldvalue
- the new value of the fieldpublic static void setInternalState(Object object, Class<?> fieldType, Object value)
object
- the object to modifyfieldType
- the type of the fieldvalue
- the new value of the fieldpublic static void setInternalState(Object object, Object value, Object... additionalValues)
object
- the object to modifyvalue
- the new value of the fieldadditionalValues
- Additional values to set on the objectpublic static void setInternalState(Object object, Object value, Class<?> where)
object
- the object to modifyvalue
- the new value of the fieldwhere
- the class in the hierarchy where the field is definedpublic static void setInternalState(Object object, Class<?> fieldType, Object value, Class<?> where)
object
- the object to modifyfieldType
- the type of the field the should be set.value
- the new value of the fieldwhere
- which class in the hierarchy defining the fieldpublic static void setInternalState(Object object, String fieldName, Object value, Class<?> where)
object
- the object to modifyfieldName
- the name of the fieldvalue
- the new value of the fieldwhere
- which class the field is definedpublic static <T> T getInternalState(Object object, String fieldName)
T
- the generic typeobject
- the object to modifyfieldName
- the name of the fieldgetInternalState(Object, String, Class)
.public static <T> T getInternalState(Object object, Class<T> fieldType)
T
- the generic typeobject
- the object to modifyfieldType
- the type of the fieldpublic static <T> T getInternalState(Object object, Class<T> fieldType, Class<?> where)
T
- the expected type of the fieldobject
- the object to modifyfieldType
- the type of the fieldwhere
- which class the field is definedpublic static <T> T getInternalState(Object object, String fieldName, Class<?> where)
T
- the expected type of the fieldobject
- the object to modifyfieldName
- the name of the fieldwhere
- which class the field is definedpublic static <T> T invokeMethod(Object tested, Object... arguments) throws Exception
T
- the generic typetested
- the testedarguments
- the argumentsException
- 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.public static <T> T invokeMethod(Class<?> tested, Object... arguments) throws Exception
T
- the generic typetested
- the testedarguments
- the argumentsException
- 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.public static <T> T invokeMethod(Object tested, String methodToExecute, Object... arguments) throws Exception
T
- the generic typetested
- the testedmethodToExecute
- the method to executearguments
- the argumentsException
- the exceptionpublic static <T> T invokeMethod(Object tested, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception
invokeMethod(Class, String, Object...)
instead.T
- the generic typetested
- the testedmethodToExecute
- the method to executeargumentTypes
- the argument typesarguments
- the argumentsException
- Exception that may occur when invoking this method.public static <T> T invokeMethod(Object tested, String methodToExecute, Class<?> definedIn, Class<?>[] argumentTypes, Object... arguments) throws Exception
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 useT
- the generic typetested
- the testedmethodToExecute
- the method to executedefinedIn
- the defined inargumentTypes
- the argument typesarguments
- the argumentsException
- Exception that may occur when invoking this method.
invokeMethod(Class, String, Object...)
instead.public static <T> T invokeMethod(Object tested, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception
T
- the generic typetested
- the testeddeclaringClass
- the declaring classmethodToExecute
- the method to executearguments
- the argumentsException
- Exception that may occur when invoking this method.public static <T> T invokeMethod(Object object, Class<?> declaringClass, String methodToExecute, Class<?>[] parameterTypes, Object... arguments) throws Exception
T
- the generic typeobject
- the objectdeclaringClass
- the declaring classmethodToExecute
- the method to executeparameterTypes
- the parameter typesarguments
- the argumentsException
- Exception that may occur when invoking this method.
invokeMethod(Object, Object...)
or
invokeMethod(Object, String, Object...)
instead.public static <T> T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments) throws Exception
T
- the generic typeclazz
- the clazzmethodToExecute
- the method to executearguments
- the argumentsException
- the exceptionpublic static Method findMethodOrThrowException(Object tested, Class<?> declaringClass, String methodToExecute, Object[] arguments)
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.{@link
- org.powermock.reflect.exceptions.MethodNotFoundException} if no method was found{@link
- org.powermock.reflect.exceptions.TooManyMethodsFoundException} if too methods matchedpublic static Method getBestMethodCandidate(Class<?> cls, String methodName, Class<?>[] signature, boolean exactParameterTypeMatch)
cls
- the clsmethodName
- the method namesignature
- the signatureexactParameterTypeMatch
- 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.public static Constructor<?> findDefaultConstructorOrThrowException(Class<?> type)
throwExceptionWhenMultipleConstructorMatchesFound(java.lang.reflect.Constructor[])
.type
- The type where the constructor should be located.{@link
- org.powermock.reflect.exceptions.TooManyConstructorsFoundException} if too many constructors was found.public static Constructor<?> findConstructorOrThrowException(Class<?> type)
throwExceptionWhenMultipleConstructorMatchesFound(java.lang.reflect.Constructor[])
.type
- The type where the constructor should be located.{@link
- org.powermock.reflect.exceptions.TooManyConstructorsFoundException} if too many constructors was found.public static Constructor<?> findUniqueConstructorOrThrowException(Class<?> type, Object... arguments)
type
- The type where the constructor should be located.arguments
- The arguments passed to the constructor.{@link
- org.powermock.reflect.exceptions.ConstructorNotFoundException} if no constructor was found{@link
- org.powermock.reflect.exceptions.TooManyConstructorsFoundException} if too constructors matchedpublic static void throwExceptionIfMethodWasNotFound(Class<?> type, String methodName, Method methodToMock, Object... arguments)
type
- the typemethodName
- the method namemethodToMock
- the method to mockarguments
- the argumentspublic static void throwExceptionIfFieldWasNotFound(Class<?> type, String fieldName, Field field)
type
- the typefieldName
- the field namefield
- the fieldpublic static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Class<?>[] parameterTypes, Object[] arguments) throws Exception
public class MyClass { private MyClass(Integer i) { ... } private MyClass(int i) { ... }This ought to be a really rare case. So for most situation, use
T
- the generic typeclassThatContainsTheConstructorToTest
- the class that contains the constructor to testparameterTypes
- the parameter typesarguments
- the argumentsException
- If an exception occur when invoking the constructor.
invokeConstructor(Class, Object...)
instead.public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Object... arguments) throws Exception
T
- the generic typeclassThatContainsTheConstructorToTest
- the class that contains the constructor to testarguments
- the argumentsException
- If an exception occur when invoking the constructor.public static Constructor<?>[] getAllConstructors(Class<?> clazz)
true
.clazz
- The class whose constructors to get.public static Method[] getAllMethods(Class<?> clazz)
clazz
- The class whose methods to get.public static Field[] getAllFields(Class<?> clazz)
clazz
- The class whose fields to get.public static Constructor<?> getFirstParentConstructor(Class<?> klass)
klass
.klass
- The class where the constructor is located. null
).public static <T> Method findMethod(Class<T> type, String methodName, Class<?>... parameterTypes)
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 anT
- the generic typetype
- the typemethodName
- the method nameparameterTypes
- the parameter typesIllegalArgumentException
will be thrown.public static boolean isProxy(Class<?> type)
type
- the typepublic static <T> Class<?> getUnmockedType(Class<T> type)
T
- the generic typetype
- the typepublic static Method findMethodOrThrowException(Class<?> type, String methodName, Class<?>... parameterTypes)
type
- the typemethodName
- the method nameparameterTypes
- the parameter typespublic static Method[] getMethods(Class<?> clazz, String... methodNames)
Method
's that matches the supplied list of method
names. Both instance and static methods are taken into account.clazz
- The class that should contain the methods.methodNames
- Names of the methods that will be returned.public static Method[] getMethods(Class<?> clazz, String methodName, Class<?>[] expectedTypes, boolean exactParameterTypeMatch)
Method
's that matches the method name and whose
argument types are assignable from expectedTypes
. Both
instance and static methods are taken into account.clazz
- The class that should contain the methods.methodName
- Names of the methods that will be returned.expectedTypes
- The methods must matchexactParameterTypeMatch
- 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.public static Field[] getFields(Class<?> clazz, String... fieldNames)
Field
's that matches the supplied list of field
names. Both instance and static fields are taken into account.clazz
- The class that should contain the fields.fieldNames
- Names of the fields that will be returned.public static <T> T performMethodInvocation(Object tested, Method methodToInvoke, Object... arguments) throws Exception
T
- the generic typetested
- the testedmethodToInvoke
- the method to invokearguments
- the argumentsException
- the exceptionpublic static <T> Method[] getAllMethodExcept(Class<T> type, String... methodNames)
T
- the generic typetype
- the typemethodNames
- the method namespublic static <T> Method[] getAllMetodsExcept(Class<T> type, String methodNameToExclude, Class<?>[] argumentTypes)
T
- the generic typetype
- the typemethodNameToExclude
- the method name to excludeargumentTypes
- the argument typespublic static boolean areAllMethodsStatic(Method... methods)
methods
- the methodspublic static Class<?> getType(Object object)
object
- the objectpublic static Class<Object> getInnerClassType(Class<?> declaringClass, String name) throws ClassNotFoundException
declaringClass
- The class in which the inner class is declared.name
- The unqualified name (simple name) of the inner class.ClassNotFoundException
- the class not found exceptionpublic static Class<Object> getLocalClassType(Class<?> declaringClass, int occurrence, String name) throws ClassNotFoundException
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.ClassNotFoundException
- the class not found exceptionpublic static Class<Object> getAnonymousInnerClassType(Class<?> declaringClass, int occurrence) throws ClassNotFoundException
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.ClassNotFoundException
- the class not found exceptionpublic static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation> annotation, Class<? extends Annotation>... additionalAnnotations)
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
.public static Set<Field> getFieldsAnnotatedWith(Object object, Class<? extends Annotation>[] annotationTypes)
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 forpublic static Set<Field> getFieldsOfType(Object object, Class<?> type)
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.public static Set<Field> getAllInstanceFields(Object object)
object
- The object whose instance fields to get.public static Set<Field> getAllStaticFields(Class<?> type)
type
- The class whose static fields to get.public static boolean isClass(Object argument)
argument
- the argumentpublic static boolean checkIfParameterTypesAreSame(boolean isVarArgs, Class<?>[] expectedParameterTypes, Class<?>[] actualParameterTypes)
isVarArgs
- Whether or not the method or constructor contains var args.expectedParameterTypes
- the expected parameter typesactualParameterTypes
- the actual parameter typespublic static void setInternalStateFromContext(Object object, Object context, Object[] additionalContexts)
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
.object
- the objectcontext
- The context where the fields are defined.additionalContexts
- Optionally more additional contexts.public static void setInternalStateFromContext(Object object, Object context, FieldMatchingStrategy strategy)
public static void setInternalStateFromContext(Object object, Class<?> context, Class<?>[] additionalContexts)
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
.object
- the objectcontext
- The context where the fields are defined.additionalContexts
- Optionally more additional contexts.Copyright © 2007–2015. All rights reserved.