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