public final class Deencapsulation extends Object
Modifier and Type | Method and 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 <T> T |
newInnerInstance(Class<? extends T> innerClassToInstantiate,
Object outerClassInstance,
Object... nonNullArgs)
The same as
newInstance(String, Class[], Object...) , but for instantiating an inner class of some other
class, and where all other (if any) initialization arguments are known to be non-null. |
static <T> T |
newInnerInstance(String innerClassSimpleName,
Object outerClassInstance,
Object... nonNullArgs)
The same as
newInstance(String, Class[], Object...) , but for instantiating an inner non-accessible class
of some other class, and where all other (if any) initialization arguments are known to be non-null. |
static <T> T |
newInstance(Class<? extends T> classToInstantiate,
Class<?>[] parameterTypes,
Object... initArgs)
Creates a new instance of a given class, invoking the constructor which has the specified parameter types.
|
static <T> T |
newInstance(Class<? extends T> classToInstantiate,
Object... nonNullArgs)
Creates a new instance of a given class, invoking the constructor which has parameters matching the number, order,
and types of the given non-null arguments.
|
static <T> T |
newInstance(String className,
Class<?>[] parameterTypes,
Object... initArgs)
Creates a new instance of a given non-accessible class, invoking the constructor which has the specified parameter
types.
|
static <T> T |
newInstance(String className,
Object... nonNullArgs)
Creates a new instance of a given non-accessible class, invoking the constructor which has parameters matching the
number, order, and types of the given non-null arguments.
|
static <T> T |
newUninitializedInstance(Class<? extends T> classToInstantiate)
Creates a new instance of a given class, without invoking any constructor.
|
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.
|
public static <T> T getField(Class<?> classWithStaticField, Class<T> fieldType)
T
- interface or class type to which the returned value should be assignableclassWithStaticField
- the class from which to get the field valuefieldType
- the declared type of the field, or a sub-type of the declared field typeIllegalArgumentException
- if either the desired field is not found, or more than one isgetField(Class, String)
,
getField(Object, Class)
,
setField(Class, Object)
public static <T> T getField(Class<?> classWithStaticField, String fieldName)
T
- interface or class type to which the returned value should be assignableclassWithStaticField
- the class from which to get the field valuefieldName
- the name of the static field to getIllegalArgumentException
- if the desired field is not foundgetField(Class, Class)
,
getField(Object, String)
,
setField(Class, String, Object)
public static <T> T getField(Object objectWithField, Class<T> fieldType)
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.objectWithField
- the instance from which to get the field valuefieldType
- the declared type of the field, or a sub-type of the declared field typeIllegalArgumentException
- if either the desired field is not found, or more than one isgetField(Object, String)
,
getField(Class, String)
,
setField(Object, Object)
public static <T> T getField(Object objectWithField, String fieldName)
private
) field from a given object.T
- interface or class type to which the returned value should be assignableobjectWithField
- the instance from which to get the field valuefieldName
- the name of the field to getIllegalArgumentException
- if the desired field is not foundgetField(Object, Class)
,
getField(Class, String)
,
setField(Object, String, Object)
public static <T> T invoke(Class<?> classWithStaticMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs)
private
) static
method with the given arguments.T
- type to which the returned value should be assignableclassWithStaticMethod
- 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 invocationinvoke(String, String, Object...)
public static <T> T invoke(Class<?> classWithStaticMethod, String methodName, Object... nonNullArgs)
private
) static
method with the given arguments.T
- type to which the returned value should be assignableclassWithStaticMethod
- 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,
the Class
object for the corresponding parameter type must be passed insteadIllegalArgumentException
- if a null reference was provided for a parameterinvoke(String, String, Object...)
public static <T> T invoke(Object objectWithMethod, String methodName, Class<?>[] parameterTypes, Object... methodArgs)
private
) instance method from a given class with the given arguments.T
- type to which the returned value should be assignableobjectWithMethod
- 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 invocationinvoke(Class, String, Object...)
public static <T> T invoke(Object objectWithMethod, String methodName, Object... nonNullArgs)
private
) instance method from a given class with the given arguments.T
- type to which the returned value should be assignableobjectWithMethod
- 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,
the Class
object for the corresponding parameter type must be passed insteadIllegalArgumentException
- if a null reference was provided for a parameterinvoke(Class, String, Object...)
public static <T> T invoke(String classWithStaticMethod, String methodName, Object... nonNullArgs)
private
) static
method with the given arguments.T
- type to which the returned value should be assignableclassWithStaticMethod
- 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,
the Class
object for the corresponding parameter type must be passed insteadIllegalArgumentException
- if a null reference was provided for a parameterinvoke(Class, String, Object...)
public static <T> T newInnerInstance(Class<? extends T> innerClassToInstantiate, Object outerClassInstance, Object... nonNullArgs)
newInstance(String, Class[], Object...)
, but for instantiating an inner class of some other
class, and where all other (if any) initialization arguments are known to be non-null.T
- type to which the returned instance should be assignableinnerClassToInstantiate
- the inner class to be instantiatedouterClassInstance
- the outer class instance to which the inner class instance will belongnonNullArgs
- 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 insteadIllegalArgumentException
- if a null reference was provided for a parameternewInnerInstance(String, Object, Object...)
public static <T> T newInnerInstance(String innerClassSimpleName, Object outerClassInstance, Object... nonNullArgs)
newInstance(String, Class[], Object...)
, but for instantiating an inner non-accessible class
of some other class, and where all other (if any) initialization arguments are known to be non-null.T
- type to which the returned instance should be assignableinnerClassSimpleName
- the inner class simple name, that is, the part after the "$" character in its full
nameouterClassInstance
- the outer class instance to which the inner class instance will belongnonNullArgs
- 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 insteadIllegalArgumentException
- if a null reference was provided for a parameternewInnerInstance(Class, Object, Object...)
public static <T> T newInstance(Class<? extends T> classToInstantiate, Class<?>[] parameterTypes, Object... initArgs)
T
- type to which the returned instance should be assignableclassToInstantiate
- the class to be instantiatedparameterTypes
- the formal parameter types for the desired constructor, possibly emptyinitArgs
- the invocation arguments for the constructor, which must be consistent with the specified
parameter typesnewInstance(Class, Object...)
,
newInnerInstance(Class, Object, Object...)
,
newInstance(String, Class[], Object...)
public static <T> T newInstance(Class<? extends T> classToInstantiate, Object... nonNullArgs)
T
- type to which the returned instance should be assignablenonNullArgs
- 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 insteadIllegalArgumentException
- if a null reference was provided for a parameter, or if no constructor was found
that matched the given argument typesnewInnerInstance(Class, Object, Object...)
,
newInstance(String, Object...)
,
newInstance(String, Class[], Object...)
public static <T> T newInstance(String className, Class<?>[] parameterTypes, Object... initArgs)
T
- type to which the returned instance should be assignableclassName
- the fully qualified name of the desired classparameterTypes
- the formal parameter types for the desired constructor, possibly emptyinitArgs
- the invocation arguments for the constructor, which must be consistent with the specified
parameter typesnewInstance(String, Object...)
,
newInstance(Class, Class[], Object...)
,
newInnerInstance(String, Object, Object...)
public static <T> T newInstance(String className, Object... nonNullArgs)
T
- type to which the returned instance should be assignablenonNullArgs
- 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 insteadIllegalArgumentException
- if a null reference was provided for a parameter, or if no constructor was found
that matched the given argument typesnewInnerInstance(String, Object, Object...)
,
newInstance(Class, Object...)
,
newInstance(String, Class[], Object...)
public static <T> T newUninitializedInstance(Class<? extends T> classToInstantiate)
abstract
or an interface
, then a concrete class is created, with empty
implementations for the abstract
/interface
methods.T
- type to which the returned instance should be assignableclassToInstantiate
- the class to be instantiatednewInstance(Class, Object...)
,
newInstance(String, Class[], Object...)
,
newInnerInstance(Class, Object, Object...)
public static void setField(Class<?> classWithStaticField, Object fieldValue)
classWithStaticField
- the class on which the static field is definedfieldValue
- the value to set the field toIllegalArgumentException
- if either the desired field is not found, or more than one issetField(Class, String, Object)
,
setField(Object, Object)
,
getField(Class, Class)
public static void setField(Class<?> classWithStaticField, String fieldName, Object fieldValue)
classWithStaticField
- the class on which the static field is definedfieldName
- the name of the field to setfieldValue
- the value to set the field toIllegalArgumentException
- if the desired field is not foundsetField(Class, Object)
,
setField(Object, String, Object)
,
getField(Class, String)
public static void setField(Object objectWithField, Object fieldValue)
IllegalArgumentException
- if either the desired field is not found, or more than one issetField(Object, String, Object)
,
setField(Class, String, Object)
,
getField(Object, String)
public static void setField(Object objectWithField, String fieldName, Object fieldValue)
objectWithField
- the instance on which to set the field valuefieldName
- the name of the field to setfieldValue
- the value to set the field toIllegalArgumentException
- if the desired field is not foundsetField(Class, String, Object)
,
setField(Object, Object)
,
getField(Object, String)