public class WhiteboxImpl
extends java.lang.Object
Constructor and Description |
---|
WhiteboxImpl() |
Modifier and Type | Method and Description |
---|---|
static boolean |
areAllMethodsStatic(java.lang.reflect.Method... methods)
Are all methods static.
|
static boolean |
checkIfParameterTypesAreSame(boolean isVarArgs,
java.lang.Class<?>[] expectedParameterTypes,
java.lang.Class<?>[] actualParameterTypes)
Check if parameter types are same.
|
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.
|
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.
|
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.
|
static java.util.Set<java.lang.reflect.Field> |
getAllStaticFields(java.lang.Class<?> type)
Get all static fields for a particular type.
|
static java.lang.Class<java.lang.Object> |
getAnonymousInnerClassType(java.lang.Class<?> declaringClass,
int occurrence)
Get the type of an anonymous inner class.
|
static java.lang.reflect.Method |
getBestMethodCandidate(java.lang.Class<?> cls,
java.lang.String methodName,
java.lang.Class<?>[] signature,
boolean exactParameterTypeMatch)
Gets the best method candidate.
|
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.
|
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 . |
static <T> java.lang.Class<?> |
getOriginalUnmockedType(java.lang.Class<T> type)
Gets the unmocked type.
|
static java.lang.Class<?> |
getType(java.lang.Object object)
Gets the type.
|
static <T> UnproxiedType |
getUnproxiedType(java.lang.Class<T> type) |
static java.lang.Class<?> |
getUnproxyType(java.lang.Object object)
Gets the type.
|
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.
|
static boolean |
isClass(java.lang.Object argument)
Checks if is class.
|
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.
|
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,
FieldMatchingStrategy strategy) |
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 |
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.
|
public static java.lang.reflect.Method getMethod(java.lang.Class<?> type, java.lang.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 java.lang.reflect.Method getMethod(java.lang.Class<?> type, java.lang.String methodName, java.lang.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 java.lang.reflect.Field getField(java.lang.Class<?> type, java.lang.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(java.lang.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 java.lang.reflect.Constructor<?> getConstructor(java.lang.Class<?> type, java.lang.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(java.lang.Object object, java.lang.String fieldName, java.lang.Object value)
object
- the object whose field to modifyfieldName
- the name of the fieldvalue
- the new value of the fieldpublic static void setInternalState(java.lang.Object object, java.lang.String fieldName, java.lang.Object[] value)
object
- the object to modifyfieldName
- the name of the fieldvalue
- the new value of the fieldpublic static void setInternalState(java.lang.Object object, java.lang.Class<?> fieldType, java.lang.Object value)
object
- the object to modifyfieldType
- the type of the fieldvalue
- the new value of the fieldpublic static void setInternalState(java.lang.Object object, java.lang.Object value, java.lang.Object... additionalValues)
object
- the object to modifyvalue
- the new value of the fieldadditionalValues
- Additional values to set on the objectpublic static void setInternalState(java.lang.Object object, java.lang.Object value, java.lang.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(java.lang.Object object, java.lang.Class<?> fieldType, java.lang.Object value, java.lang.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(java.lang.Object object, java.lang.String fieldName, java.lang.Object value, java.lang.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(java.lang.Object object, java.lang.String fieldName)
T
- the generic typeobject
- the object to modifyfieldName
- the name of the fieldgetInternalState(Object, String, Class)
.public static <T> T getInternalState(java.lang.Object object, java.lang.Class<T> fieldType)
T
- the generic typeobject
- the object to modifyfieldType
- the type of the fieldpublic static <T> T getInternalState(java.lang.Object object, java.lang.Class<T> fieldType, java.lang.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(java.lang.Object object, java.lang.String fieldName, java.lang.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(java.lang.Object tested, java.lang.Object... arguments) throws java.lang.Exception
T
- the generic typetested
- the testedarguments
- the argumentsjava.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.public static <T> T invokeMethod(java.lang.Class<?> tested, java.lang.Object... arguments) throws java.lang.Exception
T
- the generic typetested
- the testedarguments
- the argumentsjava.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.public static <T> T invokeMethod(java.lang.Object tested, java.lang.String methodToExecute, java.lang.Object... arguments) throws java.lang.Exception
T
- the generic typetested
- the testedmethodToExecute
- the method to executearguments
- the argumentsjava.lang.Exception
- the exceptionpublic static <T> T invokeMethod(java.lang.Object tested, java.lang.String methodToExecute, java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments) throws java.lang.Exception
invokeMethod(Class, String, Object...)
instead.T
- the generic typetested
- the testedmethodToExecute
- the method to executeargumentTypes
- the argument typesarguments
- the argumentsjava.lang.Exception
- Exception that may occur when invoking this method.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
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 argumentsjava.lang.Exception
- Exception that may occur when invoking this method.
invokeMethod(Class, String, Object...)
instead.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
T
- the generic typetested
- the testeddeclaringClass
- the declaring classmethodToExecute
- the method to executearguments
- the argumentsjava.lang.Exception
- Exception that may occur when invoking this method.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
T
- the generic typeobject
- the objectdeclaringClass
- the declaring classmethodToExecute
- the method to executeparameterTypes
- the parameter typesarguments
- the argumentsjava.lang.Exception
- Exception that may occur when invoking this method.
invokeMethod(Object, Object...)
or
invokeMethod(Object, String, Object...)
instead.public static <T> T invokeMethod(java.lang.Class<?> clazz, java.lang.String methodToExecute, java.lang.Object... arguments) throws java.lang.Exception
T
- the generic typeclazz
- the clazzmethodToExecute
- the method to executearguments
- the argumentsjava.lang.Exception
- the exceptionpublic static java.lang.reflect.Method findMethodOrThrowException(java.lang.Object tested, java.lang.Class<?> declaringClass, java.lang.String methodToExecute, java.lang.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.MethodNotFoundException
- if no method was found.TooManyMethodsFoundException
- if too methods matched.java.lang.IllegalArgumentException
- if tested
is null.public static java.lang.reflect.Method getBestMethodCandidate(java.lang.Class<?> cls, java.lang.String methodName, java.lang.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 java.lang.reflect.Constructor<?> findDefaultConstructorOrThrowException(java.lang.Class<?> type) throws ConstructorNotFoundException
throwExceptionWhenMultipleConstructorMatchesFound(java.lang.reflect.Constructor[])
.type
- The type where the constructor should be located.ConstructorNotFoundException
- if too many constructors was found.public static java.lang.reflect.Constructor<?> findConstructorOrThrowException(java.lang.Class<?> type)
throwExceptionWhenMultipleConstructorMatchesFound(java.lang.reflect.Constructor[])
.type
- The type where the constructor should be located.TooManyConstructorsFoundException
- if too many constructors was found.public static java.lang.reflect.Constructor<?> findUniqueConstructorOrThrowException(java.lang.Class<?> type, java.lang.Object... arguments)
type
- The type where the constructor should be located.arguments
- The arguments passed to the constructor.ConstructorNotFoundException
- if no constructor was found.TooManyConstructorsFoundException
- if too constructors matched.java.lang.IllegalArgumentException
- if type
is null.public static void throwExceptionIfMethodWasNotFound(java.lang.Class<?> type, java.lang.String methodName, java.lang.reflect.Method methodToMock, java.lang.Object... arguments)
type
- the typemethodName
- the method namemethodToMock
- the method to mockarguments
- the argumentspublic static void throwExceptionIfFieldWasNotFound(java.lang.Class<?> type, java.lang.String fieldName, java.lang.reflect.Field field)
type
- the typefieldName
- the field namefield
- the fieldpublic static <T> T invokeConstructor(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Class<?>[] parameterTypes, java.lang.Object[] arguments) throws java.lang.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 argumentsjava.lang.Exception
- If an exception occur when invoking the constructor.
invokeConstructor(Class, Object...)
instead.public static <T> T invokeConstructor(java.lang.Class<T> classThatContainsTheConstructorToTest, java.lang.Object... arguments) throws java.lang.Exception
T
- the generic typeclassThatContainsTheConstructorToTest
- the class that contains the constructor to testarguments
- the argumentsjava.lang.Exception
- If an exception occur when invoking the constructor.public static java.lang.reflect.Constructor<?>[] getAllConstructors(java.lang.Class<?> clazz)
true
.clazz
- The class whose constructors to get.public static java.lang.reflect.Method[] getAllMethods(java.lang.Class<?> clazz)
clazz
- The class whose methods to get.public static java.lang.reflect.Field[] getAllFields(java.lang.Class<?> clazz)
clazz
- The class whose fields to get.public static java.lang.reflect.Constructor<?> getFirstParentConstructor(java.lang.Class<?> klass)
klass
.klass
- The class where the constructor is located. null
).public static <T> java.lang.reflect.Method findMethod(java.lang.Class<T> type, java.lang.String methodName, java.lang.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 <T> java.lang.Class<?> getOriginalUnmockedType(java.lang.Class<T> type)
T
- the generic typetype
- the typepublic static <T> UnproxiedType getUnproxiedType(java.lang.Class<T> type)
public static java.lang.reflect.Method findMethodOrThrowException(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
type
- the typemethodName
- the method nameparameterTypes
- the parameter typespublic static java.lang.reflect.Method[] getMethods(java.lang.Class<?> clazz, java.lang.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 java.lang.reflect.Method[] getMethods(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.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 java.lang.reflect.Field[] getFields(java.lang.Class<?> clazz, java.lang.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(java.lang.Object tested, java.lang.reflect.Method methodToInvoke, java.lang.Object... arguments) throws java.lang.Exception
T
- the generic typetested
- the testedmethodToInvoke
- the method to invokearguments
- the argumentsjava.lang.Exception
- the exceptionpublic static <T> java.lang.reflect.Method[] getAllMethodExcept(java.lang.Class<T> type, java.lang.String... methodNames)
T
- the generic typetype
- the typemethodNames
- the method namespublic static <T> java.lang.reflect.Method[] getAllMethodsExcept(java.lang.Class<T> type, java.lang.String methodNameToExclude, java.lang.Class<?>[] argumentTypes)
T
- the generic typetype
- the typemethodNameToExclude
- the method name to excludeargumentTypes
- the argument typespublic static boolean areAllMethodsStatic(java.lang.reflect.Method... methods)
methods
- the methodspublic static java.lang.Class<?> getType(java.lang.Object object)
object
- the objectpublic static java.lang.Class<?> getUnproxyType(java.lang.Object object)
object
- the objectpublic static java.lang.Class<java.lang.Object> getInnerClassType(java.lang.Class<?> declaringClass, java.lang.String name) throws java.lang.ClassNotFoundException
declaringClass
- The class in which the inner class is declared.name
- The unqualified name (simple name) of the inner class.java.lang.ClassNotFoundException
- the class not found exceptionpublic static java.lang.Class<java.lang.Object> getLocalClassType(java.lang.Class<?> declaringClass, int occurrence, java.lang.String name) throws java.lang.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.java.lang.ClassNotFoundException
- the class not found exceptionpublic static java.lang.Class<java.lang.Object> getAnonymousInnerClassType(java.lang.Class<?> declaringClass, int occurrence) throws java.lang.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.java.lang.ClassNotFoundException
- the class not found exceptionpublic 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)
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 java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.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 java.util.Set<java.lang.reflect.Field> getFieldsOfType(java.lang.Object object, java.lang.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 java.util.Set<java.lang.reflect.Field> getAllInstanceFields(java.lang.Object object)
object
- The object whose instance fields to get.public static java.util.Set<java.lang.reflect.Field> getAllStaticFields(java.lang.Class<?> type)
type
- The class whose static fields to get.public static boolean isClass(java.lang.Object argument)
argument
- the argumentpublic static boolean checkIfParameterTypesAreSame(boolean isVarArgs, java.lang.Class<?>[] expectedParameterTypes, java.lang.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(java.lang.Object object, java.lang.Object context, java.lang.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(java.lang.Object object, java.lang.Object context, FieldMatchingStrategy strategy)
public static void setInternalStateFromContext(java.lang.Object object, java.lang.Class<?> context, java.lang.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.