Class Field
- java.lang.Object
-
- java.lang.reflect.AccessibleObject
-
- java.lang.reflect.Field
-
- All Implemented Interfaces:
AnnotatedElement,Member
public final class Field extends AccessibleObject implements Member
This class represents a field. Information about the field can be accessed, and the field's value can be accessed dynamically.
-
-
Field Summary
Fields Modifier and Type Field Description static Comparator<Field>ORDER_BY_NAME_AND_DECLARING_CLASSOrders fields by their name and declaring class.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object other)Returns true ifotherhas the same declaring class, name and type as this field.Objectget(Object object)Returns the value of the field in the specified object.<A extends Annotation>
AgetAnnotation(Class<A> annotationType)Returns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).booleangetBoolean(Object object)Returns the value of the field in the specified object as aboolean.bytegetByte(Object object)Returns the value of the field in the specified object as abyte.chargetChar(Object object)Returns the value of the field in the specified object as achar.Annotation[]getDeclaredAnnotations()Returns, for this element, all annotations that are explicitly declared (not inherited).Class<?>getDeclaringClass()Returns the class that declares this member.intgetDexFieldIndex()Returns the index of this field's ID in its dex file.doublegetDouble(Object object)Returns the value of the field in the specified object as adouble.floatgetFloat(Object object)Returns the value of the field in the specified object as afloat.TypegetGenericType()Returns the generic type of this field.intgetInt(Object object)Returns the value of the field in the specified object as anint.longgetLong(Object object)Returns the value of the field in the specified object as along.intgetModifiers()Returns the modifiers for this field.StringgetName()Returns the name of this field.intgetOffset()Returns the offset of the field within an instance, or for static fields, the class.shortgetShort(Object object)Returns the value of the field in the specified object as ashort.Class<?>getType()Return theClassassociated with the type of this field.inthashCode()Returns an integer hash code for this object.booleanisAnnotationPresent(Class<? extends Annotation> annotationType)Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).booleanisEnumConstant()Indicates whether or not this field is an enumeration constant.booleanisSynthetic()Indicates whether or not this field is synthetic.voidset(Object object, Object value)Sets the value of the field in the specified object to the value.voidsetBoolean(Object object, boolean value)Sets the value of the field in the specified object to thebooleanvalue.voidsetByte(Object object, byte value)Sets the value of the field in the specified object to thebytevalue.voidsetChar(Object object, char value)Sets the value of the field in the specified object to thecharvalue.voidsetDouble(Object object, double value)Sets the value of the field in the specified object to thedoublevalue.voidsetFloat(Object object, float value)Sets the value of the field in the specified object to thefloatvalue.voidsetInt(Object object, int value)Set the value of the field in the specified object to theintvalue.voidsetLong(Object object, long value)Sets the value of the field in the specified object to thelongvalue.voidsetShort(Object object, short value)Sets the value of the field in the specified object to theshortvalue.StringtoGenericString()Returns the string representation of this field, including the field's generic type.StringtoString()Returns a string containing a concise, human-readable description of this field.-
Methods inherited from class java.lang.reflect.AccessibleObject
getAnnotations, isAccessible, setAccessible, setAccessible
-
-
-
-
Field Detail
-
ORDER_BY_NAME_AND_DECLARING_CLASS
public static final Comparator<Field> ORDER_BY_NAME_AND_DECLARING_CLASS
Orders fields by their name and declaring class.
-
-
Method Detail
-
getModifiers
public int getModifiers()
Returns the modifiers for this field. TheModifierclass should be used to decode the result.- Specified by:
getModifiersin interfaceMember- Returns:
- the modifiers for this field
- See Also:
Modifier
-
isEnumConstant
public boolean isEnumConstant()
Indicates whether or not this field is an enumeration constant.- Returns:
trueif this field is an enumeration constant,falseotherwise
-
isSynthetic
public boolean isSynthetic()
Indicates whether or not this field is synthetic.- Specified by:
isSyntheticin interfaceMember- Returns:
trueif this field is synthetic,falseotherwise
-
getName
public String getName()
Returns the name of this field.
-
getDeclaringClass
public Class<?> getDeclaringClass()
Description copied from interface:MemberReturns the class that declares this member.- Specified by:
getDeclaringClassin interfaceMember- Returns:
- the declaring class
-
getType
public Class<?> getType()
Return theClassassociated with the type of this field.- Returns:
- the type of this field
-
getDexFieldIndex
public int getDexFieldIndex()
Returns the index of this field's ID in its dex file.
-
getOffset
public int getOffset()
Returns the offset of the field within an instance, or for static fields, the class.
-
hashCode
public int hashCode()
Returns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.Equivalent to
getDeclaringClass().getName().hashCode() ^ getName().hashCode().- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(Object other)
Returns true ifotherhas the same declaring class, name and type as this field.- Overrides:
equalsin classObject- Parameters:
other- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
toGenericString
public String toGenericString()
Returns the string representation of this field, including the field's generic type.- Returns:
- the string representation of this field
-
getGenericType
public Type getGenericType()
Returns the generic type of this field.- Returns:
- the generic type
- Throws:
GenericSignatureFormatError- if the generic field signature is invalidTypeNotPresentException- if the generic type points to a missing typeMalformedParameterizedTypeException- if the generic type points to a type that cannot be instantiated for some reason
-
getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
Description copied from interface:AnnotatedElementReturns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Overrides:
getDeclaredAnnotationsin classAccessibleObject- Returns:
- an array of annotations declared for this element
-
getAnnotation
public <A extends Annotation> A getAnnotation(Class<A> annotationType)
Description copied from interface:AnnotatedElementReturns, for this element, the annotation with the specified type, ornullif no annotation with the specified type is present (including inherited annotations).- Specified by:
getAnnotationin interfaceAnnotatedElement- Overrides:
getAnnotationin classAccessibleObject- Parameters:
annotationType- the type of the annotation to search for- Returns:
- the annotation with the specified type or
null
-
isAnnotationPresent
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
Description copied from interface:AnnotatedElementIndicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- Overrides:
isAnnotationPresentin classAccessibleObject- Parameters:
annotationType- the type of the annotation to search for- Returns:
trueif the annotation exists,falseotherwise
-
get
public Object get(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object. This reproduces the effect ofobject.fieldNameIf the type of this field is a primitive type, the field value is automatically boxed.
If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.
If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value, possibly boxed
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getBoolean
public boolean getBoolean(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as aboolean. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getByte
public byte getByte(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as abyte. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getChar
public char getChar(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as achar. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getDouble
public double getDouble(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as adouble. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getFloat
public float getFloat(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as afloat. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getInt
public int getInt(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as anint. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getLong
public long getLong(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as along. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getShort
public short getShort(Object object) throws IllegalAccessException, IllegalArgumentException
Returns the value of the field in the specified object as ashort. This reproduces the effect ofobject.fieldNameIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
- Parameters:
object- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
set
public void set(Object object, Object value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to the value. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the field type is a primitive type, the value is automatically unboxed. If the unboxing fails, an IllegalArgumentException is thrown. If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setBoolean
public void setBoolean(Object object, boolean value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to thebooleanvalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setByte
public void setByte(Object object, byte value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to thebytevalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setChar
public void setChar(Object object, char value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to thecharvalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setDouble
public void setDouble(Object object, double value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to thedoublevalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setFloat
public void setFloat(Object object, float value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to thefloatvalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setInt
public void setInt(Object object, int value) throws IllegalAccessException, IllegalArgumentException
Set the value of the field in the specified object to theintvalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setLong
public void setLong(Object object, long value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to thelongvalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setShort
public void setShort(Object object, short value) throws IllegalAccessException, IllegalArgumentException
Sets the value of the field in the specified object to theshortvalue. This reproduces the effect ofobject.fieldName = valueIf this field is static, the object argument is ignored. Otherwise, if the object is
null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
- Parameters:
object- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
toString
public String toString()
Returns a string containing a concise, human-readable description of this field.The format of the string is:
- modifiers (if any)
- type
- declaring class name
- '.'
- field name
For example:
public static java.io.InputStream java.lang.System.in
-
-