public final class ReflectionUtil
extends java.lang.Object
Modifier | Constructor and Description |
---|---|
private |
ReflectionUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> java.lang.reflect.Constructor<T> |
getDefaultConstructor(java.lang.Class<T> clazz)
Gets the default (no-arg) constructor for a given class.
|
static java.lang.Object |
getFieldValue(java.lang.reflect.Field field,
java.lang.Object instance)
Gets the value of a
Field , making it accessible if required. |
static java.lang.Object |
getStaticFieldValue(java.lang.reflect.Field field)
Gets the value of a static
Field , making it accessible if required. |
static <T> T |
instantiate(java.lang.Class<T> clazz)
Constructs a new
T object using the default constructor of its class. |
static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.Member> |
isAccessible(T member)
Indicates whether or not a
Member is both public and is contained in a public class. |
static void |
makeAccessible(java.lang.reflect.Field field)
Makes a
Field accessible if it is not public or if it is final. |
static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.Member> |
makeAccessible(T member)
Makes a
Member accessible if the member is not public. |
static void |
setFieldValue(java.lang.reflect.Field field,
java.lang.Object instance,
java.lang.Object value)
Sets the value of a
Field , making it accessible if required. |
static void |
setStaticFieldValue(java.lang.reflect.Field field,
java.lang.Object value)
Sets the value of a static
Field , making it accessible if required. |
public static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.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.java.lang.NullPointerException
- if member
is null
.public static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.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
).java.lang.NullPointerException
- if member
is null
.public static void makeAccessible(java.lang.reflect.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
).java.lang.NullPointerException
- if field
is null
.public static java.lang.Object getFieldValue(java.lang.reflect.Field field, java.lang.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.java.lang.NullPointerException
- if field
is null
, or if instance
is null
but
field
is not static
.Field.get(Object)
public static java.lang.Object getStaticFieldValue(java.lang.reflect.Field field)
Field
, making it accessible if required.field
- the Field to obtain a value from (must not be null
).java.lang.NullPointerException
- if field
is null
, or if field
is not static
.Field.get(Object)
public static void setFieldValue(java.lang.reflect.Field field, java.lang.Object instance, java.lang.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.java.lang.NullPointerException
- if field
is null
, or if instance
is null
but
field
is not static
.Field.set(Object, Object)
public static void setStaticFieldValue(java.lang.reflect.Field field, java.lang.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.java.lang.NullPointerException
- if field
is null
, or if field
is not static
.Field.set(Object, Object)
public static <T> java.lang.reflect.Constructor<T> getDefaultConstructor(java.lang.Class<T> clazz)
T
- the type made by the constructorclazz
- the class to find a constructor forjava.lang.IllegalStateException
- if no default constructor can be foundpublic static <T> T instantiate(java.lang.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.java.lang.IllegalArgumentException
- if the given class is abstract, an interface, an array class, a primitive type,
or voidjava.lang.IllegalStateException
- if access is denied to the constructor, or there are no default constructors