Class ConstantPool.Reference

  • Enclosing class:
    ConstantPool

    public static class ConstantPool.Reference
    extends java.lang.Object
    A Data-Class which can represent:



    Example Java Constant-Pool Table:
    The following table was (partially) put together by none other than the illustrious Chat-GPT on December 17th, 2024. This table does not actually contain everything that can / does go into a typical Constant-Table, but rather only the constants which are pertinent to this example are included in this, example, table.

    Admittedly, I ended up adding all kinds of stuff to the table, and changing around some of the text, but, when your brain isn't working so well, A.I. will help you tremendously come up with stuff. "Writer's Block" (starting at a blank screen) is a real phenomenon when programming.

    Chat-GPT never ceases to amaze me.

    Index Tag-Kind Tag-Kind Name Value
    1 1 UTF8 `toString`
    2 7 Class Index-Pointer: 10 → Points to [`UTF8`, "MyClass"]
    3 12 NameAndType Index-Pointer: 1 → Points to [`UTF8`, "toString"]
    Index-Pointer: 9 → Points to [`UTF8`, "()Ljava/lang/String;"]
    4 1 UTF8 `equals`
    5 12 NameAndType Index-Pointer: 4 → Points to [`UTF8`, "equals"]
    Index-Pointer: 8 → Points to [`UTF8`, "(Ljava/lang/Object;)Z"]
    6 7 Class Index-Pointer: 11 → Points to [`UTF8`, "java/lang/Object"]
    7 10 MethodRef Index-Pointer: 2 → Points to a [`Class` Entry]
    Index-Pointer: 3 → Points to a [`NameAndType` Entry]
    8 1 UTF8 `(Ljava/lang/Object;)Z`
    9 1 UTF8 `()Ljava/lang/String;`
    10 1 UTF8 `MyClass`
    11 1 UTF8 `java/lang/Object`
    12 9 MethodRef Index-Pointer: 6 → Points to a [`Class` Entry]
    Index-Pointer: 5 → Points to a [`NameAndType` Entry]


    Method-Reference Entries / Records:

    Method-Reference for toString() is a Pointer-Pair located @ Table-Index 7:

    • Index 2 → Points to a [Class Entry], which is another Index-Pointer to →
      • Index 10: which contains a [UTF-8 Entry] "MyClass"
    • Index 3 → Points to a [Name-And-Type Entry], which is another Pointer-Pair to →
      • Index 1: (Name), which contains a [UTF-8 Entry] "toString"
      • Index 9: (Type), which contains a [UTF-8 Entry] "()Ljava/lang/String;"

    Method-Reference for equals() is a Pointer-Pair located @ Table-Index 12:

    • Index 6 → Points to a [Class Entry], which is another Index-Pointer to →
      • Index 11: which contains a [UTF-8 Entry] "java/lang/Object"
    • Index 5 → Points to a [Name-And-Type Entry], which is another Pointer-Pair to →
      • Index 4: (Name), which contains a [UTF-8 Entry] "equals"
      • Index 8: (Type), which contains a [UTF-8 Entry] "(Ljava/lang/Object;)Z"



    Instances of 'ConstantPool.Reference':
    ConstantPool.Reference FieldAssigned Value
    tag 10 (TAG_METHOD_REF)
    tableIndex 7
    name "toString"
    ownerClassName "MyClass"
    descriptor "()Ljava/lang/String;"
    descriptorUTF8Index 9
    nameAndTypeIndex 3
    nameUTF8Index 1
    ownerClassIndex 2
    ownerClassNameUTF8Index 10


    ConstantPool.Reference FieldAssigned Value
    tag 10 (TAG_METHOD_REF)
    tableIndex 12
    name "equals"
    ownerClassName "java/lang/Object"
    descriptor "(Ljava/lang/Object;)Z"
    descriptorUTF8Index 8
    nameAndTypeIndex 5
    nameUTF8Index 4
    ownerClassIndex 6
    ownerClassNameUTF8Index 11


    • Field Summary

       
      Either TAG_FIELD_REF (9) or TAG_METHOD_REF (10) or TAG_INTERFACE_METHOD_REF (11)
      Modifier and Type Field Description
      byte tag
      The Constant-Pool Tg-Kind associated with this instance.
       
      Reference's Actual Table-Index in the Constant-Pool
      Modifier and Type Field Description
      int tableIndex
      The Constant-Pool Table-Index where this Reference is located
       
      UTF-8 Name-String Reference Data
      Modifier and Type Field Description
      String name
      This is just the name of the Method or Field.
      String ownerClassName
      The 'className' field specifies in which class (where) the Method or Field has actuallly been defined.
       
      UTF-8 Method or Field Descriptor, as a String
      Modifier and Type Field Description
      String descriptor
      This is the descriptor of a method or field, which is Java single-line description which includes type-information stored as a simple String.
       
      Constant-Pool Table-Entry Indices
      Modifier and Type Field Description
      int descriptorUTF8Index
      Constant-Pool Table-Index identifying where the UTF-8 Descriptor's Description-As-A-String is located.
      int nameAndTypeIndex
      An intermediate Constant-Pool Index-Pair that points to a Name UTF-8 Constant, and a Type-Descriptor Constant.
      int nameUTF8Index
      Constant-Pool Table-Index identifying where the UTF-8 Method's or Field's Name-As-A-String is located.
      int ownerClassIndex
      Constant-Pool Table-Index where owner's className 'Class Constant' was stored
      int ownerClassNameUTF8Index
      Constant-Pool Table-Index identifying where the UTF-8 Class' Name-As-A-String is located.
    • Method Summary

       
      Methods: class java.lang.Object
      Modifier and Type Method Description
      boolean equals​(Object other)
      Checks for Object-equality between 'this' instance and 'other'.
      int hashCode()
      Generates a Hash-Code.
      String toString()
      Converts instance into a String.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • tableIndex

        🡇     🗕  🗗  🗖
        public final int tableIndex
        The Constant-Pool Table-Index where this Reference is located
        Code:
        Exact Field Declaration Expression:
         public final int tableIndex;
        
      • ownerClassName

        🡅  🡇     🗕  🗗  🗖
        public final java.lang.String ownerClassName
        The 'className' field specifies in which class (where) the Method or Field has actuallly been defined. For most methods and fields, the 'className' is the same as the actual class whose Constant-Pool was loaded into an instance of ConstantPool.

        If the field or method reference is defined in an ancestor or parent class, then this field would contain the name of that ancestor.
        Code:
        Exact Field Declaration Expression:
         public final String ownerClassName;
        
      • ownerClassIndex

        🡅  🡇     🗕  🗗  🗖
        public final int ownerClassIndex
        Constant-Pool Table-Index where owner's className 'Class Constant' was stored
        Code:
        Exact Field Declaration Expression:
         public final int ownerClassIndex;
        
      • ownerClassNameUTF8Index

        🡅  🡇     🗕  🗗  🗖
        public final int ownerClassNameUTF8Index
        Constant-Pool Table-Index identifying where the UTF-8 Class' Name-As-A-String is located.
        Code:
        Exact Field Declaration Expression:
         public final int ownerClassNameUTF8Index;
        
      • nameAndTypeIndex

        🡅  🡇     🗕  🗗  🗖
        public final int nameAndTypeIndex
        An intermediate Constant-Pool Index-Pair that points to a Name UTF-8 Constant, and a Type-Descriptor Constant.
        Code:
        Exact Field Declaration Expression:
         public final int nameAndTypeIndex;
        
      • name

        🡅  🡇     🗕  🗗  🗖
        public final java.lang.String name
        This is just the name of the Method or Field. Some common method names are (just for example) and 'equals'. This particular Nested-Type has three fields of it's own, named: 'className, 'name' and 'descriptor'.
        Code:
        Exact Field Declaration Expression:
         public final String name;
        
      • nameUTF8Index

        🡅  🡇     🗕  🗗  🗖
        public final int nameUTF8Index
        Constant-Pool Table-Index identifying where the UTF-8 Method's or Field's Name-As-A-String is located.
        Code:
        Exact Field Declaration Expression:
         public final int nameUTF8Index;
        
      • descriptor

        🡅  🡇     🗕  🗗  🗖
        public final java.lang.String descriptor
        This is the descriptor of a method or field, which is Java single-line description which includes type-information stored as a simple String. A descriptor is of the format:

        • For a method, the descriptor specifies the parameter types and return type.
        • For a field, the descriptor specifies the field type.
        Code:
        Exact Field Declaration Expression:
         public final String descriptor;
        
      • descriptorUTF8Index

        🡅  🡇     🗕  🗗  🗖
        public final int descriptorUTF8Index
        Constant-Pool Table-Index identifying where the UTF-8 Descriptor's Description-As-A-String is located.
        Code:
        Exact Field Declaration Expression:
         public final int descriptorUTF8Index;
        
    • Method Detail

      • toString

        🡅  🡇     🗕  🗗  🗖
        public final java.lang.String toString()
        Converts instance into a java.lang.String.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The data-content of this record, as a String, for printing.
        Code:
        Exact Method Body:
         return
             "Reference:\n" +
             "{\n" +
             "    tag:             " + tag + " - " + ConstantPool.tagNames.get(tag) + '\n' +
             "    Table-Index:     " + this.tableIndex + '\n' +                
             "    ownerClassName:  " + this.ownerClassName + '\n' +
             "    name:            " + this.name + '\n' +
             "    descriptor:      " + this.descriptor + '\n' +
             "}";
        
      • equals

        🡅  🡇     🗕  🗗  🗖
        public boolean equals​(java.lang.Object other)
        Checks for Object-equality between 'this' instance and 'other'.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        other - This may be any Java-Object, but only an instance of ConstantPool.Reference will permit this method to return TRUE.
        Returns:
        TRUE if-and-only-if parameter 'other' is an instance of Reference and has identical field values.
        Code:
        Exact Method Body:
         if (!(other instanceof Reference)) return false;
        
         Reference r = (Reference) other;
        
         return
                 (this.tag == r.tag)
        
             &&  Objects.equals(this.ownerClassName, r.ownerClassName)
             &&  (this.ownerClassIndex           == r.ownerClassIndex)
             &&  (this.ownerClassNameUTF8Index   == r.ownerClassNameUTF8Index)
        
             &&  Objects.equals(this.name, r.name)
             &&  (this.nameUTF8Index == r.nameUTF8Index)
        
             &&  Objects.equals(this.descriptor, r.descriptor)
             &&  (this.descriptorUTF8Index == r.descriptorUTF8Index);
        
      • hashCode

        🡅     🗕  🗗  🗖
        public int hashCode()
        Generates a Hash-Code.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        An integer Hash-Code that may be used by Java's Hashing Data-Structures.
        Code:
        Exact Method Body:
         return this.name.hashCode() +
             this.ownerClassNameUTF8Index +
             this.nameUTF8Index +
             this.descriptorUTF8Index;