On this page

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

static Field[]

getAllFields(Class<?> cls)

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

static List<Field>

getAllFieldsList(Class<?> cls)

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

static Field

getDeclaredField(Class<?> cls, String fieldName)

Gets an accessible Field by name respecting scope.

static Field

getDeclaredField(Class<?> cls, String fieldName, boolean forceAccess)

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

static Field

getField(Class<?> cls, String fieldName)

Gets an accessible Field by name respecting scope.

static Field

getField(Class<?> cls, String fieldName, boolean forceAccess)

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

static List<Field>

getFieldsListWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)

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

static Field[]

getFieldsWithAnnotation(Class<?> cls, Class<? extends Annotation> annotationCls)

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

static Object

readDeclaredField(Object target, String fieldName)

Reads the named public Field.

static Object

readDeclaredField(Object target, String fieldName, boolean forceAccess)

Gets a Field value by name.

static Object

readDeclaredStaticField(Class<?> cls, String fieldName)

Gets the value of a static Field by name.

static Object

readDeclaredStaticField(Class<?> cls, String fieldName, boolean forceAccess)

Gets the value of a static Field by name.

static Object

readField(Object target, String fieldName)

Reads the named public Field.

static Object

readField(Object target, String fieldName, boolean forceAccess)

Reads the named Field.

static Object

readField(Field field, Object target)

Reads an accessible Field.

static Object

readField(Field field, Object target, boolean forceAccess)

Reads a Field.

static Object

readStaticField(Class<?> cls, String fieldName)

Reads the named public static Field.

static Object

readStaticField(Class<?> cls, String fieldName, boolean forceAccess)

Reads the named static Field.

static Object

readStaticField(Field field)

Reads an accessible static Field.

static Object

readStaticField(Field field, boolean forceAccess)

Reads a static Field.

static void

removeFinalModifier(Field field)

Removes the final modifier from a Field.

static void

removeFinalModifier(Field field, boolean forceAccess)

Deprecated. Removes the final modifier from a Field.

static void

writeDeclaredField(Object target, String fieldName, Object value)

Writes a public Field.

static void

writeDeclaredField(Object target, String fieldName, Object value, boolean forceAccess)

Writes a public Field.

static void

writeDeclaredStaticField(Class<?> cls, String fieldName, Object value)

Writes a named public static Field.

static void

writeDeclaredStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess)

Writes a named static Field.

static void

writeField(Object target, String fieldName, Object value)

Writes a public Field.

static void

writeField(Object target, String fieldName, Object value, boolean forceAccess)

Writes a Field.

static void

writeField(Field field, Object target, Object value)

Writes an accessible Field.

static void

writeField(Field field, Object target, Object value, boolean forceAccess)

Writes a Field.

static void

writeStaticField(Class<?> cls, String fieldName, Object value)

Writes a named public static Field.

static void

writeStaticField(Class<?> cls, String fieldName, Object value, boolean forceAccess)

Writes a named static Field.

static void

writeStaticField(Field field, Object value)

Writes a public static Field.

static void

writeStaticField(Field field, Object value, boolean forceAccess)

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:

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:

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:

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:

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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

Reads an accessible static Field.

Parameters:

field – to read.

Returns:

The field value.

Throws:
See also:

SecurityManager.checkPermission(Permission)

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

Reads a static Field.

Parameters:
Returns:

The field value.

Throws:
See also:

SecurityManager.checkPermission(Permission)

public static void removeFinalModifier(Field field)[source]

Removes the final modifier from a Field.

Parameters:

field – to remove the final modifier.

Throws:
Since:

3.2

See also:

SecurityManager.checkPermission(Permission)

@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:
Since:

3.3

See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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:
See also:

SecurityManager.checkPermission(Permission)

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

Writes a static Field.

Parameters:
Throws:
See also:

SecurityManager.checkPermission(Permission)