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