View Javadoc
1   /*
2    * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  /*
27   * This file is available under and governed by the GNU General Public
28   * License version 2 only, as published by the Free Software Foundation.
29   * However, the following notice accompanied the original version of this
30   * file:
31   *
32   * ASM: a very small and fast Java bytecode manipulation framework
33   * Copyright (c) 2000-2007 INRIA, France Telecom
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions
38   * are met:
39   * 1. Redistributions of source code must retain the above copyright
40   *    notice, this list of conditions and the following disclaimer.
41   * 2. Redistributions in binary form must reproduce the above copyright
42   *    notice, this list of conditions and the following disclaimer in the
43   *    documentation and/or other materials provided with the distribution.
44   * 3. Neither the name of the copyright holders nor the names of its
45   *    contributors may be used to endorse or promote products derived from
46   *    this software without specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
59   */
60  package com.sun.xml.internal.ws.org.objectweb.asm;
61  
62  /**
63   * A constant pool item. Constant pool items can be created with the 'newXXX'
64   * methods in the {@link ClassWriter} class.
65   *
66   * @author Eric Bruneton
67   */
68  final class Item {
69  
70      /**
71       * Index of this item in the constant pool.
72       */
73      int index;
74  
75      /**
76       * Type of this constant pool item. A single class is used to represent all
77       * constant pool item types, in order to minimize the bytecode size of this
78       * package. The value of this field is one of {@link ClassWriter#INT},
79       * {@link ClassWriter#LONG}, {@link ClassWriter#FLOAT},
80       * {@link ClassWriter#DOUBLE}, {@link ClassWriter#UTF8},
81       * {@link ClassWriter#STR}, {@link ClassWriter#CLASS},
82       * {@link ClassWriter#NAME_TYPE}, {@link ClassWriter#FIELD},
83       * {@link ClassWriter#METH}, {@link ClassWriter#IMETH}.
84       *
85       * Special Item types are used for Items that are stored in the ClassWriter
86       * {@link ClassWriter#typeTable}, instead of the constant pool, in order to
87       * avoid clashes with normal constant pool items in the ClassWriter constant
88       * pool's hash table. These special item types are
89       * {@link ClassWriter#TYPE_NORMAL}, {@link ClassWriter#TYPE_UNINIT} and
90       * {@link ClassWriter#TYPE_MERGED}.
91       */
92      int type;
93  
94      /**
95       * Value of this item, for an integer item.
96       */
97      int intVal;
98  
99      /**
100      * Value of this item, for a long item.
101      */
102     long longVal;
103 
104     /**
105      * First part of the value of this item, for items that do not hold a
106      * primitive value.
107      */
108     String strVal1;
109 
110     /**
111      * Second part of the value of this item, for items that do not hold a
112      * primitive value.
113      */
114     String strVal2;
115 
116     /**
117      * Third part of the value of this item, for items that do not hold a
118      * primitive value.
119      */
120     String strVal3;
121 
122     /**
123      * The hash code value of this constant pool item.
124      */
125     int hashCode;
126 
127     /**
128      * Link to another constant pool item, used for collision lists in the
129      * constant pool's hash table.
130      */
131     Item next;
132 
133     /**
134      * Constructs an uninitialized {@link Item}.
135      */
136     Item() {
137     }
138 
139     /**
140      * Constructs an uninitialized {@link Item} for constant pool element at
141      * given position.
142      *
143      * @param index index of the item to be constructed.
144      */
145     Item(final int index) {
146         this.index = index;
147     }
148 
149     /**
150      * Constructs a copy of the given item.
151      *
152      * @param index index of the item to be constructed.
153      * @param i the item that must be copied into the item to be constructed.
154      */
155     Item(final int index, final Item i) {
156         this.index = index;
157         type = i.type;
158         intVal = i.intVal;
159         longVal = i.longVal;
160         strVal1 = i.strVal1;
161         strVal2 = i.strVal2;
162         strVal3 = i.strVal3;
163         hashCode = i.hashCode;
164     }
165 
166     /**
167      * Sets this item to an integer item.
168      *
169      * @param intVal the value of this item.
170      */
171     void set(final int intVal) {
172         this.type = ClassWriter.INT;
173         this.intVal = intVal;
174         this.hashCode = 0x7FFFFFFF & (type + intVal);
175     }
176 
177     /**
178      * Sets this item to a long item.
179      *
180      * @param longVal the value of this item.
181      */
182     void set(final long longVal) {
183         this.type = ClassWriter.LONG;
184         this.longVal = longVal;
185         this.hashCode = 0x7FFFFFFF & (type + (int) longVal);
186     }
187 
188     /**
189      * Sets this item to a float item.
190      *
191      * @param floatVal the value of this item.
192      */
193     void set(final float floatVal) {
194         this.type = ClassWriter.FLOAT;
195         this.intVal = Float.floatToRawIntBits(floatVal);
196         this.hashCode = 0x7FFFFFFF & (type + (int) floatVal);
197     }
198 
199     /**
200      * Sets this item to a double item.
201      *
202      * @param doubleVal the value of this item.
203      */
204     void set(final double doubleVal) {
205         this.type = ClassWriter.DOUBLE;
206         this.longVal = Double.doubleToRawLongBits(doubleVal);
207         this.hashCode = 0x7FFFFFFF & (type + (int) doubleVal);
208     }
209 
210     /**
211      * Sets this item to an item that do not hold a primitive value.
212      *
213      * @param type the type of this item.
214      * @param strVal1 first part of the value of this item.
215      * @param strVal2 second part of the value of this item.
216      * @param strVal3 third part of the value of this item.
217      */
218     void set(
219         final int type,
220         final String strVal1,
221         final String strVal2,
222         final String strVal3)
223     {
224         this.type = type;
225         this.strVal1 = strVal1;
226         this.strVal2 = strVal2;
227         this.strVal3 = strVal3;
228         switch (type) {
229             case ClassWriter.UTF8:
230             case ClassWriter.STR:
231             case ClassWriter.CLASS:
232             case ClassWriter.TYPE_NORMAL:
233                 hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
234                 return;
235             case ClassWriter.NAME_TYPE:
236                 hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
237                         * strVal2.hashCode());
238                 return;
239                 // ClassWriter.FIELD:
240                 // ClassWriter.METH:
241                 // ClassWriter.IMETH:
242             default:
243                 hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
244                         * strVal2.hashCode() * strVal3.hashCode());
245         }
246     }
247 
248     /**
249      * Indicates if the given item is equal to this one.
250      *
251      * @param i the item to be compared to this one.
252      * @return <tt>true</tt> if the given item if equal to this one,
253      *         <tt>false</tt> otherwise.
254      */
255     boolean isEqualTo(final Item i) {
256         if (i.type == type) {
257             switch (type) {
258                 case ClassWriter.INT:
259                 case ClassWriter.FLOAT:
260                     return i.intVal == intVal;
261                 case ClassWriter.TYPE_MERGED:
262                 case ClassWriter.LONG:
263                 case ClassWriter.DOUBLE:
264                     return i.longVal == longVal;
265                 case ClassWriter.UTF8:
266                 case ClassWriter.STR:
267                 case ClassWriter.CLASS:
268                 case ClassWriter.TYPE_NORMAL:
269                     return i.strVal1.equals(strVal1);
270                 case ClassWriter.TYPE_UNINIT:
271                     return i.intVal == intVal && i.strVal1.equals(strVal1);
272                 case ClassWriter.NAME_TYPE:
273                     return i.strVal1.equals(strVal1)
274                             && i.strVal2.equals(strVal2);
275                     // ClassWriter.FIELD:
276                     // ClassWriter.METH:
277                     // ClassWriter.IMETH:
278                 default:
279                     return i.strVal1.equals(strVal1)
280                             && i.strVal2.equals(strVal2)
281                             && i.strVal3.equals(strVal3);
282             }
283         }
284         return false;
285     }
286 }