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  * The path to a type argument, wildcard bound, array element type, or static inner type within an
  63  * enclosing type.
  64  *
  65  * @author Eric Bruneton
  66  */
  67 public final class TypePath {
  68 
  69     /** A type path step that steps into the element type of an array type. See {@link #getStep}. */
  70     public static final int ARRAY_ELEMENT = 0;
  71 
  72     /** A type path step that steps into the nested type of a class type. See {@link #getStep}. */
  73     public static final int INNER_TYPE = 1;
  74 
  75     /** A type path step that steps into the bound of a wildcard type. See {@link #getStep}. */
  76     public static final int WILDCARD_BOUND = 2;
  77 
  78     /** A type path step that steps into a type argument of a generic type. See {@link #getStep}. */
  79     public static final int TYPE_ARGUMENT = 3;
  80 
  81     /**
  82       * The byte array where the 'type_path' structure - as defined in the Java Virtual Machine
  83       * Specification (JVMS) - corresponding to this TypePath is stored. The first byte of the
  84       * structure in this array is given by {@link #typePathOffset}.
  85       *
  86       * @see <a
  87       *     href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.20.2">JVMS
  88       *     4.7.20.2</a>
  89       */
  90     private final byte[] typePathContainer;
  91 
  92     /** The offset of the first byte of the type_path JVMS structure in {@link #typePathContainer}. */
  93     private final int typePathOffset;
  94 
  95     /**
  96       * Constructs a new TypePath.
  97       *
  98       * @param typePathContainer a byte array containing a type_path JVMS structure.
  99       * @param typePathOffset the offset of the first byte of the type_path structure in
 100       *     typePathContainer.
 101       */
 102     TypePath(final byte[] typePathContainer, final int typePathOffset) {
 103         this.typePathContainer = typePathContainer;
 104         this.typePathOffset = typePathOffset;
 105     }
 106 
 107     /**
 108       * Returns the length of this path, i.e. its number of steps.
 109       *
 110       * @return the length of this path.
 111       */
 112     public int getLength() {
 113         // path_length is stored in the first byte of a type_path.
 114         return typePathContainer[typePathOffset];
 115     }
 116 
 117     /**
 118       * Returns the value of the given step of this path.
 119       *
 120       * @param index an index between 0 and {@link #getLength()}, exclusive.
 121       * @return one of {@link #ARRAY_ELEMENT}, {@link #INNER_TYPE}, {@link #WILDCARD_BOUND}, or {@link
 122       *     #TYPE_ARGUMENT}.
 123       */
 124     public int getStep(final int index) {
 125         // Returns the type_path_kind of the path element of the given index.
 126         return typePathContainer[typePathOffset + 2 * index + 1];
 127     }
 128 
 129     /**
 130       * Returns the index of the type argument that the given step is stepping into. This method should
 131       * only be used for steps whose value is {@link #TYPE_ARGUMENT}.
 132       *
 133       * @param index an index between 0 and {@link #getLength()}, exclusive.
 134       * @return the index of the type argument that the given step is stepping into.
 135       */
 136     public int getStepArgument(final int index) {
 137         // Returns the type_argument_index of the path element of the given index.
 138         return typePathContainer[typePathOffset + 2 * index + 2];
 139     }
 140 
 141     /**
 142       * Converts a type path in string form, in the format used by {@link #toString()}, into a TypePath
 143       * object.
 144       *
 145       * @param typePath a type path in string form, in the format used by {@link #toString()}. May be
 146       *     {@literal null} or empty.
 147       * @return the corresponding TypePath object, or {@literal null} if the path is empty.
 148       */
 149     public static TypePath fromString(final String typePath) {
 150         if (typePath == null || typePath.isEmpty()) {
 151             return null;
 152         }
 153         int typePathLength = typePath.length();
 154         ByteVector output = new ByteVector(typePathLength);
 155         output.putByte(0);
 156         int typePathIndex = 0;
 157         while (typePathIndex < typePathLength) {
 158             char c = typePath.charAt(typePathIndex++);
 159             if (c == '[') {
 160                 output.put11(ARRAY_ELEMENT, 0);
 161             } else if (c == '.') {
 162                 output.put11(INNER_TYPE, 0);
 163             } else if (c == '*') {
 164                 output.put11(WILDCARD_BOUND, 0);
 165             } else if (c >= '0' && c <= '9') {
 166                 int typeArg = c - '0';
 167                 while (typePathIndex < typePathLength) {
 168                     c = typePath.charAt(typePathIndex++);
 169                     if (c >= '0' && c <= '9') {
 170                         typeArg = typeArg * 10 + c - '0';
 171                     } else if (c == ';') {
 172                         break;
 173                     } else {
 174                         throw new IllegalArgumentException();
 175                     }
 176                 }
 177                 output.put11(TYPE_ARGUMENT, typeArg);
 178             } else {
 179                 throw new IllegalArgumentException();
 180             }
 181         }
 182         output.data[0] = (byte) (output.length / 2);
 183         return new TypePath(output.data, 0);
 184     }
 185 
 186     /**
 187       * Returns a string representation of this type path. {@link #ARRAY_ELEMENT} steps are represented
 188       * with '[', {@link #INNER_TYPE} steps with '.', {@link #WILDCARD_BOUND} steps with '*' and {@link
 189       * #TYPE_ARGUMENT} steps with their type argument index in decimal form followed by ';'.
 190       */
 191     @Override
 192     public String toString() {
 193         int length = getLength();
 194         StringBuilder result = new StringBuilder(length * 2);
 195         for (int i = 0; i < length; ++i) {
 196             switch (getStep(i)) {
 197                 case ARRAY_ELEMENT:
 198                     result.append('[');
 199                     break;
 200                 case INNER_TYPE:
 201                     result.append('.');
 202                     break;
 203                 case WILDCARD_BOUND:
 204                     result.append('*');
 205                     break;
 206                 case TYPE_ARGUMENT:
 207                     result.append(getStepArgument(i)).append(';');
 208                     break;
 209                 default:
 210                     throw new AssertionError();
 211             }
 212         }
 213         return result.toString();
 214     }
 215 
 216     /**
 217       * Puts the type_path JVMS structure corresponding to the given TypePath into the given
 218       * ByteVector.
 219       *
 220       * @param typePath a TypePath instance, or {@literal null} for empty paths.
 221       * @param output where the type path must be put.
 222       */
 223     static void put(final TypePath typePath, final ByteVector output) {
 224         if (typePath == null) {
 225             output.putByte(0);
 226         } else {
 227             int length = typePath.typePathContainer[typePath.typePathOffset] * 2 + 1;
 228             output.putByteArray(typePath.typePathContainer, typePath.typePathOffset, length);
 229         }
 230     }
 231 }