Class ReflectionUtil
- java.lang.Object
-
- org.apache.logging.log4j.core.util.ReflectionUtil
-
public final class ReflectionUtil extends java.lang.ObjectUtility class for performing common reflective operations.- Since:
- 2.1
-
-
Constructor Summary
Constructors Modifier Constructor Description privateReflectionUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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.ObjectgetFieldValue(java.lang.reflect.Field field, java.lang.Object instance)Gets the value of aField, making it accessible if required.static java.lang.ObjectgetStaticFieldValue(java.lang.reflect.Field field)Gets the value of a staticField, making it accessible if required.static <T> Tinstantiate(java.lang.Class<T> clazz)Constructs a newTobject using the default constructor of its class.static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.Member>
booleanisAccessible(T member)Indicates whether or not aMemberis both public and is contained in a public class.static voidmakeAccessible(java.lang.reflect.Field field)Makes aFieldaccessibleif it is not public or if it is final.static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.Member>
voidmakeAccessible(T member)Makes aMemberaccessibleif the member is not public.static voidsetFieldValue(java.lang.reflect.Field field, java.lang.Object instance, java.lang.Object value)Sets the value of aField, making it accessible if required.static voidsetStaticFieldValue(java.lang.reflect.Field field, java.lang.Object value)Sets the value of a staticField, making it accessible if required.
-
-
-
Method Detail
-
isAccessible
public static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.Member> boolean isAccessible(T member)
Indicates whether or not aMemberis both public and is contained in a public class.- Type Parameters:
T- type of the object whose accessibility to test- Parameters:
member- the Member to check for public accessibility (must not benull).- Returns:
trueifmemberis public and contained in a public class.- Throws:
java.lang.NullPointerException- ifmemberisnull.
-
makeAccessible
public static <T extends java.lang.reflect.AccessibleObject & java.lang.reflect.Member> void makeAccessible(T member)
Makes aMemberaccessibleif the member is not public.- Type Parameters:
T- type of the object to make accessible- Parameters:
member- the Member to make accessible (must not benull).- Throws:
java.lang.NullPointerException- ifmemberisnull.
-
makeAccessible
public static void makeAccessible(java.lang.reflect.Field field)
Makes aFieldaccessibleif it is not public or if it is final.Note that using this method to make a
finalfield writable will most likely not work very well due to compiler optimizations and the like.- Parameters:
field- the Field to make accessible (must not benull).- Throws:
java.lang.NullPointerException- iffieldisnull.
-
getFieldValue
public static java.lang.Object getFieldValue(java.lang.reflect.Field field, java.lang.Object instance)Gets the value of aField, making it accessible if required.- Parameters:
field- the Field to obtain a value from (must not benull).instance- the instance to obtain the field value from ornullonly if the field is static.- Returns:
- the value stored by the field.
- Throws:
java.lang.NullPointerException- iffieldisnull, or ifinstanceisnullbutfieldis notstatic.- See Also:
Field.get(Object)
-
getStaticFieldValue
public static java.lang.Object getStaticFieldValue(java.lang.reflect.Field field)
Gets the value of a staticField, making it accessible if required.- Parameters:
field- the Field to obtain a value from (must not benull).- Returns:
- the value stored by the static field.
- Throws:
java.lang.NullPointerException- iffieldisnull, or iffieldis notstatic.- See Also:
Field.get(Object)
-
setFieldValue
public static void setFieldValue(java.lang.reflect.Field field, java.lang.Object instance, java.lang.Object value)Sets the value of aField, making it accessible if required.- Parameters:
field- the Field to write a value to (must not benull).instance- the instance to write the value to ornullonly if the field is static.value- the (possibly wrapped) value to write to the field.- Throws:
java.lang.NullPointerException- iffieldisnull, or ifinstanceisnullbutfieldis notstatic.- See Also:
Field.set(Object, Object)
-
setStaticFieldValue
public static void setStaticFieldValue(java.lang.reflect.Field field, java.lang.Object value)Sets the value of a staticField, making it accessible if required.- Parameters:
field- the Field to write a value to (must not benull).value- the (possibly wrapped) value to write to the field.- Throws:
java.lang.NullPointerException- iffieldisnull, or iffieldis notstatic.- See Also:
Field.set(Object, Object)
-
getDefaultConstructor
public static <T> java.lang.reflect.Constructor<T> getDefaultConstructor(java.lang.Class<T> clazz)
Gets the default (no-arg) constructor for a given class.- Type Parameters:
T- the type made by the constructor- Parameters:
clazz- the class to find a constructor for- Returns:
- the default constructor for the given class
- Throws:
java.lang.IllegalStateException- if no default constructor can be found
-
instantiate
public static <T> T instantiate(java.lang.Class<T> clazz)
Constructs a newTobject using the default constructor of its class. Any exceptions thrown by the constructor will be rethrown by this method, possibly wrapped in anUndeclaredThrowableException.- Type Parameters:
T- the type of the object to construct.- Parameters:
clazz- the class to use for instantiation.- Returns:
- a new instance of T made from its default constructor.
- Throws:
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 constructorsInternalException- wrapper of the underlying exception if checked
-
-