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#MODULE}, {@link ClassWriter#PACKAGE},
  84      * {@link ClassWriter#MTYPE}, {@link ClassWriter#INDY}.
  85      *
  86      * MethodHandle constant 9 variations are stored using a range of 9 values
  87      * from {@link ClassWriter#HANDLE_BASE} + 1 to
  88      * {@link ClassWriter#HANDLE_BASE} + 9.
  89      *
  90      * Special Item types are used for Items that are stored in the ClassWriter
  91      * {@link ClassWriter#typeTable}, instead of the constant pool, in order to
  92      * avoid clashes with normal constant pool items in the ClassWriter constant
  93      * pool's hash table. These special item types are
  94      * {@link ClassWriter#TYPE_NORMAL}, {@link ClassWriter#TYPE_UNINIT} and
  95      * {@link ClassWriter#TYPE_MERGED}.
  96      */
  97     int type;
  98 
  99     /**
 100      * Value of this item, for an integer item.
 101      */
 102     int intVal;
 103 
 104     /**
 105      * Value of this item, for a long item.
 106      */
 107     long longVal;
 108 
 109     /**
 110      * First part of the value of this item, for items that do not hold a
 111      * primitive value.
 112      */
 113     String strVal1;
 114 
 115     /**
 116      * Second part of the value of this item, for items that do not hold a
 117      * primitive value.
 118      */
 119     String strVal2;
 120 
 121     /**
 122      * Third part of the value of this item, for items that do not hold a
 123      * primitive value.
 124      */
 125     String strVal3;
 126 
 127     /**
 128      * The hash code value of this constant pool item.
 129      */
 130     int hashCode;
 131 
 132     /**
 133      * Link to another constant pool item, used for collision lists in the
 134      * constant pool's hash table.
 135      */
 136     Item next;
 137 
 138     /**
 139      * Constructs an uninitialized {@link Item}.
 140      */
 141     Item() {
 142     }
 143 
 144     /**
 145      * Constructs an uninitialized {@link Item} for constant pool element at
 146      * given position.
 147      *
 148      * @param index
 149      *            index of the item to be constructed.
 150      */
 151     Item(final int index) {
 152         this.index = index;
 153     }
 154 
 155     /**
 156      * Constructs a copy of the given item.
 157      *
 158      * @param index
 159      *            index of the item to be constructed.
 160      * @param i
 161      *            the item that must be copied into the item to be constructed.
 162      */
 163     Item(final int index, final Item i) {
 164         this.index = index;
 165         type = i.type;
 166         intVal = i.intVal;
 167         longVal = i.longVal;
 168         strVal1 = i.strVal1;
 169         strVal2 = i.strVal2;
 170         strVal3 = i.strVal3;
 171         hashCode = i.hashCode;
 172     }
 173 
 174     /**
 175      * Sets this item to an integer item.
 176      *
 177      * @param intVal
 178      *            the value of this item.
 179      */
 180     void set(final int intVal) {
 181         this.type = ClassWriter.INT;
 182         this.intVal = intVal;
 183         this.hashCode = 0x7FFFFFFF & (type + intVal);
 184     }
 185 
 186     /**
 187      * Sets this item to a long item.
 188      *
 189      * @param longVal
 190      *            the value of this item.
 191      */
 192     void set(final long longVal) {
 193         this.type = ClassWriter.LONG;
 194         this.longVal = longVal;
 195         this.hashCode = 0x7FFFFFFF & (type + (int) longVal);
 196     }
 197 
 198     /**
 199      * Sets this item to a float item.
 200      *
 201      * @param floatVal
 202      *            the value of this item.
 203      */
 204     void set(final float floatVal) {
 205         this.type = ClassWriter.FLOAT;
 206         this.intVal = Float.floatToRawIntBits(floatVal);
 207         this.hashCode = 0x7FFFFFFF & (type + (int) floatVal);
 208     }
 209 
 210     /**
 211      * Sets this item to a double item.
 212      *
 213      * @param doubleVal
 214      *            the value of this item.
 215      */
 216     void set(final double doubleVal) {
 217         this.type = ClassWriter.DOUBLE;
 218         this.longVal = Double.doubleToRawLongBits(doubleVal);
 219         this.hashCode = 0x7FFFFFFF & (type + (int) doubleVal);
 220     }
 221 
 222     /**
 223      * Sets this item to an item that do not hold a primitive value.
 224      *
 225      * @param type
 226      *            the type of this item.
 227      * @param strVal1
 228      *            first part of the value of this item.
 229      * @param strVal2
 230      *            second part of the value of this item.
 231      * @param strVal3
 232      *            third part of the value of this item.
 233      */
 234     @SuppressWarnings("fallthrough")
 235     void set(final int type, final String strVal1, final String strVal2,
 236             final String strVal3) {
 237         this.type = type;
 238         this.strVal1 = strVal1;
 239         this.strVal2 = strVal2;
 240         this.strVal3 = strVal3;
 241         switch (type) {
 242         case ClassWriter.CLASS:
 243             this.intVal = 0;     // intVal of a class must be zero, see visitInnerClass
 244         case ClassWriter.UTF8:
 245         case ClassWriter.STR:
 246         case ClassWriter.MTYPE:
 247         case ClassWriter.MODULE:
 248         case ClassWriter.PACKAGE:
 249         case ClassWriter.TYPE_NORMAL:
 250             hashCode = 0x7FFFFFFF & (type + strVal1.hashCode());
 251             return;
 252         case ClassWriter.NAME_TYPE: {
 253             hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
 254                     * strVal2.hashCode());
 255             return;
 256         }
 257         // ClassWriter.FIELD:
 258         // ClassWriter.METH:
 259         // ClassWriter.IMETH:
 260         // ClassWriter.HANDLE_BASE + 1..9
 261         default:
 262             hashCode = 0x7FFFFFFF & (type + strVal1.hashCode()
 263                     * strVal2.hashCode() * strVal3.hashCode());
 264         }
 265     }
 266 
 267     /**
 268      * Sets the item to an InvokeDynamic item.
 269      *
 270      * @param name
 271      *            invokedynamic's name.
 272      * @param desc
 273      *            invokedynamic's desc.
 274      * @param bsmIndex
 275      *            zero based index into the class attribute BootrapMethods.
 276      */
 277     void set(String name, String desc, int bsmIndex) {
 278         this.type = ClassWriter.INDY;
 279         this.longVal = bsmIndex;
 280         this.strVal1 = name;
 281         this.strVal2 = desc;
 282         this.hashCode = 0x7FFFFFFF & (ClassWriter.INDY + bsmIndex
 283                 * strVal1.hashCode() * strVal2.hashCode());
 284     }
 285 
 286     /**
 287      * Sets the item to a BootstrapMethod item.
 288      *
 289      * @param position
 290      *            position in byte in the class attribute BootrapMethods.
 291      * @param hashCode
 292      *            hashcode of the item. This hashcode is processed from the
 293      *            hashcode of the bootstrap method and the hashcode of all
 294      *            bootstrap arguments.
 295      */
 296     void set(int position, int hashCode) {
 297         this.type = ClassWriter.BSM;
 298         this.intVal = position;
 299         this.hashCode = hashCode;
 300     }
 301 
 302     /**
 303      * Indicates if the given item is equal to this one. <i>This method assumes
 304      * that the two items have the same {@link #type}</i>.
 305      *
 306      * @param i
 307      *            the item to be compared to this one. Both items must have the
 308      *            same {@link #type}.
 309      * @return <tt>true</tt> if the given item if equal to this one,
 310      *         <tt>false</tt> otherwise.
 311      */
 312     boolean isEqualTo(final Item i) {
 313         switch (type) {
 314         case ClassWriter.UTF8:
 315         case ClassWriter.STR:
 316         case ClassWriter.CLASS:
 317         case ClassWriter.MODULE:
 318         case ClassWriter.PACKAGE:
 319         case ClassWriter.MTYPE:
 320         case ClassWriter.TYPE_NORMAL:
 321             return i.strVal1.equals(strVal1);
 322         case ClassWriter.TYPE_MERGED:
 323         case ClassWriter.LONG:
 324         case ClassWriter.DOUBLE:
 325             return i.longVal == longVal;
 326         case ClassWriter.INT:
 327         case ClassWriter.FLOAT:
 328             return i.intVal == intVal;
 329         case ClassWriter.TYPE_UNINIT:
 330             return i.intVal == intVal && i.strVal1.equals(strVal1);
 331         case ClassWriter.NAME_TYPE:
 332             return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2);
 333         case ClassWriter.INDY: {
 334             return i.longVal == longVal && i.strVal1.equals(strVal1)
 335                     && i.strVal2.equals(strVal2);
 336         }
 337         // case ClassWriter.FIELD:
 338         // case ClassWriter.METH:
 339         // case ClassWriter.IMETH:
 340         // case ClassWriter.HANDLE_BASE + 1..9
 341         default:
 342             return i.strVal1.equals(strVal1) && i.strVal2.equals(strVal2)
 343                     && i.strVal3.equals(strVal3);
 344         }
 345     }
 346 
 347 }