Package mockit

Class Deencapsulation

java.lang.Object
mockit.Deencapsulation

public final class Deencapsulation extends Object
Provides utility methods that enable access to ("de-encapsulate") otherwise non-accessible fields.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    getField(Class<?> classWithStaticField, 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.
    static <T> T
    getField(Class<?> classWithStaticField, String fieldName)
    Gets the value of a non-accessible static field defined in a given class.
    static <T> T
    getField(Object objectWithField, Class<T> fieldType)
    Gets the value of a non-accessible (eg private) 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
    getField(Object objectWithField, String fieldName)
    Gets the value of a non-accessible (eg private) field from a given object.
    static <T> T
    invoke(Class<?> classWithStaticMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs)
    Invokes a non-accessible (eg private) static method with the given arguments.
    static <T> T
    invoke(Class<?> classWithStaticMethod, String methodName, Object... nonNullArgs)
    Invokes a non-accessible (eg private) static method with the given arguments.
    static <T> T
    invoke(Object objectWithMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs)
    Invokes a non-accessible (eg private) instance method from a given class with the given arguments.
    static <T> T
    invoke(Object objectWithMethod, String methodName, Object... nonNullArgs)
    Invokes a non-accessible (eg private) instance method from a given class with the given arguments.
    static <T> T
    invoke(String classWithStaticMethod, String methodName, Object... nonNullArgs)
    Invokes a non-accessible (eg private) static method with the given arguments.
    static void
    setField(Class<?> classWithStaticField, Object fieldValue)
    Sets the value of a non-accessible static field on a given class.
    static void
    setField(Class<?> classWithStaticField, String fieldName, Object fieldValue)
    Sets the value of a non-accessible static field on a given class.
    static void
    setField(Object objectWithField, Object fieldValue)
    Sets the value of a non-accessible field on a given object.
    static void
    setField(Object objectWithField, String fieldName, Object fieldValue)
    Sets the value of a non-accessible field on a given object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getField

      @Nullable public static <T> T getField(@NonNull Object objectWithField, @NonNull String fieldName)
      Gets the value of a non-accessible (eg private) 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 value
      fieldName - 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 (eg private) 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 value
      fieldType - 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 value
      fieldName - 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 value
      fieldType - 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 value
      fieldName - the name of the field to set
      fieldValue - the value to set the field to
      Throws:
      IllegalArgumentException - if the desired field is not found
      See Also:
    • setField

      public static void setField(@NonNull Object objectWithField, @NonNull Object fieldValue)
      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 field
      fieldValue - 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 defined
      fieldName - the name of the field to set
      fieldValue - the value to set the field to
      Throws:
      IllegalArgumentException - if the desired field is not found
      See Also:
    • setField

      public static void setField(@NonNull Class<?> classWithStaticField, @NonNull Object fieldValue)
      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 defined
      fieldValue - 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 (eg private) 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 done
      methodName - the name of the method to invoke
      parameterTypes - the types of the parameters as declared in the desired method
      methodArgs - 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

      public static <T> T invoke(Object objectWithMethod, String methodName, Object... nonNullArgs)
      Invokes a non-accessible (eg private) 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 done
      methodName - the name of the method to invoke
      nonNullArgs - zero or more non-null parameter values for the invocation; if a null value needs to be passed, the Class 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 (eg private) 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 done
      methodName - the name of the static method to invoke
      parameterTypes - the types of the parameters as declared in the desired method
      methodArgs - 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 (eg private) 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 done
      methodName - the name of the static method to invoke
      nonNullArgs - zero or more non-null parameter values for the invocation; if a null value needs to be passed, the Class 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(String classWithStaticMethod, String methodName, Object... nonNullArgs)
      Invokes a non-accessible (eg private) 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 null
      methodName - the name of the static method to invoke
      nonNullArgs - zero or more non-null parameter values for the invocation; if a null value needs to be passed, the Class 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: