Class 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 Details

    • 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 Details

    • getSignatureAttribute

      protected String getSignatureAttribute()
    • isSynthetic

      public boolean isSynthetic()
      Indicates whether or not this field is synthetic.
      Specified by:
      isSynthetic in interface Member
      Returns:
      true if this field is synthetic, false otherwise
    • toGenericString

      public String toGenericString()
      Returns the string representation of this field, including the field's generic type.
      Returns:
      the string representation of this field
    • isEnumConstant

      public boolean isEnumConstant()
      Indicates whether or not this field is an enumeration constant.
      Returns:
      true if this field is an enumeration constant, false otherwise
    • getGenericType

      public Type getGenericType()
      Returns the generic type of this field.
      Returns:
      the generic type
      Throws:
      GenericSignatureFormatError - if the generic field signature is invalid
      TypeNotPresentException - if the generic type points to a missing type
      MalformedParameterizedTypeException - if the generic type points to a type that cannot be instantiated for some reason
    • getDeclaredAnnotations

      protected Annotation[] getDeclaredAnnotations​(boolean copy)
      Overrides:
      getDeclaredAnnotations in class AccessibleObject
    • equals

      public boolean equals​(Object object)
      Indicates whether or not the specified object is equal to this field. To be equal, the specified object must be an instance of Field with the same declaring class, type and name as this field.
      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare
      Returns:
      true if the specified object is equal to this method, false otherwise
      See Also:
      hashCode()
    • get

      Returns the value of the field in the specified object. This reproduces the effect of object.fieldName

      If 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 is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 a boolean. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 a byte. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 a char. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - if this field is not accessible
    • getDeclaringClass

      public Class<?> getDeclaringClass()
      Returns the class that declares this field.
      Specified by:
      getDeclaringClass in interface Member
      Returns:
      the declaring class
    • getDouble

      public double getDouble​(Object object) throws IllegalAccessException, IllegalArgumentException
      Returns the value of the field in the specified object as a double. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 a float. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 an int. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 a long. This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - if this field is not accessible
    • getModifiers

      public int getModifiers()
      Returns the modifiers for this field. The Modifier class should be used to decode the result.
      Specified by:
      getModifiers in interface Member
      Returns:
      the modifiers for this field
      See Also:
      Modifier
    • getName

      public String getName()
      Returns the name of this field.
      Specified by:
      getName in interface Member
      Returns:
      the name of this field
    • getShort

      public short getShort​(Object object) throws IllegalAccessException, IllegalArgumentException
      Returns the value of the field in the specified object as a short . This reproduces the effect of object.fieldName

      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
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - if this field is not accessible
    • getType

      public Class<?> getType()
      Return the Class associated with the type of this field.
      Returns:
      the type of this field
    • hashCode

      public int hashCode()
      Returns an integer hash code for this field. Objects which are equal return the same value for this method.

      The hash code for a Field is the exclusive-or combination of the hash code of the field's name and the hash code of the name of its declaring class.

      Overrides:
      hashCode in class Object
      Returns:
      the hash code for this field
      See Also:
      equals(java.lang.Object)
    • 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 of object.fieldName = value

      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.

      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 access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the boolean value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the byte value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the char value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the double value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the float value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the int value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the long value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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 the short value. This reproduces the effect of object.fieldName = value

      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.

      If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

      Parameters:
      object - the object to access
      value - the new value
      Throws:
      NullPointerException - if the object is null and the field is non-static
      IllegalArgumentException - if the object is not compatible with the declaring class
      IllegalAccessException - 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:

      1. modifiers (if any)
      2. type
      3. declaring class name
      4. '.'
      5. field name

      For example: public static java.io.InputStream java.lang.System.in

      Overrides:
      toString in class Object
      Returns:
      a printable representation for this field