FieldUtils

public class org.apache.commons.lang3.reflect.FieldUtils[source]

Utilities for working with Fields by reflection. Adapted and refactored from the dormant [reflect] Commons sandbox component.

The ability is provided to break the scoping restrictions coded by the programmer. This can allow fields to be changed that shouldn’t be. This facility should be used with care.

Since:

2.5

Constructors

FieldUtils()

Deprecated. FieldUtils instances should NOT be constructed in standard programming.

@Deprecated public FieldUtils()[source]

Deprecated: TODO Make private in 4.0.

FieldUtils instances should NOT be constructed in standard programming.

This constructor is public to permit tools that require a JavaBean instance to operate.

Methods

getAllFields(Class)

Gets all fields of the given class and its parents (if any).

getAllFieldsList(Class)

Gets all fields of the given class and its parents (if any).

getDeclaredField(Class, String)

Gets an accessible Field by name respecting scope.

getDeclaredField(Class, String, boolean)

Gets an accessible Field by name, breaking scope if requested.

getField(Class, String)

Gets an accessible Field by name respecting scope.

getField(Class, String, boolean)

Gets an accessible Field by name, breaking scope if requested.

getFieldsListWithAnnotation(Class, Class)

Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.

getFieldsWithAnnotation(Class, Class)

Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.

readDeclaredField(Object, String)

Reads the named public Field.

readDeclaredField(Object, String, boolean)

Gets a Field value by name.

readDeclaredStaticField(Class, String)

Gets the value of a static Field by name.

readDeclaredStaticField(Class, String, boolean)

Gets the value of a static Field by name.

readField(Object, String)

Reads the named public Field.

readField(Object, String, boolean)

Reads the named Field.

readField(Field, Object)

Reads an accessible Field.

readField(Field, Object, boolean)

Reads a Field.

readStaticField(Class, String)

Reads the named public static Field.

readStaticField(Class, String, boolean)

Reads the named static Field.

readStaticField(Field)

Reads an accessible static Field.

readStaticField(Field, boolean)

Reads a static Field.

removeFinalModifier(Field)

Removes the final modifier from a Field.

removeFinalModifier(Field, boolean)

Deprecated. Removes the final modifier from a Field.

writeDeclaredField(Object, String, Object)

Writes a public Field.

writeDeclaredField(Object, String, Object, boolean)

Writes a public Field.

writeDeclaredStaticField(Class, String, Object)

Writes a named public static Field.

writeDeclaredStaticField(Class, String, Object, boolean)

Writes a named static Field.

writeField(Object, String, Object)

Writes a public Field.

writeField(Object, String, Object, boolean)

Writes a Field.

writeField(Field, Object, Object)

Writes an accessible Field.

writeField(Field, Object, Object, boolean)

Writes a Field.

writeStaticField(Class, String, Object)

Writes a named public static Field.

writeStaticField(Class, String, Object, boolean)

Writes a named static Field.

writeStaticField(Field, Object)

Writes a public static Field.

writeStaticField(Field, Object, boolean)

Writes a static Field.

public static Field[] getAllFields(Class<?> cls)[source]

Gets all fields of the given class and its parents (if any).

Parameters:

cls – the Class to query

Returns:

