View Javadoc
1   /***
2    * ASM: a very small and fast Java bytecode manipulation framework
3    * Copyright (c) 2000-2011 INRIA, France Telecom
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met:
9    * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   * 2. Redistributions in binary form must reproduce the above copyright
12   *    notice, this list of conditions and the following disclaimer in the
13   *    documentation and/or other materials provided with the distribution.
14   * 3. Neither the name of the copyright holders nor the names of its
15   *    contributors may be used to endorse or promote products derived from
16   *    this software without specific prior written permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28   * THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.springframework.asm;
31  
32  /**
33   * A constant pool item. Constant pool items can be created with the 'newXXX'
34   * methods in the {@link ClassWriter} class.
35   * 
36   * @author Eric Bruneton
37   */
38  final class Item {
39  
40      /**
41       * Index of this item in the constant pool.
42       */
43      int index;
44  
45      /**
46       * Type of this constant pool item. A single class is used to represent all
47       * constant pool item types, in order to minimize the bytecode size of this
48       * package. The value of this field is one of {@link ClassWriter#INT},
49       * {@link ClassWriter#LONG}, {@link ClassWriter#FLOAT},
50       * {@link ClassWriter#DOUBLE}, {@link ClassWriter#UTF8},
51       * {@link ClassWriter#STR}, {@link ClassWriter#CLASS},
52       * {@link ClassWriter#NAME_TYPE}, {@link ClassWriter#FIELD},
53       * {@link ClassWriter#METH}, {@link ClassWriter#IMETH},
54       * {@link ClassWriter#MTYPE}, {@link ClassWriter#INDY}.
55       * 
56       * MethodHandle constant 9 variations are stored using a range of 9 values
57       * from {@link ClassWriter#HANDLE_BASE} + 1 to
58       * {@link ClassWriter#HANDLE_BASE} + 9.
59       * 
60       * Special Item types are used for Items that are stored in the ClassWriter
61       * {@link ClassWriter#typeTable}, instead of the constant pool, in order to
62       * avoid clashes with normal constant pool items in the ClassWriter constant
63       * pool's hash table. These special item types are
64       * {@link ClassWriter#TYPE_NORMAL}, {@link ClassWriter#TYPE_UNINIT} and
65       * {@link ClassWriter#TYPE_MERGED}.
66       */
67      int type;
68  
69      /**
70       * Value of this item, for an integer item.
71       */
72      int intVal;
73  
74      /**
75       * Value of this item, for a long item.
76       */
77      long longVal;
78  
79      /**
80       * First part of the value of this item, for items that do not hold a
81       * primitive value.
82       */
83      String strVal1;
84  
85      /**
86       * Second part of the value of this item, for items that do not hold a
87       * primitive value.
88       */
89      String strVal2;
90  
91      /**
92       * Third part of the value of this item, for items that do not hold a
93       * primitive value.
94       */
95      String strVal3;
96  
97      /**
98       * The hash code value of this constant pool item.
99       */
100     int hashCode;
101 
102     /**
103      * Link to another constant pool item, used for collision lists in the
104      * constant pool's hash table.
105      */
106     Item next;
107 
108     /**
109      * Constructs an uninitialized {@link Item}.
110      */
111     Item() {
112     }
113 
114     /**
115      * Constructs an uninitialized {@link Item} for constant pool element at
116      * given position.
117      * 
118      * @param index
119      *            index of the item to be constructed.
120      */
121     Item(final int index) {
122         this.index = index;
123     }
124 
125     /**
126      * Constructs a copy of the given item.
127      * 
128      * @param index
129      *            index of the item to be constructed.
130      * @param i
131      *            the item that must be copied into the item to be constructed.
132      */
133     Item(final int index, final Item i) {
134         this.index = index;
135         type = i.type;
136         intVal = i.intVal;
137         longVal = i.longVal;
138         strVal1 = i.strVal1;
139         strVal2 = i.strVal2;
140         strVal3 = i.strVal3;
141         hashCode = i.hashCode;
142     }
143 
144     /**
145      * Sets this item to an integer item.
146      * 
147      * @param intVal
148      *            the value of this item.
149      */
150     void set(final int intVal) {
151         this.type = ClassWriter.INT;
152         this.intVal = intVal;
153         this.hashCode = 0x7FFFFFFF & (type + intVal);
154     }
155 
156     /**
157      * Sets this item to a long item.
158      * 
159      * @param longVal
160      *            the value of this item.
161      */
162     void set(final long longVal) {
163         this.type = ClassWriter.LONG;
164         this.longVal = longVal;
165         this.hashCode = 0x7FFFFFFF & (type + (int) longVal);
166     }
167 
168     /**
169      * Sets this item to a float item.
170      * 
171      * @param floatVal
172      *            the value of this item.
173      */
174     void set(final float floatVal) {
175         this.type = ClassWriter.FLOAT;
176         this.intVal = Float.floatToRawIntBits(floatVal);
177         this.hashCode = 0x7FFFFFFF & (type + (int) floatVal);
178     }
179 
180     /**
181      * Sets this item to a double item.
182      * 
183      * @param doubleVal
184      *            the value of this item.
185      */
186     void set(final double doubleVal) {
187         this.type = ClassWriter.DOUBLE;
188         this.longVal = Double.doubleToRawLongBits(doubleVal);
189         this.hashCode = 0x7FFFFFFF & (type + (int) doubleVal);
190     }
191 
192     /**
193      * Sets this item to an item that do not hold a primitive value.
194      * 
195      * @param type
196      *            the type of this item.
197      * @param strVal1
198      *            first part of the value of this item.
199      * @param strVal2
200      *            second part of the value of this item.
201      * @param strVal3
202      *            third part of the value of this item.
203      */
204     void set(final int type, final String strVal1, final String strVal2,
205             final String strVal3) {
206         this.type = type;
207         this.strVal1 = strVal1;
208         this.strVal2 = strVal2;
209         this.strVal3 = strVal3;
210         switch (type) {
211         case ClassWriter.CLASS:
212             this.intVal = 0;     // intVal of a class must be zero, see visitInnerClass
213 			hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
214 			return;
215         case ClassWriter.UTF8:
216         case ClassWriter.STR:
217         case ClassWriter.MTYPE:
218         case ClassWriter.TYPE_NORMAL:
219             hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
220             return;
221         case ClassWriter.NAME_TYPE: {
222             hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
223                     * strVal2.hashCode());
224             return;
225         }
226         // ClassWriter.FIELD:
227         // ClassWriter.METH:
228         // ClassWriter.IMETH:
229         // ClassWriter.HANDLE_BASE + 1..9
230         default:
231             hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
232                     * strVal2.hashCode() * strVal3.hashCode());
233         }
234     }
235 
236     /**
237      * Sets the item to an InvokeDynamic item.
238      * 
239      * @param name
240      *            invokedynamic's name.
241      * @param desc
242      *            invokedynamic's desc.
243      * @param bsmIndex
244      *            zero based index into the class attribute BootrapMethods.
245      */
246     void set(String name, String desc, int bsmIndex) {
247         this.type = ClassWriter.INDY;
248         this.longVal = bsmIndex;
249         this.strVal1 = name;
250         this.strVal2 = desc;
251         this.hashCode = 0x7FFFFFFF & (ClassWriter.INDY + bsmIndex
252                 * strVal1.hashCode() * strVal2.hashCode());
253     }
254 
255     /**
256      * Sets the item to a BootstrapMethod item.
257      * 
258      * @param position
259      *            position in byte in the class attribute BootrapMethods.
260      * @param hashCode
261      *            hashcode of the item. This hashcode is processed from the
262      *            hashcode of the bootstrap method and the hashcode of all
263      *            bootstrap arguments.
264      */
265     void set(int position, int hashCode) {
266         this.type = ClassWriter.BSM;
267         this.intVal = position;
268         this.hashCode = hashCode;
269     }
270 
271     /**
272      * Indicates if the given item is equal to this one. <i>This method assumes
273      * that the two items have the same {@link #type}</i>.
274      * 
275      * @param i
276      *            the item to be compared to this one. Both items must have the
277      *            same {@link #type}.
278      * @return <tt>true</tt> if the given item if equal to this one,
279      *         <tt>false</tt> otherwise.
280      */
281     boolean isEqualTo(final Item i) {
282         switch (type) {
283         case ClassWriter.UTF8:
284         case ClassWriter.STR:
285         case ClassWriter.CLASS:
286         case ClassWriter.MTYPE:
287         case ClassWriter.TYPE_NORMAL:
288             return i.strVal1.equals(strVal1);
289         case ClassWriter.TYPE_MERGED:
290         case ClassWriter.LONG:
291         case ClassWriter.DOUBLE:
292             return i.longVal == longVal;
293         case ClassWriter.INT:
294         case ClassWriter.FLOAT:
295             return i.intVal == intVal;
296         case ClassWriter.TYPE_UNINIT:
297             return i.intVal == intVal && i.strVal1.equals(strVal1);
298         case ClassWriter.NAME_TYPE:
299             return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2);
300         case ClassWriter.INDY: {
301             return i.longVal == longVal && i.strVal1.equals(strVal1)
302                     && i.strVal2.equals(strVal2);
303         }
304         // case ClassWriter.FIELD:
305         // case ClassWriter.METH:
306         // case ClassWriter.IMETH:
307         // case ClassWriter.HANDLE_BASE + 1..9
308         default:
309             return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2)
310                     && i.strVal3.equals(strVal3);
311         }
312     }
313 
314 }