Class Field

  • All Implemented Interfaces:
    FieldInfo

    public class Field
    extends java.lang.Object
    implements FieldInfo
    Field models a field (member variable) in a class. The Field class grants access to information such as the field's modifiers, its name and type descriptor (represented as indices into the constant pool), and any attributes of the field. Static fields have a ConstantValue attribute.
    See Also:
    ConstantValue
    • Constructor Summary

      Constructors 
      Constructor Description
      Field​(java.io.DataInputStream in, ClassInfo classInfo)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int constantValue()
      Get the index into the constant pool of the field's constant value, if any.
      ClassInfo declaringClass()
      Get the class which declared the field.
      int modifiers()
      Get the modifiers of the field.
      int nameIndex()
      Get the index into the constant pool of the name of the field.
      void setConstantValue​(int index)
      Set the index into the constant pool of the field's constant value.
      void setModifiers​(int modifiers)
      Set the modifiers of the field.
      void setNameIndex​(int name)
      Set the index into the constant pool of the name of the field.
      void setTypeIndex​(int type)
      Set the index into the constant pool of the type of the field.
      java.lang.String toString()
      Convert the field to a string.
      int typeIndex()
      Get the index into the constant pool of the type of the field.
      void write​(java.io.DataOutputStream out)
      Write the field to a class file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Field

        public Field​(java.io.DataInputStream in,
                     ClassInfo classInfo)
              throws java.io.IOException
        Constructor. Read a field from a class file.
        Parameters:
        in - The data stream of the class file.
        classInfo - The class file containing the field.
        Throws:
        java.io.IOException - If an error occurs while reading.
    • Method Detail

      • declaringClass

        public ClassInfo declaringClass()
        Get the class which declared the field.
        Specified by:
        declaringClass in interface FieldInfo
        Returns:
        The ClassInfo of the class which declared the field.
      • setNameIndex

        public void setNameIndex​(int name)
        Set the index into the constant pool of the name of the field.
        Specified by:
        setNameIndex in interface FieldInfo
        Parameters:
        name - The name of the field.
      • setTypeIndex

        public void setTypeIndex​(int type)
        Set the index into the constant pool of the type of the field.
        Specified by:
        setTypeIndex in interface FieldInfo
        Parameters:
        type - The type of the field.
      • nameIndex

        public int nameIndex()
        Get the index into the constant pool of the name of the field.
        Specified by:
        nameIndex in interface FieldInfo
        Returns:
        The index into the constant pool of the name of the field.
      • typeIndex

        public int typeIndex()
        Get the index into the constant pool of the type of the field.
        Specified by:
        typeIndex in interface FieldInfo
        Returns:
        The index into the constant pool of the type of the field.
      • setModifiers

        public void setModifiers​(int modifiers)
        Set the modifiers of the field. The values correspond to the constants in the Modifiers class.
        Specified by:
        setModifiers in interface FieldInfo
        Parameters:
        modifiers - A bit vector of modifier flags for the field.
        See Also:
        Modifiers
      • modifiers

        public int modifiers()
        Get the modifiers of the field. The values correspond to the constants in the Modifiers class.
        Specified by:
        modifiers in interface FieldInfo
        Returns:
        A bit vector of modifier flags for the field.
        See Also:
        Modifiers
      • constantValue

        public int constantValue()
        Get the index into the constant pool of the field's constant value, if any. Returns 0 if the field does not have a constant value.
        Specified by:
        constantValue in interface FieldInfo
        See Also:
        ClassInfo.constants()
      • write

        public void write​(java.io.DataOutputStream out)
                   throws java.io.IOException
        Write the field to a class file.
        Parameters:
        out - The data stream of the class file.
        Throws:
        java.io.IOException - If an error occurs while writing.
      • toString

        public java.lang.String toString()
        Convert the field to a string.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of the field.