An array of Fields (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class is null.

Since:

3.2

public static List<Field> getAllFieldsList(Class<?> cls)[source]

Gets all fields of the given class and its parents (if any).

Parameters:

cls – the Class to query

Returns:

A list of Fields (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class is null.

Since:

3.2

public static Field getDeclaredField(Class<?> cls, String fieldName)[source]

Gets an accessible Field by name respecting scope. Only the specified class will be considered.

Parameters:
  • cls – the Class to reflect, must not be null

  • fieldName – the field name to obtain.

Returns:

The Field object.

Throws:
  • java.lang.NullPointerException – Thrown if the class is null.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, or empty.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Field getDeclaredField(Class<?> cls, String fieldName, boolean forceAccess)[source]

Gets an accessible Field by name, breaking scope if requested. Only the specified class will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Returns:

The Field object

Throws:
  • java.lang.NullPointerException – Thrown if the class is null.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, or empty.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Field getField(Class<?> cls, String fieldName)[source]

Gets an accessible Field by name respecting scope. Superclasses/interfaces will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

Returns:

The Field object.

Throws:
  • java.lang.NullPointerException – Thrown if the class is null.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, or empty.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Field getField(Class<?> cls, String fieldName, boolean forceAccess)[source]

Gets an accessible Field by name, breaking scope if requested. Superclasses/interfaces will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Returns:

The Field object.

Throws:
  • java.lang.NullPointerException – Thrown if the class is null.

  • java.lang.IllegalArgumentException – Thrown if the field name is blank or empty or is matched at multiple places in the inheritance hierarchy.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static List<Field> getFieldsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)[source]

Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.

Parameters:
  • cls – the Class to query.

  • annotationCls – the Annotation that must be present on a field to be matched.

Returns:

A list of Fields (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class or annotation are null.

Since:

3.4

public static Field[] getFieldsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)[source]

Gets all fields of the given class and its parents (if any) that are annotated with the given annotation.

Parameters:
  • cls – the Class to query.

  • annotationCls – the Annotation that must be present on a field to be matched

Returns:

An array of Fields (possibly empty).

Throws:

java.lang.NullPointerException – Thrown if the class or annotation are null.

Since:

3.4

public static Object readDeclaredField(Object target, String fieldName) throws IllegalAccessException[source]

Reads the named public Field. Only the class of the specified object will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

Returns:

The value of the field.

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank or empty, or could not be found.

  • java.lang.IllegalAccessException – Thrown if the named field is not public.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readDeclaredField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException[source]

Gets a Field value by name. Only the class of the specified object will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Returns:

The Field object.

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank or empty, or could not be found.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readDeclaredStaticField(Class<?> cls, String fieldName) throws IllegalAccessException[source]

Gets the value of a static Field by name. The field must be public. Only the specified class will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

Returns:

The value of the field.

Throws:
  • java.lang.NullPointerException – Thrown if the class is null, or the field could not be found.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, empty, or is not static.

  • java.lang.IllegalAccessException – Thrown if the field is not accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException[source]

Gets the value of a static Field by name. Only the specified class will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Returns:

The Field object

Throws:
  • java.lang.NullPointerException – Thrown if the class is null, or the field could not be found.

  • java.lang.IllegalArgumentException – Thrown if the field name is blank or empty, is not static.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readField(Object target, String fieldName) throws IllegalAccessException[source]

Reads the named public Field. Superclasses will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

Returns:

The value of the field.

Throws:
  • java.lang.NullPointerException – Thrown if the target is null.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, empty, or could not be found.

  • java.lang.IllegalAccessException – Thrown if the named field is not public.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readField(Object target, String fieldName, boolean forceAccess) throws IllegalAccessException[source]

Reads the named Field. Superclasses will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Returns:

The field value

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, empty, or could not be found.

  • java.lang.IllegalAccessException – Thrown if the named field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readField(Field field, Object target) throws IllegalAccessException[source]

Reads an accessible Field.

Parameters:
  • field – the field to use.

  • target – the object to call on, may be null for static fields.

Returns:

The field value

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalAccessException – Thrown if the field is not accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readField(Field field, Object target, boolean forceAccess) throws IllegalAccessException[source]

Reads a Field.

Parameters:
  • field – the field to use.

  • target – the object to call on, may be null for static fields.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method.

Returns:

The field value

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

    Thrown if an underlying accessible object’s method denies the request.

See also:
public static Object readStaticField(Class<?> cls, String fieldName) throws IllegalAccessException[source]

Reads the named public static Field. Superclasses will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

Returns:

The value of the field.

Throws:
  • java.lang.NullPointerException – Thrown if the class is null, or the field could not be found.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank or empty, or is not static.

  • java.lang.IllegalAccessException – Thrown if the field is not accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readStaticField(Class<?> cls, String fieldName, boolean forceAccess) throws IllegalAccessException[source]

Reads the named static Field. Superclasses will be considered.

Parameters:
  • cls – the Class to reflect, must not be null.

  • fieldName – the field name to obtain.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Returns:

The Field object.

Throws:
  • java.lang.NullPointerException – Thrown if the class is null, or the field could not be found.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank or empty, or is not static.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readStaticField(Field field) throws IllegalAccessException[source]

Reads an accessible static Field.

Parameters:

field – to read.

Returns:

The field value.

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalArgumentException – Thrown if the field is not static.

  • java.lang.IllegalAccessException – Thrown if the field is not accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static Object readStaticField(Field field, boolean forceAccess) throws IllegalAccessException[source]

Reads a static Field.

Parameters:
Returns:

The field value.

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalArgumentException – Thrown if the field is not static.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void removeFinalModifier(Field field)[source]

Removes the final modifier from a Field.

Parameters:

field – to remove the final modifier.

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

Since:

3.2

@Deprecated public static void removeFinalModifier(Field field, boolean forceAccess)[source]

Deprecated: As of Java 12, we can no longer drop the final modifier, thus rendering this method obsolete. The JDK discussion about this change can be found here: https://mail.openjdk.java.net/pipermail/core-libs-dev/2018-November/056486.html

Removes the final modifier from a Field.

Parameters:
Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

Since:

3.3

public static void writeDeclaredField(Object target, String fieldName, Object value) throws IllegalAccessException[source]

Writes a public Field. Only the specified class will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

  • value – the new value.

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank or empty, or could not be found, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]

