Package mockit
Class Deencapsulation
java.lang.Object
mockit.Deencapsulation
Provides utility methods that enable access to ("de-encapsulate") otherwise non-accessible fields.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Gets the value of a non-accessible static field defined in a given class, assuming there is only one field declared in the given class whose type can receive values of the specified field type.static <T> T
Gets the value of a non-accessible static field defined in a given class.static <T> T
Gets the value of a non-accessible (egprivate
) field from a given object, assuming there is only one field declared in the class of the given object whose type can receive values of the specified field type.static <T> T
Gets the value of a non-accessible (egprivate
) field from a given object.static <T> T
invoke
(Class<?> classWithStaticMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs) Invokes a non-accessible (egprivate
)static
method with the given arguments.static <T> T
Invokes a non-accessible (egprivate
)static
method with the given arguments.static <T> T
Invokes a non-accessible (egprivate
) instance method from a given class with the given arguments.static <T> T
Invokes a non-accessible (egprivate
) instance method from a given class with the given arguments.static <T> T
Invokes a non-accessible (egprivate
)static
method with the given arguments.static void
Sets the value of a non-accessible static field on a given class.static void
Sets the value of a non-accessible static field on a given class.static void
Sets the value of a non-accessible field on a given object.static void
Sets the value of a non-accessible field on a given object.
-
Method Details
-
getField
Gets the value of a non-accessible (egprivate
) field from a given object.- Type Parameters:
T
- interface or class type to which the returned value should be assignable- Parameters:
objectWithField
- the instance from which to get the field valuefieldName
- the name of the field to get- Returns:
- the field
- Throws:
IllegalArgumentException
- if the desired field is not found- See Also:
-
getField
@Nullable public static <T> T getField(@NonNull Object objectWithField, @NonNull Class<T> fieldType) Gets the value of a non-accessible (egprivate
) field from a given object, assuming there is only one field declared in the class of the given object whose type can receive values of the specified field type.- Type Parameters:
T
- the generic type- Parameters:
objectWithField
- the instance from which to get the field valuefieldType
- the declared type of the field, or a sub-type of the declared field type- Returns:
- the field
- Throws:
IllegalArgumentException
- if either the desired field is not found, or more than one is- See Also:
-
getField
@Nullable public static <T> T getField(@NonNull Class<?> classWithStaticField, @NonNull String fieldName) Gets the value of a non-accessible static field defined in a given class.- Type Parameters:
T
- interface or class type to which the returned value should be assignable- Parameters:
classWithStaticField
- the class from which to get the field valuefieldName
- the name of the static field to get- Returns:
- the field
- Throws:
IllegalArgumentException
- if the desired field is not found- See Also:
-
getField
@Nullable public static <T> T getField(@NonNull Class<?> classWithStaticField, @NonNull Class<T> fieldType) Gets the value of a non-accessible static field defined in a given class, assuming there is only one field declared in the given class whose type can receive values of the specified field type.- Type Parameters:
T
- interface or class type to which the returned value should be assignable- Parameters:
classWithStaticField
- the class from which to get the field valuefieldType
- the declared type of the field, or a sub-type of the declared field type- Returns:
- the field
- Throws:
IllegalArgumentException
- if either the desired field is not found, or more than one is- See Also:
-
setField
public static void setField(@NonNull Object objectWithField, @NonNull String fieldName, @Nullable Object fieldValue) Sets the value of a non-accessible field on a given object.- Parameters:
objectWithField
- the instance on which to set the field valuefieldName
- the name of the field to setfieldValue
- the value to set the field to- Throws:
IllegalArgumentException
- if the desired field is not found- See Also:
-
setField
Sets the value of a non-accessible field on a given object. The field is looked up by the type of the given field value instead of by name.- Parameters:
objectWithField
- the object with fieldfieldValue
- the field value- Throws:
IllegalArgumentException
- if either the desired field is not found, or more than one is- See Also:
-
setField
public static void setField(@NonNull Class<?> classWithStaticField, @NonNull String fieldName, @Nullable Object fieldValue) Sets the value of a non-accessible static field on a given class.- Parameters:
classWithStaticField
- the class on which the static field is definedfieldName
- the name of the field to setfieldValue
- the value to set the field to- Throws:
IllegalArgumentException
- if the desired field is not found- See Also:
-
setField
Sets the value of a non-accessible static field on a given class. The field is looked up by the type of the given field value instead of by name.- Parameters:
classWithStaticField
- the class on which the static field is definedfieldValue
- the value to set the field to- Throws:
IllegalArgumentException
- if either the desired field is not found, or more than one is- See Also:
-
invoke
public static <T> T invoke(Object objectWithMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs) Invokes a non-accessible (egprivate
) instance method from a given class with the given arguments.- Type Parameters:
T
- type to which the returned value should be assignable- Parameters:
objectWithMethod
- the instance on which the invocation is to be donemethodName
- the name of the method to invokeparameterTypes
- the types of the parameters as declared in the desired methodmethodArgs
- zero or more parameter values for the invocation- Returns:
- the return value from the invoked method
- Throws:
IllegalArgumentException
- if the desired method is not found- See Also:
-
invoke
Invokes a non-accessible (egprivate
) instance method from a given class with the given arguments.- Type Parameters:
T
- type to which the returned value should be assignable- Parameters:
objectWithMethod
- the instance on which the invocation is to be donemethodName
- the name of the method to invokenonNullArgs
- zero or more non-null parameter values for the invocation; if a null value needs to be passed, theClass
object for the corresponding parameter type must be passed instead- Returns:
- the return value from the invoked method
- Throws:
IllegalArgumentException
- if the desired method is not found, or a null reference was provided for a parameter- See Also:
-
invoke
public static <T> T invoke(Class<?> classWithStaticMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs) Invokes a non-accessible (egprivate
)static
method with the given arguments.- Type Parameters:
T
- type to which the returned value should be assignable- Parameters:
classWithStaticMethod
- the class on which the invocation is to be donemethodName
- the name of the static method to invokeparameterTypes
- the types of the parameters as declared in the desired methodmethodArgs
- zero or more parameter values for the invocation- Returns:
- the return value from the invoked method
- See Also:
-
invoke
public static <T> T invoke(Class<?> classWithStaticMethod, String methodName, Object... nonNullArgs) Invokes a non-accessible (egprivate
)static
method with the given arguments.- Type Parameters:
T
- type to which the returned value should be assignable- Parameters:
classWithStaticMethod
- the class on which the invocation is to be donemethodName
- the name of the static method to invokenonNullArgs
- zero or more non-null parameter values for the invocation; if a null value needs to be passed, theClass
object for the corresponding parameter type must be passed instead- Returns:
- the return value from the invoked method
- Throws:
IllegalArgumentException
- if the desired method is not found, or a null reference was provided for a parameter- See Also:
-
invoke
Invokes a non-accessible (egprivate
)static
method with the given arguments.- Type Parameters:
T
- type to which the returned value should be assignable- Parameters:
classWithStaticMethod
- the (fully qualified) name of the class on which the invocation is to be done; must not be nullmethodName
- the name of the static method to invokenonNullArgs
- zero or more non-null parameter values for the invocation; if a null value needs to be passed, theClass
object for the corresponding parameter type must be passed instead- Returns:
- the return value from the invoked method
- Throws:
IllegalArgumentException
- if the desired method is not found, or a null reference was provided for a parameter- See Also:
-