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 |
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 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 instantiatedpublic 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)
Copyright © 2006–2017. All rights reserved.