Writes a public Field. Only the specified class will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

  • value – the new value.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Throws:
  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank or empty, or could not be found, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException[source]

Writes a named public static Field. Only the specified class will be considered.

Parameters:
  • cls – Class on which the field is to be found.

  • fieldName – to write.

  • value – the new value.

Throws:
  • java.lang.NullPointerException – Thrown if cls is null or the field cannot be located.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, empty, not static, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not public or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]

Writes a named static Field. Only the specified class will be considered.

Parameters:
  • cls – Class on which the field is to be found.

  • fieldName – to write

  • value – the new value.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Throws:
  • java.lang.NullPointerException – Thrown if cls is null or the field cannot be located.

  • java.lang.IllegalArgumentException – Thrown if the field name is null, blank, empty, not static, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeField(Object target, String fieldName, Object value) throws IllegalAccessException[source]

Writes a public Field. Superclasses will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

  • value – the new value.

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank, empty, or could not be found, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]

Writes a Field. Superclasses will be considered.

Parameters:
  • target – the object to reflect, must not be null.

  • fieldName – the field name to obtain.

  • value – the new value.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank, empty, or could not be found, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeField(Field field, Object target, Object value) throws IllegalAccessException[source]

Writes an accessible Field.

Parameters:
  • field – to write.

  • target – the object to call on, may be null for static fields.

  • value – the new value.

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalArgumentException – Thrown if value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not accessible or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeField(Field field, Object target, Object value, boolean forceAccess) throws IllegalAccessException[source]

Writes a Field.

Parameters:
  • field – to write.

  • target – the object to call on, may be null for static fields

  • value – the new value.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalArgumentException – Thrown if value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeStaticField(Class<?> cls, String fieldName, Object value) throws IllegalAccessException[source]

Writes a named public static Field. Superclasses will be considered.

Parameters:
  • cls – Class on which the field is to be found.

  • fieldName – to write.

  • value – the new value.

Throws:
  • java.lang.NullPointerException – Thrown if target is null.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank or empty, the field cannot be located or is not static, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not public or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException[source]

Writes a named static Field. Superclasses will be considered.

Parameters:
  • cls – Class on which the field is to be found.

  • fieldName – to write.

  • value – the new value.

  • forceAccess – whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.

Throws:
  • java.lang.NullPointerException – Thrown if cls is null or the field cannot be located.

  • java.lang.IllegalArgumentException – Thrown if fieldName is null, blank or empty, the field not static, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeStaticField(Field field, Object value) throws IllegalAccessException[source]

Writes a public static Field.

Parameters:
  • field – to write.

  • value – the new value.

Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalArgumentException – Thrown if the field is not static, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not public or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission

public static void writeStaticField(Field field, Object value, boolean forceAccess) throws IllegalAccessException[source]

Writes a static Field.

Parameters:
Throws:
  • java.lang.NullPointerException – Thrown if the field is null.

  • java.lang.IllegalArgumentException – Thrown if the field is not static, or value is not assignable.

  • java.lang.IllegalAccessException – Thrown if the field is not made accessible or is final.

  • java.lang.SecurityException – Thrown if an underlying accessible object’s method denies the request.

See also:

SecurityManager#checkPermission