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