View Javadoc
1   package mockit.asm.constantPool;
2   
3   import edu.umd.cs.findbugs.annotations.NonNull;
4   import edu.umd.cs.findbugs.annotations.Nullable;
5   
6   import mockit.asm.jvmConstants.ConstantPoolTypes;
7   
8   import org.checkerframework.checker.index.qual.NonNegative;
9   
10  /**
11   * A constant pool item of a given {@linkplain ConstantPoolTypes type}.
12   */
13  public abstract class Item {
14      /**
15       * Index of this item in the constant pool.
16       */
17      @NonNegative
18      public final int index;
19  
20      /**
21       * {@link ConstantPoolTypes Type} of this constant pool item.
22       * <p>
23       * MethodHandle variations are stored using a range of 9 values from {@link ConstantPoolTypes#HANDLE_BASE
24       * HANDLE_BASE} + 1 to {@link ConstantPoolTypes#HANDLE_BASE HANDLE_BASE} + 9.
25       * <p>
26       * Special Item types are used for items that are stored in the {@link ConstantPoolGeneration#typeTable}, instead of
27       * the constant pool, in order to avoid clashes with normal constant pool items in the constant pool's hash table.
28       * These special item types are defined in {@link TypeTableItem.SpecialType}.
29       */
30      @NonNegative
31      int type;
32  
33      /**
34       * The hash code value of this constant pool item.
35       */
36      @NonNegative
37      private int hashCode;
38  
39      /**
40       * Link to another constant pool item, used for collision lists in the constant pool's hash table.
41       */
42      @Nullable
43      private Item next;
44  
45      /**
46       * Initializes an Item for a constant pool element at the given position.
47       *
48       * @param index
49       *            index of the item
50       */
51      Item(@NonNegative int index) {
52          this.index = index;
53      }
54  
55      /**
56       * Initializes this item as a copy of a given item.
57       *
58       * @param index
59       *            index of the new item
60       * @param item
61       *            the item to be copied into this item
62       */
63      Item(@NonNegative int index, @NonNull Item item) {
64          this.index = index;
65          type = item.type;
66          hashCode = item.hashCode;
67      }
68  
69      /**
70       * Returns the {@link #type} of this item.
71       */
72      @NonNegative
73      public int getType() {
74          return type;
75      }
76  
77      /**
78       * Returns the {@link #hashCode} value.
79       */
80      @NonNegative
81      public final int getHashCode() {
82          return hashCode;
83      }
84  
85      final void setHashCode(int valuesHashCode) {
86          hashCode = 0x7FFFFFFF & type + valuesHashCode;
87      }
88  
89      /**
90       * Returns the {@link #next} item, if any.
91       */
92      @Nullable
93      public Item getNext() {
94          return next;
95      }
96  
97      public final void setNext(@NonNull Item[] items) {
98          int indexOfNextItem = hashCode % items.length;
99          next = items[indexOfNextItem];
100         items[indexOfNextItem] = this;
101     }
102 
103     /**
104      * Indicates if the given item is equal to this one, <em>assuming that the two items have the same
105      * {@link #type}</em>.
106      *
107      * @param item
108      *            the item to be compared to this one (both items must have the same {@link #type})
109      *
110      * @return <code>true</code> if the given item is equal to this one, <code>false</code> otherwise
111      */
112     abstract boolean isEqualTo(@NonNull Item item);
113 }