public final class ReflectionUtil extends Object
Modifier and Type | Method | Description |
---|---|---|
static <T> Constructor<T> |
getDefaultConstructor(Class<T> clazz) |
Gets the default (no-arg) constructor for a given class.
|
static Object |
getFieldValue(Field field,
Object instance) |
Gets the value of a
Field , making it accessible if required. |
static Object |
getStaticFieldValue(Field field) |
Gets the value of a static
Field , making it accessible if required. |
static <T> T |
instantiate(Class<T> clazz) |
Constructs a new
T object using the default constructor of its class. |
static <T extends AccessibleObject & Member> |
isAccessible(T member) |
Indicates whether or not a
Member is both public and is contained in a public class. |
static void |
makeAccessible(Field field) |
Makes a
Field accessible if it is not public or if it is final. |
static <T extends AccessibleObject & Member> |
makeAccessible(T member) |
Makes a
Member accessible if the member is not public. |
static void |
setFieldValue(Field field,
Object instance,
Object value) |
Sets the value of a
Field , making it accessible if required. |
static void |
setStaticFieldValue(Field field,
Object value) |
Sets the value of a static
Field , making it accessible if required. |
public static <T extends AccessibleObject & Member> boolean isAccessible(T member)
Member
is both public and is contained in a public class.T
- type of the object whose accessibility to testmember
- the Member to check for public accessibility (must not be null
).true
if member
is public and contained in a public class.NullPointerException
- if member
is null
.public static <T extends AccessibleObject & Member> void makeAccessible(T member)
Member
accessible
if the member is not public.T
- type of the object to make accessiblemember
- the Member to make accessible (must not be null
).NullPointerException
- if member
is null
.public static void makeAccessible(Field field)
Field
accessible
if it is not public or if it is final.
Note that using this method to make a final
field writable will most likely not work very well due to
compiler optimizations and the like.
field
- the Field to make accessible (must not be null
).NullPointerException
- if field
is null
.public static Object getFieldValue(Field field, Object instance)
Field
, making it accessible if required.field
- the Field to obtain a value from (must not be null
).instance
- the instance to obtain the field value from or null
only if the field is static.NullPointerException
- if field
is null
, or if instance
is null
but
field
is not static
.Field.get(Object)
public static Object getStaticFieldValue(Field field)
Field
, making it accessible if required.field
- the Field to obtain a value from (must not be null
).NullPointerException
- if field
is null
, or if field
is not static
.Field.get(Object)
public static void setFieldValue(Field field, Object instance, Object value)
Field
, making it accessible if required.field
- the Field to write a value to (must not be null
).instance
- the instance to write the value to or null
only if the field is static.value
- the (possibly wrapped) value to write to the field.NullPointerException
- if field
is null
, or if instance
is null
but
field
is not static
.Field.set(Object, Object)
public static void setStaticFieldValue(Field field, Object value)
Field
, making it accessible if required.field
- the Field to write a value to (must not be null
).value
- the (possibly wrapped) value to write to the field.NullPointerException
- if field
is null
, or if field
is not static
.Field.set(Object, Object)
public static <T> Constructor<T> getDefaultConstructor(Class<T> clazz)
T
- the type made by the constructorclazz
- the class to find a constructor forIllegalStateException
- if no default constructor can be foundpublic static <T> T instantiate(Class<T> clazz)
T
object using the default constructor of its class. Any exceptions thrown by the
constructor will be rethrown by this method, possibly wrapped in an
UndeclaredThrowableException
.T
- the type of the object to construct.clazz
- the class to use for instantiation.IllegalArgumentException
- if the given class is abstract, an interface, an array class, a primitive type,
or voidIllegalStateException
- if access is denied to the constructor, or there are no default constructorsCopyright © 1999-2018. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.