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 reference to a type appearing in a class, field or method declaration, or on an instruction. 63 * Such a reference designates the part of the class where the referenced type is appearing (e.g. an 64 * 'extends', 'implements' or 'throws' clause, a 'new' instruction, a 'catch' clause, a type cast, a 65 * local variable declaration, etc). 66 * 67 * @author Eric Bruneton 68 */ 69 public class TypeReference { 70 71 /** 72 * The sort of type references that target a type parameter of a generic class. See {@link 73 * #getSort}. 74 */ 75 public static final int CLASS_TYPE_PARAMETER = 0x00; 76 77 /** 78 * The sort of type references that target a type parameter of a generic method. See {@link 79 * #getSort}. 80 */ 81 public static final int METHOD_TYPE_PARAMETER = 0x01; 82 83 /** 84 * The sort of type references that target the super class of a class or one of the interfaces it 85 * implements. See {@link #getSort}. 86 */ 87 public static final int CLASS_EXTENDS = 0x10; 88 89 /** 90 * The sort of type references that target a bound of a type parameter of a generic class. See 91 * {@link #getSort}. 92 */ 93 public static final int CLASS_TYPE_PARAMETER_BOUND = 0x11; 94 95 /** 96 * The sort of type references that target a bound of a type parameter of a generic method. See 97 * {@link #getSort}. 98 */ 99 public static final int METHOD_TYPE_PARAMETER_BOUND = 0x12; 100 101 /** The sort of type references that target the type of a field. See {@link #getSort}. */ 102 public static final int FIELD = 0x13; 103 104 /** The sort of type references that target the return type of a method. See {@link #getSort}. */ 105 public static final int METHOD_RETURN = 0x14; 106 107 /** 108 * The sort of type references that target the receiver type of a method. See {@link #getSort}. 109 */ 110 public static final int METHOD_RECEIVER = 0x15; 111 112 /** 113 * The sort of type references that target the type of a formal parameter of a method. See {@link 114 * #getSort}. 115 */ 116 public static final int METHOD_FORMAL_PARAMETER = 0x16; 117 118 /** 119 * The sort of type references that target the type of an exception declared in the throws clause 120 * of a method. See {@link #getSort}. 121 */ 122 public static final int THROWS = 0x17; 123 124 /** 125 * The sort of type references that target the type of a local variable in a method. See {@link 126 * #getSort}. 127 */ 128 public static final int LOCAL_VARIABLE = 0x40; 129 130 /** 131 * The sort of type references that target the type of a resource variable in a method. See {@link 132 * #getSort}. 133 */ 134 public static final int RESOURCE_VARIABLE = 0x41; 135 136 /** 137 * The sort of type references that target the type of the exception of a 'catch' clause in a 138 * method. See {@link #getSort}. 139 */ 140 public static final int EXCEPTION_PARAMETER = 0x42; 141 142 /** 143 * The sort of type references that target the type declared in an 'instanceof' instruction. See 144 * {@link #getSort}. 145 */ 146 public static final int INSTANCEOF = 0x43; 147 148 /** 149 * The sort of type references that target the type of the object created by a 'new' instruction. 150 * See {@link #getSort}. 151 */ 152 public static final int NEW = 0x44; 153 154 /** 155 * The sort of type references that target the receiver type of a constructor reference. See 156 * {@link #getSort}. 157 */ 158 public static final int CONSTRUCTOR_REFERENCE = 0x45; 159 160 /** 161 * The sort of type references that target the receiver type of a method reference. See {@link 162 * #getSort}. 163 */ 164 public static final int METHOD_REFERENCE = 0x46; 165 166 /** 167 * The sort of type references that target the type declared in an explicit or implicit cast 168 * instruction. See {@link #getSort}. 169 */ 170 public static final int CAST = 0x47; 171 172 /** 173 * The sort of type references that target a type parameter of a generic constructor in a 174 * constructor call. See {@link #getSort}. 175 */ 176 public static final int CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = 0x48; 177 178 /** 179 * The sort of type references that target a type parameter of a generic method in a method call. 180 * See {@link #getSort}. 181 */ 182 public static final int METHOD_INVOCATION_TYPE_ARGUMENT = 0x49; 183 184 /** 185 * The sort of type references that target a type parameter of a generic constructor in a 186 * constructor reference. See {@link #getSort}. 187 */ 188 public static final int CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = 0x4A; 189 190 /** 191 * The sort of type references that target a type parameter of a generic method in a method 192 * reference. See {@link #getSort}. 193 */ 194 public static final int METHOD_REFERENCE_TYPE_ARGUMENT = 0x4B; 195 196 /** 197 * The target_type and target_info structures - as defined in the Java Virtual Machine 198 * Specification (JVMS) - corresponding to this type reference. target_type uses one byte, and all 199 * the target_info union fields use up to 3 bytes (except localvar_target, handled with the 200 * specific method {@link MethodVisitor#visitLocalVariableAnnotation}). Thus, both structures can 201 * be stored in an int. 202 * 203 * <p>This int field stores target_type (called the TypeReference 'sort' in the public API of this 204 * class) in its most significant byte, followed by the target_info fields. Depending on 205 * target_type, 1, 2 or even 3 least significant bytes of this field are unused. target_info 206 * fields which reference bytecode offsets are set to 0 (these offsets are ignored in ClassReader, 207 * and recomputed in MethodWriter). 208 * 209 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.20">JVMS 210 * 4.7.20</a> 211 * @see <a 212 * href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.7.20.1">JVMS 213 * 4.7.20.1</a> 214 */ 215 private final int targetTypeAndInfo; 216 217 /** 218 * Constructs a new TypeReference. 219 * 220 * @param typeRef the int encoded value of the type reference, as received in a visit method 221 * related to type annotations, such as {@link ClassVisitor#visitTypeAnnotation}. 222 */ TypeReference(final int typeRef)223 public TypeReference(final int typeRef) { 224 this.targetTypeAndInfo = typeRef; 225 } 226 227 /** 228 * Returns a type reference of the given sort. 229 * 230 * @param sort one of {@link #FIELD}, {@link #METHOD_RETURN}, {@link #METHOD_RECEIVER}, {@link 231 * #LOCAL_VARIABLE}, {@link #RESOURCE_VARIABLE}, {@link #INSTANCEOF}, {@link #NEW}, {@link 232 * #CONSTRUCTOR_REFERENCE}, or {@link #METHOD_REFERENCE}. 233 * @return a type reference of the given sort. 234 */ newTypeReference(final int sort)235 public static TypeReference newTypeReference(final int sort) { 236 return new TypeReference(sort << 24); 237 } 238 239 /** 240 * Returns a reference to a type parameter of a generic class or method. 241 * 242 * @param sort one of {@link #CLASS_TYPE_PARAMETER} or {@link #METHOD_TYPE_PARAMETER}. 243 * @param paramIndex the type parameter index. 244 * @return a reference to the given generic class or method type parameter. 245 */ newTypeParameterReference(final int sort, final int paramIndex)246 public static TypeReference newTypeParameterReference(final int sort, final int paramIndex) { 247 return new TypeReference((sort << 24) | (paramIndex << 16)); 248 } 249 250 /** 251 * Returns a reference to a type parameter bound of a generic class or method. 252 * 253 * @param sort one of {@link #CLASS_TYPE_PARAMETER} or {@link #METHOD_TYPE_PARAMETER}. 254 * @param paramIndex the type parameter index. 255 * @param boundIndex the type bound index within the above type parameters. 256 * @return a reference to the given generic class or method type parameter bound. 257 */ newTypeParameterBoundReference( final int sort, final int paramIndex, final int boundIndex)258 public static TypeReference newTypeParameterBoundReference( 259 final int sort, final int paramIndex, final int boundIndex) { 260 return new TypeReference((sort << 24) | (paramIndex << 16) | (boundIndex << 8)); 261 } 262 263 /** 264 * Returns a reference to the super class or to an interface of the 'implements' clause of a 265 * class. 266 * 267 * @param itfIndex the index of an interface in the 'implements' clause of a class, or -1 to 268 * reference the super class of the class. 269 * @return a reference to the given super type of a class. 270 */ newSuperTypeReference(final int itfIndex)271 public static TypeReference newSuperTypeReference(final int itfIndex) { 272 return new TypeReference((CLASS_EXTENDS << 24) | ((itfIndex & 0xFFFF) << 8)); 273 } 274 275 /** 276 * Returns a reference to the type of a formal parameter of a method. 277 * 278 * @param paramIndex the formal parameter index. 279 * @return a reference to the type of the given method formal parameter. 280 */ newFormalParameterReference(final int paramIndex)281 public static TypeReference newFormalParameterReference(final int paramIndex) { 282 return new TypeReference((METHOD_FORMAL_PARAMETER << 24) | (paramIndex << 16)); 283 } 284 285 /** 286 * Returns a reference to the type of an exception, in a 'throws' clause of a method. 287 * 288 * @param exceptionIndex the index of an exception in a 'throws' clause of a method. 289 * @return a reference to the type of the given exception. 290 */ newExceptionReference(final int exceptionIndex)291 public static TypeReference newExceptionReference(final int exceptionIndex) { 292 return new TypeReference((THROWS << 24) | (exceptionIndex << 8)); 293 } 294 295 /** 296 * Returns a reference to the type of the exception declared in a 'catch' clause of a method. 297 * 298 * @param tryCatchBlockIndex the index of a try catch block (using the order in which they are 299 * visited with visitTryCatchBlock). 300 * @return a reference to the type of the given exception. 301 */ newTryCatchReference(final int tryCatchBlockIndex)302 public static TypeReference newTryCatchReference(final int tryCatchBlockIndex) { 303 return new TypeReference((EXCEPTION_PARAMETER << 24) | (tryCatchBlockIndex << 8)); 304 } 305 306 /** 307 * Returns a reference to the type of a type argument in a constructor or method call or 308 * reference. 309 * 310 * @param sort one of {@link #CAST}, {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT}, {@link 311 * #METHOD_INVOCATION_TYPE_ARGUMENT}, {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or {@link 312 * #METHOD_REFERENCE_TYPE_ARGUMENT}. 313 * @param argIndex the type argument index. 314 * @return a reference to the type of the given type argument. 315 */ newTypeArgumentReference(final int sort, final int argIndex)316 public static TypeReference newTypeArgumentReference(final int sort, final int argIndex) { 317 return new TypeReference((sort << 24) | argIndex); 318 } 319 320 /** 321 * Returns the sort of this type reference. 322 * 323 * @return one of {@link #CLASS_TYPE_PARAMETER}, {@link #METHOD_TYPE_PARAMETER}, {@link 324 * #CLASS_EXTENDS}, {@link #CLASS_TYPE_PARAMETER_BOUND}, {@link #METHOD_TYPE_PARAMETER_BOUND}, 325 * {@link #FIELD}, {@link #METHOD_RETURN}, {@link #METHOD_RECEIVER}, {@link 326 * #METHOD_FORMAL_PARAMETER}, {@link #THROWS}, {@link #LOCAL_VARIABLE}, {@link 327 * #RESOURCE_VARIABLE}, {@link #EXCEPTION_PARAMETER}, {@link #INSTANCEOF}, {@link #NEW}, 328 * {@link #CONSTRUCTOR_REFERENCE}, {@link #METHOD_REFERENCE}, {@link #CAST}, {@link 329 * #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT}, {@link #METHOD_INVOCATION_TYPE_ARGUMENT}, {@link 330 * #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or {@link #METHOD_REFERENCE_TYPE_ARGUMENT}. 331 */ getSort()332 public int getSort() { 333 return targetTypeAndInfo >>> 24; 334 } 335 336 /** 337 * Returns the index of the type parameter referenced by this type reference. This method must 338 * only be used for type references whose sort is {@link #CLASS_TYPE_PARAMETER}, {@link 339 * #METHOD_TYPE_PARAMETER}, {@link #CLASS_TYPE_PARAMETER_BOUND} or {@link 340 * #METHOD_TYPE_PARAMETER_BOUND}. 341 * 342 * @return a type parameter index. 343 */ getTypeParameterIndex()344 public int getTypeParameterIndex() { 345 return (targetTypeAndInfo & 0x00FF0000) >> 16; 346 } 347 348 /** 349 * Returns the index of the type parameter bound, within the type parameter {@link 350 * #getTypeParameterIndex}, referenced by this type reference. This method must only be used for 351 * type references whose sort is {@link #CLASS_TYPE_PARAMETER_BOUND} or {@link 352 * #METHOD_TYPE_PARAMETER_BOUND}. 353 * 354 * @return a type parameter bound index. 355 */ getTypeParameterBoundIndex()356 public int getTypeParameterBoundIndex() { 357 return (targetTypeAndInfo & 0x0000FF00) >> 8; 358 } 359 360 /** 361 * Returns the index of the "super type" of a class that is referenced by this type reference. 362 * This method must only be used for type references whose sort is {@link #CLASS_EXTENDS}. 363 * 364 * @return the index of an interface in the 'implements' clause of a class, or -1 if this type 365 * reference references the type of the super class. 366 */ getSuperTypeIndex()367 public int getSuperTypeIndex() { 368 return (short) ((targetTypeAndInfo & 0x00FFFF00) >> 8); 369 } 370 371 /** 372 * Returns the index of the formal parameter whose type is referenced by this type reference. This 373 * method must only be used for type references whose sort is {@link #METHOD_FORMAL_PARAMETER}. 374 * 375 * @return a formal parameter index. 376 */ getFormalParameterIndex()377 public int getFormalParameterIndex() { 378 return (targetTypeAndInfo & 0x00FF0000) >> 16; 379 } 380 381 /** 382 * Returns the index of the exception, in a 'throws' clause of a method, whose type is referenced 383 * by this type reference. This method must only be used for type references whose sort is {@link 384 * #THROWS}. 385 * 386 * @return the index of an exception in the 'throws' clause of a method. 387 */ getExceptionIndex()388 public int getExceptionIndex() { 389 return (targetTypeAndInfo & 0x00FFFF00) >> 8; 390 } 391 392 /** 393 * Returns the index of the try catch block (using the order in which they are visited with 394 * visitTryCatchBlock), whose 'catch' type is referenced by this type reference. This method must 395 * only be used for type references whose sort is {@link #EXCEPTION_PARAMETER} . 396 * 397 * @return the index of an exception in the 'throws' clause of a method. 398 */ getTryCatchBlockIndex()399 public int getTryCatchBlockIndex() { 400 return (targetTypeAndInfo & 0x00FFFF00) >> 8; 401 } 402 403 /** 404 * Returns the index of the type argument referenced by this type reference. This method must only 405 * be used for type references whose sort is {@link #CAST}, {@link 406 * #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT}, {@link #METHOD_INVOCATION_TYPE_ARGUMENT}, {@link 407 * #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or {@link #METHOD_REFERENCE_TYPE_ARGUMENT}. 408 * 409 * @return a type parameter index. 410 */ getTypeArgumentIndex()411 public int getTypeArgumentIndex() { 412 return targetTypeAndInfo & 0xFF; 413 } 414 415 /** 416 * Returns the int encoded value of this type reference, suitable for use in visit methods related 417 * to type annotations, like visitTypeAnnotation. 418 * 419 * @return the int encoded value of this type reference. 420 */ getValue()421 public int getValue() { 422 return targetTypeAndInfo; 423 } 424 425 /** 426 * Puts the given target_type and target_info JVMS structures into the given ByteVector. 427 * 428 * @param targetTypeAndInfo a target_type and a target_info structures encoded as in {@link 429 * #targetTypeAndInfo}. LOCAL_VARIABLE and RESOURCE_VARIABLE target types are not supported. 430 * @param output where the type reference must be put. 431 */ putTarget(final int targetTypeAndInfo, final ByteVector output)432 static void putTarget(final int targetTypeAndInfo, final ByteVector output) { 433 switch (targetTypeAndInfo >>> 24) { 434 case CLASS_TYPE_PARAMETER: 435 case METHOD_TYPE_PARAMETER: 436 case METHOD_FORMAL_PARAMETER: 437 output.putShort(targetTypeAndInfo >>> 16); 438 break; 439 case FIELD: 440 case METHOD_RETURN: 441 case METHOD_RECEIVER: 442 output.putByte(targetTypeAndInfo >>> 24); 443 break; 444 case CAST: 445 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: 446 case METHOD_INVOCATION_TYPE_ARGUMENT: 447 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: 448 case METHOD_REFERENCE_TYPE_ARGUMENT: 449 output.putInt(targetTypeAndInfo); 450 break; 451 case CLASS_EXTENDS: 452 case CLASS_TYPE_PARAMETER_BOUND: 453 case METHOD_TYPE_PARAMETER_BOUND: 454 case THROWS: 455 case EXCEPTION_PARAMETER: 456 case INSTANCEOF: 457 case NEW: 458 case CONSTRUCTOR_REFERENCE: 459 case METHOD_REFERENCE: 460 output.put12(targetTypeAndInfo >>> 24, (targetTypeAndInfo & 0xFFFF00) >> 8); 461 break; 462 default: 463 throw new IllegalArgumentException(); 464 } 465 } 466 } 467