Top Description Constructors Methods
org.apache.commons.lang3.reflect

public Class FieldUtils

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, java.lang.reflect.AccessibleObject, .Field, .Modifier, java.util.ArrayList, .Collections, .List, .Objects, java.util.stream.Collectors, org.apache.commons.lang3.ArrayUtils, .ClassUtils, .JavaVersion, .StringUtils, .SystemUtils, .Validate

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

Constructor Summary

AccessConstructor and Description
public
FieldUtils()
Deprecated TODO Make private in 4.0.

FieldUtils instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
public static Field[]

Returns:

an array of Fields (possibly empty).
getAllFields
(final Class<?>
the Class to query
cls
)

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

public static List<Field>

Returns:

a list of Fields (possibly empty).
getAllFieldsList
(final Class<?>
the Class to query
cls
)

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

public static Field

Returns:

the Field object.
getDeclaredField
(final Class<?>
the Class to reflect, must not be null
cls
,
final String
the field name to obtain.
fieldName
)

Gets an accessible Field by name respecting scope.

public static Field

Returns:

the Field object
getDeclaredField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

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

public static Field

Returns:

the Field object.
getField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
)

Gets an accessible Field by name respecting scope.

public static Field

Returns:

the Field object.
getField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

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

public static List<Field>

Returns:

a list of Fields (possibly empty).
getFieldsListWithAnnotation
(final Class<?>
the Class to query.
cls
,
final Class<? extends Annotation>
the Annotation that must be present on a field to be matched.
annotationCls
)

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

public static Field[]

Returns:

an array of Fields (possibly empty).
getFieldsWithAnnotation
(final Class<?>
the Class to query.
cls
,
final Class<? extends Annotation>
the Annotation that must be present on a field to be matched
annotationCls
)

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

public static Object

Returns:

the value of the field.
readDeclaredField
(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
)

Reads the named public Field.

public static Object

Returns:

the Field object.
readDeclaredField
(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Gets a Field value by name.

public static Object

Returns:

the value of the field.
readDeclaredStaticField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
)

Gets the value of a static Field by name.

public static Object

Returns:

the Field object
readDeclaredStaticField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Gets the value of a static Field by name.

public static Object

Returns:

the field value
readField
(final Field
the field to use.
field
,
final Object
the object to call on, may be null for static fields.
target
)

Reads an accessible Field.

public static Object

Returns:

the field value
readField
(final Field
the field to use.
field
,
final Object
the object to call on, may be null for static fields.
target
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method.
forceAccess
)

Reads a Field.

public static Object

Returns:

the value of the field.
readField
(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
)

Reads the named public Field.

public static Object

Returns:

the field value
readField
(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Reads the named Field.

public static Object

Returns:

the value of the field.
readStaticField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
)

Reads the named public static Field.

public static Object

Returns:

the Field object.
readStaticField
(final Class<?>
the Class to reflect, must not be null.
cls
,
final String
the field name to obtain.
fieldName
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Reads the named static Field.

public static Object

Returns:

the field value.
readStaticField
(final Field
to read.
field
)

Reads an accessible static Field.

public static Object

Returns:

the field value.
readStaticField
(final Field
to read.
field
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method.
forceAccess
)

Reads a static Field.

public static void
removeFinalModifier(final Field
to remove the final modifier.
field
)

Removes the final modifier from a Field.

public static void
removeFinalModifier(final Field
to remove the final modifier.
field
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)
Deprecated As of Java 12, we can no longer drop the final modifier, thus rendering this method obsolete.

Removes the final modifier from a Field.

pack-priv static Field
setAccessible(final Field field, final boolean forceAccess)

public static void
writeDeclaredField(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
,
final Object
the new value.
value
)

Writes a public Field.

public static void
writeDeclaredField(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
,
final Object
the new value.
value
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Writes a public Field.

public static void
writeDeclaredStaticField(final Class<?>
Class on which the field is to be found.
cls
,
final String
to write.
fieldName
,
final Object
the new value.
value
)

Writes a named public static Field.

public static void
writeDeclaredStaticField(final Class<?>
Class on which the field is to be found.
cls
,
final String
to write
fieldName
,
final Object
the new value.
value
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Writes a named static Field.

public static void
writeField(final Field
to write.
field
,
final Object
the object to call on, may be null for static fields.
target
,
final Object
the new value.
value
)

Writes an accessible Field.

public static void
writeField(final Field
to write.
field
,
final Object
the object to call on, may be null for static fields
target
,
final Object
the new value.
value
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Writes a Field.

public static void
writeField(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
,
final Object
the new value.
value
)

Writes a public Field.

public static void
writeField(final Object
the object to reflect, must not be null.
target
,
final String
the field name to obtain.
fieldName
,
final Object
the new value.
value
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Writes a Field.

public static void
writeStaticField(final Class<?>
Class on which the field is to be found.
cls
,
final String
to write.
fieldName
,
final Object
the new value.
value
)

Writes a named public static Field.

public static void
writeStaticField(final Class<?>
Class on which the field is to be found.
cls
,
final String
to write.
fieldName
,
final Object
the new value.
value
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Writes a named static Field.

public static void
writeStaticField(final Field
to write.
field
,
final Object
the new value.
value
)

Writes a public static Field.

public static void
writeStaticField(final Field
to write.
field
,
final Object
the new value.
value
,
final boolean
whether to break scope restrictions using the AccessibleObject#setAccessible(boolean) method. false will only match public fields.
forceAccess
)

Writes a static Field.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait