APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang.reflect

public final Class Field

extends AccessibleObject
implements Member
Class Inheritance
All Implemented Interfaces
java.lang.reflect.Member
Imports
java.lang.annotation.Annotation, java.net.URL, java.security.CodeSource, java.util.Map, .Set, .Objects, jdk.internal.access.SharedSecrets, jdk.internal.event.FinalFieldMutationEvent, jdk.internal.loader.ClassLoaders, jdk.internal.misc.VM, jdk.internal.module.ModuleBootstrap, .Modules, jdk.internal.reflect.CallerSensitive, .FieldAccessor, .Reflection, jdk.internal.vm.annotation.ForceInline, .Stable, sun.reflect.generics.repository.FieldRepository, sun.reflect.generics.factory.CoreReflectionFactory, .GenericsFactory, sun.reflect.generics.scope.ClassScope, sun.reflect.annotation.AnnotationParser, .AnnotationSupport, .TypeAnnotation, .TypeAnnotationParser

A Field provides information about, and dynamic access to, a single field of a class or an interface. The reflected field may be a class (static) field or an instance field.

A Field permits widening conversions to occur during a get or set access operation, but throws an IllegalArgumentException if a narrowing conversion would occur.

Authors
Kenneth Russell, Nakul Saraiya
Since
1.1
See Also
Member, java.lang.Class, java.lang.Class#getFields(), java.lang.Class#getField(String), java.lang.Class#getDeclaredFields(), java.lang.Class#getDeclaredField(String)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static interface
Field.FieldSetter

A function that sets a field to a value.

Field Summary

Modifier and TypeField and Description
private final byte[]
private final Class<?>
private transient volatile Map<Class<? extends Annotation>, Annotation>
private FieldAccessor
private transient volatile FieldRepository
private final int
private final String
private FieldAccessor
private Field
private final transient String
private final int
private final boolean
private final Class<?>
Inherited from java.lang.reflect.AccessibleObject:
accessCheckCacheoverridereflectionFactory

Constructor Summary

AccessConstructor and Description
pack-priv
Field(Class<?> declaringClass, String name, Class<?> type, int modifiers, boolean trustedFinal, int slot, String signature, byte[] annotations)

Package-private constructor

Method Summary

Modifier and TypeMethod and Description
public Set<AccessFlag>

Returns:

an unmodifiable set of the access flags for this field, possibly empty
accessFlags
()

Overrides default java.lang.reflect.Member.accessFlags.

Returns an unmodifiable set of the access flags for this field, possibly empty.

private FieldAccessor
private FieldAccessor
private String
callerNotAllowedToMutateMessage(Class<?> caller, boolean unreflect)

Returns the exception message for the IllegalAccessException when this final field cannot be mutated because the caller module is not allowed to mutate final fields.

private String
cannotSetFieldMessage(Class<?> caller, boolean unreflect)

Returns a message to indicate that the caller cannot set/unreflect this final field.

private String
cannotSetFieldMessage(String prefix, boolean unreflect)

Returns a message to indicate that a field cannot be set/unreflected.

private void
checkAccess(Class<?> caller, Object obj)

pack-priv void
checkAllowedToUnreflectFinalSetter(Class<?> caller)

Check that the caller is allowed to unreflect for mutation a final instance field in a normal class.

pack-priv void
checkCanSetAccessible(Class<?> caller)

Overrides java.lang.reflect.AccessibleObject.checkCanSetAccessible.

If the given AccessibleObject is a Constructor, Method or Field then checks that its declaring class is in a package that can be accessed by the given caller of setAccessible.

pack-priv Field
copy()

Package-private routine (exposed to java.lang.Class via ReflectAccess) which returns a copy of this Field.

private Map<Class<? extends Annotation>, Annotation>
public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Compares this Field against the specified object.

private String
finalFieldMutationWarning(Class<?> caller, boolean unreflect)

Returns the warning message to print when this final field is mutated by the given possibly-null caller.

public Object

Returns:

the value of the represented field in object obj; primitive values are wrapped in an appropriate object before being returned
get
(Object
object from which the represented field's value is to be extracted
obj
)

Returns the value of the field represented by this Field, on the specified object.

public AnnotatedType

Returns:

an object representing the declared type of the field represented by this Field
getAnnotatedType
()

Returns an AnnotatedType object that represents the use of a type to specify the declared type of the field represented by this Field.

public <T extends Annotation> T
getAnnotation(Class<T> annotationClass)

Overrides java.lang.reflect.AccessibleObject.getAnnotation.

Implements java.lang.reflect.AnnotatedElement.getAnnotation.

Returns this element's annotation for the specified type if such an annotation is present, else null.

public <T extends Annotation> T[]
getAnnotationsByType(Class<T> annotationClass)

Overrides java.lang.reflect.AccessibleObject.getAnnotationsByType.

Overrides default java.lang.reflect.AnnotatedElement.getAnnotationsByType.

Returns annotations that are associated with this element.

public boolean

Returns:

the value of the boolean field
getBoolean
(Object
the object to extract the boolean value from
obj
)

Gets the value of a static or instance boolean field.

public byte

Returns:

the value of the byte field
getByte
(Object
the object to extract the byte value from
obj
)

Gets the value of a static or instance byte field.

public char

Returns:

the value of the field converted to type char
getChar
(Object
the object to extract the char value from
obj
)

Gets the value of a static or instance field of type char or of another primitive type convertible to type char via a widening conversion.

public Annotation[]
public Class<?>
getDeclaringClass()

Implements java.lang.reflect.Member.getDeclaringClass.

Returns the Class object representing the class or interface that declares the field represented by this Field object.

public double

Returns:

