KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > Item


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 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
31 package oracle.toplink.libraries.asm;
32
33 /**
34  * A constant pool item. Constant pool items can be created with the 'newXXX'
35  * methods in the {@link ClassWriter} class.
36  *
37  * @author Eric Bruneton
38  */

39
40 final class Item {
41
42   /**
43    * Index of this item in the constant pool.
44    */

45
46   short index;
47
48   /**
49    * Type of this constant pool item. A single class is used to represent all
50    * constant pool item types, in order to minimize the bytecode size of this
51    * package. The value of this field is one of the constants defined in the
52    * {@link ClassWriter ClassWriter} class.
53    */

54
55   int type;
56
57   /**
58    * Value of this item, for a {@link ClassWriter#INT INT} item.
59    */

60
61   int intVal;
62
63   /**
64    * Value of this item, for a {@link ClassWriter#LONG LONG} item.
65    */

66
67   long longVal;
68
69   /**
70    * Value of this item, for a {@link ClassWriter#FLOAT FLOAT} item.
71    */

72
73   float floatVal;
74
75   /**
76    * Value of this item, for a {@link ClassWriter#DOUBLE DOUBLE} item.
77    */

78
79   double doubleVal;
80
81   /**
82    * First part of the value of this item, for items that do not hold a
83    * primitive value.
84    */

85
86   String JavaDoc strVal1;
87
88   /**
89    * Second part of the value of this item, for items that do not hold a
90    * primitive value.
91    */

92
93   String JavaDoc strVal2;
94
95   /**
96    * Third part of the value of this item, for items that do not hold a
97    * primitive value.
98    */

99
100   String JavaDoc strVal3;
101
102   /**
103    * The hash code value of this constant pool item.
104    */

105
106   int hashCode;
107
108   /**
109    * Link to another constant pool item, used for collision lists in the
110    * constant pool's hash table.
111    */

112
113   Item next;
114
115   /**
116    * Constructs an uninitialized {@link Item Item} object.
117    */

118
119   Item () {
120   }
121
122   /**
123    * Constructs a copy of the given item.
124    *
125    * @param index index of the item to be constructed.
126    * @param i the item that must be copied into the item to be constructed.
127    */

128
129   Item (final short index, final Item i) {
130     this.index = index;
131     type = i.type;
132     intVal = i.intVal;
133     longVal = i.longVal;
134     floatVal = i.floatVal;
135     doubleVal = i.doubleVal;
136     strVal1 = i.strVal1;
137     strVal2 = i.strVal2;
138     strVal3 = i.strVal3;
139     hashCode = i.hashCode;
140   }
141
142   /**
143    * Sets this item to an {@link ClassWriter#INT INT} item.
144    *
145    * @param intVal the value of this item.
146    */

147
148   void set (final int intVal) {
149     this.type = ClassWriter.INT;
150     this.intVal = intVal;
151     this.hashCode = 0x7FFFFFFF & (type + intVal);
152   }
153
154   /**
155    * Sets this item to a {@link ClassWriter#LONG LONG} item.
156    *
157    * @param longVal the value of this item.
158    */

159
160   void set (final long longVal) {
161     this.type = ClassWriter.LONG;
162     this.longVal = longVal;
163     this.hashCode = 0x7FFFFFFF & (type + (int)longVal);
164   }
165
166   /**
167    * Sets this item to a {@link ClassWriter#FLOAT FLOAT} item.
168    *
169    * @param floatVal the value of this item.
170    */

171
172   void set (final float floatVal) {
173     this.type = ClassWriter.FLOAT;
174     this.floatVal = floatVal;
175     this.hashCode = 0x7FFFFFFF & (type + (int)floatVal);
176   }
177
178   /**
179    * Sets this item to a {@link ClassWriter#DOUBLE DOUBLE} item.
180    *
181    * @param doubleVal the value of this item.
182    */

183
184   void set (final double doubleVal) {
185     this.type = ClassWriter.DOUBLE;
186     this.doubleVal = doubleVal;
187     this.hashCode = 0x7FFFFFFF & (type + (int)doubleVal);
188   }
189
190   /**
191    * Sets this item to an item that do not hold a primitive value.
192    *
193    * @param type the type of this item.
194    * @param strVal1 first part of the value of this item.
195    * @param strVal2 second part of the value of this item.
196    * @param strVal3 third part of the value of this item.
197    */

198
199   void set (
200     final int type,
201     final String JavaDoc strVal1,
202     final String JavaDoc strVal2,
203     final String JavaDoc strVal3)
204   {
205     this.type = type;
206     this.strVal1 = strVal1;
207     this.strVal2 = strVal2;
208     this.strVal3 = strVal3;
209     switch (type) {
210       case ClassWriter.UTF8:
211       case ClassWriter.STR:
212       case ClassWriter.CLASS:
213         hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
214         return;
215       case ClassWriter.NAME_TYPE:
216         hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()*strVal2.hashCode());
217         return;
218       //case ClassWriter.FIELD:
219
//case ClassWriter.METH:
220
//case ClassWriter.IMETH:
221
default:
222         hashCode = 0x7FFFFFFF & (type +
223           strVal1.hashCode()*strVal2.hashCode()*strVal3.hashCode());
224     }
225   }
226
227   /**
228    * Indicates if the given item is equal to this one.
229    *
230    * @param i the item to be compared to this one.
231    * @return <tt>true</tt> if the given item if equal to this one,
232    * <tt>false</tt> otherwise.
233    */

234
235   boolean isEqualTo (final Item i) {
236     if (i.type == type) {
237       switch (type) {
238         case ClassWriter.INT:
239           return i.intVal == intVal;
240         case ClassWriter.LONG:
241           return i.longVal == longVal;
242         case ClassWriter.FLOAT:
243           return i.floatVal == floatVal;
244         case ClassWriter.DOUBLE:
245           return i.doubleVal == doubleVal;
246         case ClassWriter.UTF8:
247         case ClassWriter.STR:
248         case ClassWriter.CLASS:
249           return i.strVal1.equals(strVal1);
250         case ClassWriter.NAME_TYPE:
251           return i.strVal1.equals(strVal1) &&
252                  i.strVal2.equals(strVal2);
253         //case ClassWriter.FIELD:
254
//case ClassWriter.METH:
255
//case ClassWriter.IMETH:
256
default:
257           return i.strVal1.equals(strVal1) &&
258                  i.strVal2.equals(strVal2) &&
259                  i.strVal3.equals(strVal3);
260       }
261     }
262     return false;
263   }
264 }
265
Popular Tags