001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (C) 2000 INRIA, France Telecom
004: * Copyright (C) 2002 France Telecom
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Contact: Eric.Bruneton@rd.francetelecom.com
021: *
022: * Author: Eric Bruneton
023: */package bsh.org.objectweb.asm;
024:
025: /**
026: * A constant pool item. Constant pool items can be created with the 'newXXX'
027: * methods in the {@link ClassWriter} class.
028: */
029:
030: final class Item {
031:
032: /**
033: * Index of this item in the constant pool.
034: */
035:
036: short index;
037:
038: /**
039: * Type of this constant pool item. A single class is used to represent all
040: * constant pool item types, in order to minimize the bytecode size of this
041: * package. The value of this field is one of the constants defined in the
042: * {@link ClassWriter ClassWriter} class.
043: */
044:
045: int type;
046:
047: /**
048: * Value of this item, for a {@link ClassWriter#INT INT} item.
049: */
050:
051: int intVal;
052:
053: /**
054: * Value of this item, for a {@link ClassWriter#LONG LONG} item.
055: */
056:
057: long longVal;
058:
059: /**
060: * Value of this item, for a {@link ClassWriter#FLOAT FLOAT} item.
061: */
062:
063: float floatVal;
064:
065: /**
066: * Value of this item, for a {@link ClassWriter#DOUBLE DOUBLE} item.
067: */
068:
069: double doubleVal;
070:
071: /**
072: * First part of the value of this item, for items that do not hold a
073: * primitive value.
074: */
075:
076: String strVal1;
077:
078: /**
079: * Second part of the value of this item, for items that do not hold a
080: * primitive value.
081: */
082:
083: String strVal2;
084:
085: /**
086: * Third part of the value of this item, for items that do not hold a
087: * primitive value.
088: */
089:
090: String strVal3;
091:
092: /**
093: * The hash code value of this constant pool item.
094: */
095:
096: int hashCode;
097:
098: /**
099: * Link to another constant pool item, used for collision lists in the
100: * constant pool's hash table.
101: */
102:
103: Item next;
104:
105: /**
106: * Constructs an uninitialized {@link Item Item} object.
107: */
108:
109: Item() {
110: }
111:
112: /**
113: * Constructs a copy of the given item.
114: *
115: * @param index index of the item to be constructed.
116: * @param i the item that must be copied into the item to be constructed.
117: */
118:
119: Item(final short index, final Item i) {
120: this .index = index;
121: type = i.type;
122: intVal = i.intVal;
123: longVal = i.longVal;
124: floatVal = i.floatVal;
125: doubleVal = i.doubleVal;
126: strVal1 = i.strVal1;
127: strVal2 = i.strVal2;
128: strVal3 = i.strVal3;
129: hashCode = i.hashCode;
130: }
131:
132: /**
133: * Sets this item to an {@link ClassWriter#INT INT} item.
134: *
135: * @param intVal the value of this item.
136: */
137:
138: void set(final int intVal) {
139: this .type = ClassWriter.INT;
140: this .intVal = intVal;
141: this .hashCode = type + intVal;
142: }
143:
144: /**
145: * Sets this item to a {@link ClassWriter#LONG LONG} item.
146: *
147: * @param longVal the value of this item.
148: */
149:
150: void set(final long longVal) {
151: this .type = ClassWriter.LONG;
152: this .longVal = longVal;
153: this .hashCode = type + (int) longVal;
154: }
155:
156: /**
157: * Sets this item to a {@link ClassWriter#FLOAT FLOAT} item.
158: *
159: * @param floatVal the value of this item.
160: */
161:
162: void set(final float floatVal) {
163: this .type = ClassWriter.FLOAT;
164: this .floatVal = floatVal;
165: this .hashCode = type + (int) floatVal;
166: }
167:
168: /**
169: * Sets this item to a {@link ClassWriter#DOUBLE DOUBLE} item.
170: *
171: * @param doubleVal the value of this item.
172: */
173:
174: void set(final double doubleVal) {
175: this .type = ClassWriter.DOUBLE;
176: this .doubleVal = doubleVal;
177: this .hashCode = type + (int) doubleVal;
178: }
179:
180: /**
181: * Sets this item to an item that do not hold a primitive value.
182: *
183: * @param type the type of this item.
184: * @param strVal1 first part of the value of this item.
185: * @param strVal2 second part of the value of this item.
186: * @param strVal3 third part of the value of this item.
187: */
188:
189: void set(final int type, final String strVal1,
190: final String strVal2, final String strVal3) {
191: this .type = type;
192: this .strVal1 = strVal1;
193: this .strVal2 = strVal2;
194: this .strVal3 = strVal3;
195: switch (type) {
196: case ClassWriter.UTF8:
197: case ClassWriter.STR:
198: case ClassWriter.CLASS:
199: hashCode = type + strVal1.hashCode();
200: return;
201: case ClassWriter.NAME_TYPE:
202: hashCode = type + strVal1.hashCode() * strVal2.hashCode();
203: return;
204: //case ClassWriter.FIELD:
205: //case ClassWriter.METH:
206: //case ClassWriter.IMETH:
207: default:
208: hashCode = type + strVal1.hashCode() * strVal2.hashCode()
209: * strVal3.hashCode();
210: return;
211: }
212: }
213:
214: /**
215: * Indicates if the given item is equal to this one.
216: *
217: * @param i the item to be compared to this one.
218: * @return <tt>true</tt> if the given item if equal to this one,
219: * <tt>false</tt> otherwise.
220: */
221:
222: boolean isEqualTo(final Item i) {
223: if (i.type == type) {
224: switch (type) {
225: case ClassWriter.INT:
226: return i.intVal == intVal;
227: case ClassWriter.LONG:
228: return i.longVal == longVal;
229: case ClassWriter.FLOAT:
230: return i.floatVal == floatVal;
231: case ClassWriter.DOUBLE:
232: return i.doubleVal == doubleVal;
233: case ClassWriter.UTF8:
234: case ClassWriter.STR:
235: case ClassWriter.CLASS:
236: return i.strVal1.equals(strVal1);
237: case ClassWriter.NAME_TYPE:
238: return i.strVal1.equals(strVal1)
239: && i.strVal2.equals(strVal2);
240: //case ClassWriter.FIELD:
241: //case ClassWriter.METH:
242: //case ClassWriter.IMETH:
243: default:
244: return i.strVal1.equals(strVal1)
245: && i.strVal2.equals(strVal2)
246: && i.strVal3.equals(strVal3);
247: }
248: }
249: return false;
250: }
251: }
|