KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > Item


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 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.objectweb.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     short 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 I, J, F, D, S, s, C, T, G, M, or N
49      * (for Constant Integer, Long, Float, Double, STR, UTF8, Class, NameType,
50      * Fieldref, Methodref, or InterfaceMethodref constant pool items
51      * respectively).
52      */

53     char type;
54
55     /**
56      * Value of this item, for an integer item.
57      */

58     int intVal;
59
60     /**
61      * Value of this item, for a long item.
62      */

63     long longVal;
64
65     /**
66      * Value of this item, for a float item.
67      */

68     float floatVal;
69
70     /**
71      * Value of this item, for a double item.
72      */

73     double doubleVal;
74
75     /**
76      * First part of the value of this item, for items that do not hold a
77      * primitive value.
78      */

79     String JavaDoc strVal1;
80
81     /**
82      * Second part of the value of this item, for items that do not hold a
83      * primitive value.
84      */

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

91     String JavaDoc strVal3;
92
93     /**
94      * The hash code value of this constant pool item.
95      */

96     int hashCode;
97
98     /**
99      * Link to another constant pool item, used for collision lists in the
100      * constant pool's hash table.
101      */

102     Item next;
103
104     /**
105      * Constructs an uninitialized {@link Item}.
106      */

107     Item() {
108     }
109
110     /**
111      * Constructs a copy of the given item.
112      *
113      * @param index index of the item to be constructed.
114      * @param i the item that must be copied into the item to be constructed.
115      */

116     Item(final short index, final Item i) {
117         this.index = index;
118         type = i.type;
119         intVal = i.intVal;
120         longVal = i.longVal;
121         floatVal = i.floatVal;
122         doubleVal = i.doubleVal;
123         strVal1 = i.strVal1;
124         strVal2 = i.strVal2;
125         strVal3 = i.strVal3;
126         hashCode = i.hashCode;
127     }
128
129     /**
130      * Sets this item to an integer item.
131      *
132      * @param intVal the value of this item.
133      */

134     void set(final int intVal) {
135         this.type = 'I';
136         this.intVal = intVal;
137         this.hashCode = 0x7FFFFFFF & (type + intVal);
138     }
139
140     /**
141      * Sets this item to a long item.
142      *
143      * @param longVal the value of this item.
144      */

145     void set(final long longVal) {
146         this.type = 'J';
147         this.longVal = longVal;
148         this.hashCode = 0x7FFFFFFF & (type + (int) longVal);
149     }
150
151     /**
152      * Sets this item to a float item.
153      *
154      * @param floatVal the value of this item.
155      */

156     void set(final float floatVal) {
157         this.type = 'F';
158         this.floatVal = floatVal;
159         this.hashCode = 0x7FFFFFFF & (type + (int) floatVal);
160     }
161
162     /**
163      * Sets this item to a double item.
164      *
165      * @param doubleVal the value of this item.
166      */

167     void set(final double doubleVal) {
168         this.type = 'D';
169         this.doubleVal = doubleVal;
170         this.hashCode = 0x7FFFFFFF & (type + (int) doubleVal);
171     }
172
173     /**
174      * Sets this item to an item that do not hold a primitive value.
175      *
176      * @param type the type of this item.
177      * @param strVal1 first part of the value of this item.
178      * @param strVal2 second part of the value of this item.
179      * @param strVal3 third part of the value of this item.
180      */

181     void set(
182         final char type,
183         final String JavaDoc strVal1,
184         final String JavaDoc strVal2,
185         final String JavaDoc strVal3)
186     {
187         this.type = type;
188         this.strVal1 = strVal1;
189         this.strVal2 = strVal2;
190         this.strVal3 = strVal3;
191         switch (type) {
192             case 's':
193             case 'S':
194             case 'C':
195                 hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
196                 return;
197             case 'T':
198                 hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
199                         * strVal2.hashCode());
200                 return;
201             // case 'G':
202
// case 'M':
203
// case 'N':
204
default:
205                 hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
206                         * strVal2.hashCode() * strVal3.hashCode());
207         }
208     }
209
210     /**
211      * Indicates if the given item is equal to this one.
212      *
213      * @param i the item to be compared to this one.
214      * @return <tt>true</tt> if the given item if equal to this one,
215      * <tt>false</tt> otherwise.
216      */

217     boolean isEqualTo(final Item i) {
218         if (i.type == type) {
219             switch (type) {
220                 case 'I':
221                     return i.intVal == intVal;
222                 case 'J':
223                     return i.longVal == longVal;
224                 case 'F':
225                     return i.floatVal == floatVal;
226                 case 'D':
227                     return i.doubleVal == doubleVal;
228                 case 's':
229                 case 'S':
230                 case 'C':
231                     return i.strVal1.equals(strVal1);
232                 case 'T':
233                     return i.strVal1.equals(strVal1)
234                             && i.strVal2.equals(strVal2);
235                 // case 'G':
236
// case 'M':
237
// case 'N':
238
default:
239                     return i.strVal1.equals(strVal1)
240                             && i.strVal2.equals(strVal2)
241                             && i.strVal3.equals(strVal3);
242             }
243         }
244         return false;
245     }
246 }
247
Popular Tags