public class Whitebox extends Object
Constructor and Description |
---|
Whitebox() |
Modifier and Type | Method and Description |
---|---|
static Set<Field> |
getAllInstanceFields(Object object)
Get all instance fields for a particular object.
|
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 <T> Constructor<T> |
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 based on the fields type.
|
static <T> T |
getInternalState(Object object,
Class<T> fieldType,
Class<?> where)
Get the value of a field using reflection based on the field type.
|
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 <T> T |
getInternalState(Object object,
String fieldName,
Class<?> where,
Class<T> type)
Deprecated.
Use
getInternalState(Object, String, Class) instead. |
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 Class<?> |
getType(Object object) |
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<?> klass,
Object... arguments)
Invoke a private or inner class static method without the need to specify
the method name.
|
static <T> T |
invokeMethod(Class<?> clazz,
String methodToExecute,
Object... arguments)
Invoke a static private or inner class method.
|
static <T> T |
invokeMethod(Object object,
Class<?> declaringClass,
String methodToExecute,
Class<?>[] parameterTypes,
Object... arguments)
Invoke a private or inner class method in that is located in a subclass
of the instance.
|
static <T> T |
invokeMethod(Object instance,
Class<?> declaringClass,
String methodToExecute,
Object... arguments)
Invoke a private or inner class method in that is located in a subclass
of the instance.
|
static <T> T |
invokeMethod(Object instance,
Object... arguments)
Invoke a private or inner class method without the need to specify the
method name.
|
static <T> T |
invokeMethod(Object instance,
String methodToExecute,
Class<?>[] argumentTypes,
Object... arguments)
Invoke a private or inner class method in cases where PowerMock cannot
automatically determine the type of the parameters, for example when
mixing primitive types and wrapper types in the same method.
|
static <T> T |
invokeMethod(Object instance,
String methodToExecute,
Class<?> definedIn,
Class<?>[] argumentTypes,
Object... arguments)
Invoke a private or inner class method in a subclass (defined by
definedIn ) in cases where PowerMock cannot automatically
determine the type of the parameters, for example when mixing primitive
types and wrapper types in the same method. |
static <T> T |
invokeMethod(Object instance,
String methodToExecute,
Object... arguments)
Invoke a private or inner class method.
|
static <T> T |
newInstance(Class<T> classToInstantiate)
Create a new instance of a class without invoking its constructor.
|
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 an array 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 classOrInstance,
Class<?> context,
Class<?>... additionalContexts)
Set the values of multiple static fields defined in a context using
reflection.
|
static void |
setInternalStateFromContext(Object instance,
Class<?> context,
FieldMatchingStrategy strategy)
Set the values of multiple static fields defined in a context using
reflection and using an explicit
FieldMatchingStrategy . |
static void |
setInternalStateFromContext(Object instance,
Object context,
FieldMatchingStrategy strategy)
Set the values of multiple instance fields defined in a context using
reflection and using an explicit
FieldMatchingStrategy . |
static void |
setInternalStateFromContext(Object instance,
Object context,
Object... additionalContexts)
Set the values of multiple instance fields defined in a context using
reflection.
|
public static Field getField(Class<?> type, String fieldName)
The method will first try to look for a declared field in the same class.
If the field is not declared in this class it will look for the field in
the super class. This will continue throughout the whole class hierarchy.
If the field is not found an IllegalArgumentException
is thrown.
type
- The type of the class where the method is located.fieldName
- The method names.java.lang.reflect.Field
.FieldNotFoundException
- If a field cannot be found in the hierarchy.public static Field[] getFields(Class<?> clazz, String... fieldNames)
Field
's that matches the supplied list of field
names.clazz
- The class that should contain the fields.fieldNames
- The names of the fields that will be returned.null
public static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes)
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.
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
).java.lang.reflect.Method
.MethodNotFoundException
- If a method cannot be found in the hierarchy.public static Method getMethod(Class<?> type, Class<?>... parameterTypes)
The method will first try to look for a declared method in the same
class. If the method is not declared in this class it will look for the
method in the super class. This will continue throughout the whole class
hierarchy. If the method is not found an MethodNotFoundException
is thrown. Since the method name is not specified an
TooManyMethodsFoundException
is thrown if two or more methods
matches the same parameter types in the same class.
type
- The type of the class where the method is located.parameterTypes
- All parameter types of the method (may be null
).java.lang.reflect.Method
.MethodNotFoundException
- If a method cannot be found in the hierarchy.TooManyMethodsFoundException
- If several methods were found.public static <T> T newInstance(Class<T> classToInstantiate)
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.
T
- The type of the instance to create.classToInstantiate
- The type of the instance to create.public static <T> Constructor<T> 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
).java.lang.reflect.Constructor
.ConstructorNotFoundException
- if the constructor cannot be found.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, 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, String fieldName, Object value, Class<?> where)
object
- the object to modifyfieldName
- the name of the fieldvalue
- 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)
object
- the object to modifyfieldType
- the type of the fieldvalue
- the new value of the fieldpublic 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 <T> T getInternalState(Object object, String fieldName)
getInternalState(Object, String, Class)
.object
- the object to modifyfieldName
- the name of the fieldpublic static <T> T getInternalState(Object object, String fieldName, Class<?> where)
object
- the object to modifyfieldName
- the name of the fieldwhere
- which class the field is defined@Deprecated public static <T> T getInternalState(Object object, String fieldName, Class<?> where, Class<T> type)
getInternalState(Object, String, Class)
instead.T
- the expected type of the fieldobject
- the object to modifyfieldName
- the name of the fieldwhere
- which class the field is definedtype
- the expected type of the fieldpublic static <T> T getInternalState(Object object, Class<T> fieldType)
object
- 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 invokeMethod(Object instance, Object... arguments) throws 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<?> klass, Object... arguments) throws Exception
invokeMethod(Class, String, Object...)
method and is recommend
over this method for that reason. This method might be useful to test
private methods.Exception
public static <T> T invokeMethod(Object instance, String methodToExecute, Object... arguments) throws Exception
Exception
public static <T> T invokeMethod(Object instance, String methodToExecute, Class<?>[] argumentTypes, Object... arguments) throws Exception
invokeMethod(Object, Object...)
instead.Exception
- Exception that may occur when invoking this method.public static <T> T invokeMethod(Object instance, String methodToExecute, Class<?> definedIn, Class<?>[] argumentTypes, Object... arguments) throws Exception
definedIn
) in cases where PowerMock cannot automatically
determine the type of the parameters, for example when mixing primitive
types and wrapper types in the same method. For most situations use
invokeMethod(Object, Object...)
instead.Exception
- Exception that may occur when invoking this method.public static <T> T invokeMethod(Object instance, Class<?> declaringClass, String methodToExecute, Object... arguments) throws Exception
Exception
- 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
Use this for overloaded methods.
Exception
- Exception that may occur when invoking this method.public static <T> T invokeMethod(Class<?> clazz, String methodToExecute, Object... arguments) throws Exception
Exception
public 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
invokeConstructor(Class, Object...)
instead.Exception
- If an exception occur when invoking the constructor.public static <T> T invokeConstructor(Class<T> classThatContainsTheConstructorToTest, Object... arguments) throws Exception
Exception
- If an exception occur when invoking the constructor.public static Constructor<?> getFirstParentConstructor(Class<?> klass)
klass
.klass
- The class where the constructor is located. null
).java.lang.reflect.Constructor
.public 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.MethodNotFoundException
- If no method was found.public 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> 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.type
. All fields are set to
accessible.public 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 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
public 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
public 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
public static void setInternalStateFromContext(Object instance, 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
.
By default the FieldMatchingStrategy.MATCHING
strategy is used
which means that the fields defined in the context but not found in the
classOrInstance
are silently ignored.
instance
- the object whose fields to modify.context
- The context where the fields are defined.additionalContexts
- Optionally more additional contexts.public static void setInternalStateFromContext(Object classOrInstance, 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
.
By default the FieldMatchingStrategy.MATCHING
strategy is used
which means that the fields defined in the context but not found in the
classOrInstance
are silently ignored.
classOrInstance
- The object or class whose fields to set.context
- The context where the fields are defined.additionalContexts
- Optionally more additional contexts.public static void setInternalStateFromContext(Object instance, Object context, FieldMatchingStrategy strategy)
FieldMatchingStrategy
. The
values in the context will be assigned to values on the
instance
. This method will traverse the class hierarchy when
searching for the fields. Example usage:
Given:
public class MyContext { private String myString = "myString"; protected int myInt = 9; } public class MyInstance { private String myInstanceString; private int myInstanceInt; }then
Whitebox.setInternalStateFromContext(new MyInstance(), new MyContext());will set the instance variables of
myInstance
to the values
specified in MyContext
.instance
- the object whose fields to modify.context
- The context where the fields are defined.strategy
- Which field matching strategy to use.public static void setInternalStateFromContext(Object instance, Class<?> context, FieldMatchingStrategy strategy)
FieldMatchingStrategy
. The
values in the context will be assigned to values on the
classOrInstance
. This method will traverse the class
hierarchy when searching for the fields. Example usage:
Given:
public class MyContext { private static String myString = "myString"; protected static int myInt = 9; } public class MyInstance { private static String myInstanceString; private static int myInstanceInt; }then
Whitebox.setInternalStateFromContext(MyInstance.class, MyContext.class);will set the static variables of
MyInstance
to the values
specified in MyContext
.
instance
- The object or class whose fields to set.context
- The context where the fields are defined.strategy
- Which field matching strategy to use.Copyright © 2007–2015. All rights reserved.