KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > 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  * Contact: Eric.Bruneton@rd.francetelecom.com
31  *
32  * Author: Eric Bruneton
33  */

34
35 package org.logicalcobwebs.asm;
36
37 /**
38  * A constant pool item. Constant pool items can be created with the 'newXXX'
39  * methods in the {@link ClassWriter} class.
40  */

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

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

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

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

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

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

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

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

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

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

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

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

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

130
131   Item (final short index, final Item i) {
132     this.index = index;
133     type = i.type;
134     intVal = i.intVal;
135     longVal = i.longVal;
136     floatVal = i.floatVal;
137     doubleVal = i.doubleVal;
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 {@link ClassWriter#INT INT} item.
146    *
147    * @param intVal the value of this item.
148    */

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

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

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

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

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

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