the value of the field converted to type double
getDouble
(Object
the object to extract the double value from
obj
)

Gets the value of a static or instance field of type double or of another primitive type convertible to type double via a widening conversion.

private GenericsFactory
private FieldAccessor
public float

Returns:

the value of the field converted to type float
getFloat
(Object
the object to extract the float value from
obj
)

Gets the value of a static or instance field of type float or of another primitive type convertible to type float via a widening conversion.

private FieldRepository
private String
public Type

Returns:

a Type object that represents the declared type for the field represented by this Field object
getGenericType
()

Returns a Type object that represents the declared type for the field represented by this Field object.

public int

Returns:

the value of the field converted to type int
getInt
(Object
the object to extract the int value from
obj
)

Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.

public long

Returns:

the value of the field converted to type long
getLong
(Object
the object to extract the long value from
obj
)

Gets the value of a static or instance field of type long or of another primitive type convertible to type long via a widening conversion.

public int
getModifiers()

Implements java.lang.reflect.Member.getModifiers.

Returns the Java language modifiers for the field represented by this Field object, as an integer.

public String
getName()

Implements java.lang.reflect.Member.getName.

Returns the name of the field represented by this Field object.

private FieldAccessor
pack-priv Field
getRoot()

Overrides java.lang.reflect.AccessibleObject.getRoot.

Returns the root AccessibleObject; or null if this object is the root.

public short

Returns:

the value of the field converted to type short
getShort
(Object
the object to extract the short value from
obj
)

Gets the value of a static or instance field of type short or of another primitive type convertible to type short via a widening conversion.

public Class<?>

Returns:

a Class object identifying the declared type of the field represented by this object
getType
()

Returns a Class object that identifies the declared type for the field represented by this Field object.

private native byte[]
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hashcode for this Field.

public boolean

Returns:

true if and only if this field represents an element of an enumerated class.
isEnumConstant
()

Returns true if this field represents an element of an enumerated class; returns false otherwise.

private boolean
isFinalDeeplyAccessible(Class<?> caller)

Returns true if this final field is "deeply accessible" to the caller.

private boolean
isFinalInstanceInNormalClass()

Return true if this field is a final instance field in a normal class (not a record class or hidden class),

public boolean

Returns:

true if and only if this field is a synthetic field as defined by the Java Language Specification.
isSynthetic
()

Implements java.lang.reflect.Member.isSynthetic.

Returns true if this field is a synthetic field; returns false otherwise.

pack-priv boolean
private String
private Module
moduleToCheck(Class<?> caller)

Returns the Module to use for access checks with the given caller.

private String
notAccessibleToCallerMessage(Class<?> caller, boolean unreflect)

Returns the message for an IllegalAccessException when a final field cannot be mutated because the declaring class is in a package that is not "deeply accessible" to the caller.

private String
notAccessibleToNoCallerMessage(boolean unreflect)

Returns the message for an IllegalAccessException when a field is not accessible to a JNI attached thread.

private void
postSetFinal(Class<?> caller, boolean unreflect)

Invoke after mutating a final instance field, or when unreflecting a final instance field for mutation, to print a warning and record a JFR event.

private void
preSetFinal(Class<?> caller, boolean unreflect)

Invoke before attempting to mutate, or unreflect for mutation, a final instance field in a normal class.

public void
set(Object
the object whose field should be modified
obj
,
Object
the new value for the field of obj being modified
value
)

Sets the field represented by this Field object on the specified object argument to the specified new value.

public void
setAccessible(boolean flag)

Overrides java.lang.reflect.AccessibleObject.setAccessible.

Set the accessible flag for this reflected object to the indicated boolean value.

public void
setBoolean(Object
the object whose field should be modified
obj
,
boolean
the new value for the field of obj being modified
z
)

Sets the value of a field as a boolean on the specified object.

public void
setByte(Object
the object whose field should be modified
obj
,
byte
the new value for the field of obj being modified
b
)

Sets the value of a field as a byte on the specified object.

public void
setChar(Object
the object whose field should be modified
obj
,
char
the new value for the field of obj being modified
c
)

Sets the value of a field as a char on the specified object.

public void
setDouble(Object
the object whose field should be modified
obj
,
double
the new value for the field of obj being modified
d
)

Sets the value of a field as a double on the specified object.

private void
private void
setFinal(Class<?> caller, Object obj, Field.FieldSetter setter)

Attempts to set a final field.

public void
setFloat(Object
the object whose field should be modified
obj
,
float
the new value for the field of obj being modified
f
)

Sets the value of a field as a float on the specified object.

public void
setInt(Object
the object whose field should be modified
obj
,
int
the new value for the field of obj being modified
i
)

Sets the value of a field as an int on the specified object.

public void
setLong(Object
the object whose field should be modified
obj
,
long
the new value for the field of obj being modified
l
)

Sets the value of a field as a long on the specified object.

private void
public void
setShort(Object
the object whose field should be modified
obj
,
short
the new value for the field of obj being modified
s
)

Sets the value of a field as a short on the specified object.

public String

Returns:

a string describing this Field, including its generic type
toGenericString
()

Returns a string describing this Field, including its generic type.

pack-priv String
toShortString()

Overrides java.lang.reflect.AccessibleObject.toShortString.

Returns a short descriptive string to describe this object in log messages.

public String

Returns:

a string describing this Field
toString
()

Overrides java.lang.Object.toString.

Returns a string describing this Field.

Inherited from java.lang.reflect.AccessibleObject:
canAccesscheckAccesscheckCanSetAccessiblegetAnnotationsgetDeclaredAnnotationgetDeclaredAnnotationsByTypeisAccessibleisAnnotationPresentsetAccessiblesetAccessible0trySetAccessibleverifyAccess