org.apache.logging.log4j.core.util
Class ReflectionUtil

java.lang.Object
  extended by org.apache.logging.log4j.core.util.ReflectionUtil

public final class ReflectionUtil
extends Object

Utility class for performing common reflective operations.

Since:
2.1

Method Summary
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>
boolean
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>
void
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isAccessible

public static <T extends AccessibleObject & Member> boolean isAccessible(T member)
Indicates whether or not a Member is both public and is contained in a public class.

Parameters:
member - the Member to check for public accessibility (must not be null).
Returns:
true if member is public and contained in a public class.
Throws:
NullPointerException - if member is null.

makeAccessible

public static <T extends AccessibleObject & Member> void makeAccessible(T member)
Makes a Member accessible if the member is not public.

Parameters:
member - the Member to make accessible (must not be null).
Throws:
NullPointerException - if member is null.

makeAccessible

public static void makeAccessible(Field field)
Makes a 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.

Parameters:
field - the Field to make accessible (must not be null).
Throws:
NullPointerException - if field is null.

getFieldValue

public static Object getFieldValue(Field field,
                                   Object instance)
Gets the value of a Field, making it accessible if required.

Parameters:
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.
Returns:
the value stored by the field.
Throws:
NullPointerException - if field is null, or if instance is null but field is not static.
See Also:
Field.get(Object)

getStaticFieldValue

public static Object getStaticFieldValue(Field field)
Gets the value of a static Field, making it accessible if required.

Parameters:
field - the Field to obtain a value from (must not be null).
Returns:
the value stored by the static field.
Throws:
NullPointerException - if field is null, or if field is not static.
See Also:
Field.get(Object)

setFieldValue

public static void setFieldValue(Field field,
                                 Object instance,
                                 Object value)
Sets the value of a Field, making it accessible if required.

Parameters:
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.
Throws:
NullPointerException - if field is null, or if instance is null but field is not static.
See Also:
Field.set(Object, Object)

setStaticFieldValue

public static void setStaticFieldValue(Field field,
                                       Object value)
Sets the value of a static Field, making it accessible if required.

Parameters:
field - the Field to write a value to (must not be null).
value - the (possibly wrapped) value to write to the field.
Throws:
NullPointerException - if field is null, or if field is not static.
See Also:
Field.set(Object, Object)

getDefaultConstructor

public static <T> Constructor<T> getDefaultConstructor(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:
IllegalStateException - if no default constructor can be found

instantiate

public static <T> T instantiate(Class<T> clazz)
Constructs a new 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.

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:
IllegalArgumentException - if the given class is abstract, an interface, an array class, a primitive type, or void
IllegalStateException - if access is denied to the constructor, or there are no default constructors


Copyright © 1999-2014 Apache Software Foundation. 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.