1 /*** 2 * ASM: a very small and fast Java bytecode manipulation framework 3 * Copyright (c) 2000-2013 INRIA, France Telecom 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the copyright holders nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 package org.objectweb.asm; 32 33 /** 34 * A reference to a type appearing in a class, field or method declaration, or 35 * on an instruction. Such a reference designates the part of the class where 36 * the referenced type is appearing (e.g. an 'extends', 'implements' or 'throws' 37 * clause, a 'new' instruction, a 'catch' clause, a type cast, a local variable 38 * declaration, etc). 39 * 40 * @author Eric Bruneton 41 */ 42 public class TypeReference { 43 44 /** 45 * The sort of type references that target a type parameter of a generic 46 * class. See {@link #getSort getSort}. 47 */ 48 public final static int CLASS_TYPE_PARAMETER = 0x00; 49 50 /** 51 * The sort of type references that target a type parameter of a generic 52 * method. See {@link #getSort getSort}. 53 */ 54 public final static int METHOD_TYPE_PARAMETER = 0x01; 55 56 /** 57 * The sort of type references that target the super class of a class or one 58 * of the interfaces it implements. See {@link #getSort getSort}. 59 */ 60 public final static int CLASS_EXTENDS = 0x10; 61 62 /** 63 * The sort of type references that target a bound of a type parameter of a 64 * generic class. See {@link #getSort getSort}. 65 */ 66 public final static int CLASS_TYPE_PARAMETER_BOUND = 0x11; 67 68 /** 69 * The sort of type references that target a bound of a type parameter of a 70 * generic method. See {@link #getSort getSort}. 71 */ 72 public final static int METHOD_TYPE_PARAMETER_BOUND = 0x12; 73 74 /** 75 * The sort of type references that target the type of a field. See 76 * {@link #getSort getSort}. 77 */ 78 public final static int FIELD = 0x13; 79 80 /** 81 * The sort of type references that target the return type of a method. See 82 * {@link #getSort getSort}. 83 */ 84 public final static int METHOD_RETURN = 0x14; 85 86 /** 87 * The sort of type references that target the receiver type of a method. 88 * See {@link #getSort getSort}. 89 */ 90 public final static int METHOD_RECEIVER = 0x15; 91 92 /** 93 * The sort of type references that target the type of a formal parameter of 94 * a method. See {@link #getSort getSort}. 95 */ 96 public final static int METHOD_FORMAL_PARAMETER = 0x16; 97 98 /** 99 * The sort of type references that target the type of an exception declared 100 * in the throws clause of a method. See {@link #getSort getSort}. 101 */ 102 public final static int THROWS = 0x17; 103 104 /** 105 * The sort of type references that target the type of a local variable in a 106 * method. See {@link #getSort getSort}. 107 */ 108 public final static int LOCAL_VARIABLE = 0x40; 109 110 /** 111 * The sort of type references that target the type of a resource variable 112 * in a method. See {@link #getSort getSort}. 113 */ 114 public final static int RESOURCE_VARIABLE = 0x41; 115 116 /** 117 * The sort of type references that target the type of the exception of a 118 * 'catch' clause in a method. See {@link #getSort getSort}. 119 */ 120 public final static int EXCEPTION_PARAMETER = 0x42; 121 122 /** 123 * The sort of type references that target the type declared in an 124 * 'instanceof' instruction. See {@link #getSort getSort}. 125 */ 126 public final static int INSTANCEOF = 0x43; 127 128 /** 129 * The sort of type references that target the type of the object created by 130 * a 'new' instruction. See {@link #getSort getSort}. 131 */ 132 public final static int NEW = 0x44; 133 134 /** 135 * The sort of type references that target the receiver type of a 136 * constructor reference. See {@link #getSort getSort}. 137 */ 138 public final static int CONSTRUCTOR_REFERENCE = 0x45; 139 140 /** 141 * The sort of type references that target the receiver type of a method 142 * reference. See {@link #getSort getSort}. 143 */ 144 public final static int METHOD_REFERENCE = 0x46; 145 146 /** 147 * The sort of type references that target the type declared in an explicit 148 * or implicit cast instruction. See {@link #getSort getSort}. 149 */ 150 public final static int CAST = 0x47; 151 152 /** 153 * The sort of type references that target a type parameter of a generic 154 * constructor in a constructor call. See {@link #getSort getSort}. 155 */ 156 public final static int CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = 0x48; 157 158 /** 159 * The sort of type references that target a type parameter of a generic 160 * method in a method call. See {@link #getSort getSort}. 161 */ 162 public final static int METHOD_INVOCATION_TYPE_ARGUMENT = 0x49; 163 164 /** 165 * The sort of type references that target a type parameter of a generic 166 * constructor in a constructor reference. See {@link #getSort getSort}. 167 */ 168 public final static int CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = 0x4A; 169 170 /** 171 * The sort of type references that target a type parameter of a generic 172 * method in a method reference. See {@link #getSort getSort}. 173 */ 174 public final static int METHOD_REFERENCE_TYPE_ARGUMENT = 0x4B; 175 176 /** 177 * The type reference value in Java class file format. 178 */ 179 private int value; 180 181 /** 182 * Creates a new TypeReference. 183 * 184 * @param typeRef 185 * the int encoded value of the type reference, as received in a 186 * visit method related to type annotations, like 187 * visitTypeAnnotation. 188 */ TypeReference(int typeRef)189 public TypeReference(int typeRef) { 190 this.value = typeRef; 191 } 192 193 /** 194 * Returns a type reference of the given sort. 195 * 196 * @param sort 197 * {@link #FIELD FIELD}, {@link #METHOD_RETURN METHOD_RETURN}, 198 * {@link #METHOD_RECEIVER METHOD_RECEIVER}, 199 * {@link #LOCAL_VARIABLE LOCAL_VARIABLE}, 200 * {@link #RESOURCE_VARIABLE RESOURCE_VARIABLE}, 201 * {@link #INSTANCEOF INSTANCEOF}, {@link #NEW NEW}, 202 * {@link #CONSTRUCTOR_REFERENCE CONSTRUCTOR_REFERENCE}, or 203 * {@link #METHOD_REFERENCE METHOD_REFERENCE}. 204 * @return a type reference of the given sort. 205 */ newTypeReference(int sort)206 public static TypeReference newTypeReference(int sort) { 207 return new TypeReference(sort << 24); 208 } 209 210 /** 211 * Returns a reference to a type parameter of a generic class or method. 212 * 213 * @param sort 214 * {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER} or 215 * {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}. 216 * @param paramIndex 217 * the type parameter index. 218 * @return a reference to the given generic class or method type parameter. 219 */ newTypeParameterReference(int sort, int paramIndex)220 public static TypeReference newTypeParameterReference(int sort, 221 int paramIndex) { 222 return new TypeReference((sort << 24) | (paramIndex << 16)); 223 } 224 225 /** 226 * Returns a reference to a type parameter bound of a generic class or 227 * method. 228 * 229 * @param sort 230 * {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER} or 231 * {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}. 232 * @param paramIndex 233 * the type parameter index. 234 * @param boundIndex 235 * the type bound index within the above type parameters. 236 * @return a reference to the given generic class or method type parameter 237 * bound. 238 */ newTypeParameterBoundReference(int sort, int paramIndex, int boundIndex)239 public static TypeReference newTypeParameterBoundReference(int sort, 240 int paramIndex, int boundIndex) { 241 return new TypeReference((sort << 24) | (paramIndex << 16) 242 | (boundIndex << 8)); 243 } 244 245 /** 246 * Returns a reference to the super class or to an interface of the 247 * 'implements' clause of a class. 248 * 249 * @param itfIndex 250 * the index of an interface in the 'implements' clause of a 251 * class, or -1 to reference the super class of the class. 252 * @return a reference to the given super type of a class. 253 */ newSuperTypeReference(int itfIndex)254 public static TypeReference newSuperTypeReference(int itfIndex) { 255 itfIndex &= 0xFFFF; 256 return new TypeReference((CLASS_EXTENDS << 24) | (itfIndex << 8)); 257 } 258 259 /** 260 * Returns a reference to the type of a formal parameter of a method. 261 * 262 * @param paramIndex 263 * the formal parameter index. 264 * 265 * @return a reference to the type of the given method formal parameter. 266 */ newFormalParameterReference(int paramIndex)267 public static TypeReference newFormalParameterReference(int paramIndex) { 268 return new TypeReference((METHOD_FORMAL_PARAMETER << 24) 269 | (paramIndex << 16)); 270 } 271 272 /** 273 * Returns a reference to the type of an exception, in a 'throws' clause of 274 * a method. 275 * 276 * @param exceptionIndex 277 * the index of an exception in a 'throws' clause of a method. 278 * 279 * @return a reference to the type of the given exception. 280 */ newExceptionReference(int exceptionIndex)281 public static TypeReference newExceptionReference(int exceptionIndex) { 282 return new TypeReference((THROWS << 24) | (exceptionIndex << 8)); 283 } 284 285 /** 286 * Returns a reference to the type of the exception declared in a 'catch' 287 * clause of a method. 288 * 289 * @param tryCatchBlockIndex 290 * the index of a try catch block (using the order in which they 291 * are visited with visitTryCatchBlock). 292 * 293 * @return a reference to the type of the given exception. 294 */ newTryCatchReference(int tryCatchBlockIndex)295 public static TypeReference newTryCatchReference(int tryCatchBlockIndex) { 296 return new TypeReference((EXCEPTION_PARAMETER << 24) 297 | (tryCatchBlockIndex << 8)); 298 } 299 300 /** 301 * Returns a reference to the type of a type argument in a constructor or 302 * method call or reference. 303 * 304 * @param sort 305 * {@link #CAST CAST}, 306 * {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT 307 * CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT}, 308 * {@link #METHOD_INVOCATION_TYPE_ARGUMENT 309 * METHOD_INVOCATION_TYPE_ARGUMENT}, 310 * {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT 311 * CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or 312 * {@link #METHOD_REFERENCE_TYPE_ARGUMENT 313 * METHOD_REFERENCE_TYPE_ARGUMENT}. 314 * @param argIndex 315 * the type argument index. 316 * 317 * @return a reference to the type of the given type argument. 318 */ newTypeArgumentReference(int sort, int argIndex)319 public static TypeReference newTypeArgumentReference(int sort, int argIndex) { 320 return new TypeReference((sort << 24) | argIndex); 321 } 322 323 /** 324 * Returns the sort of this type reference. 325 * 326 * @return {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER}, 327 * {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}, 328 * {@link #CLASS_EXTENDS CLASS_EXTENDS}, 329 * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND}, 330 * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}, 331 * {@link #FIELD FIELD}, {@link #METHOD_RETURN METHOD_RETURN}, 332 * {@link #METHOD_RECEIVER METHOD_RECEIVER}, 333 * {@link #METHOD_FORMAL_PARAMETER METHOD_FORMAL_PARAMETER}, 334 * {@link #THROWS THROWS}, {@link #LOCAL_VARIABLE LOCAL_VARIABLE}, 335 * {@link #RESOURCE_VARIABLE RESOURCE_VARIABLE}, 336 * {@link #EXCEPTION_PARAMETER EXCEPTION_PARAMETER}, 337 * {@link #INSTANCEOF INSTANCEOF}, {@link #NEW NEW}, 338 * {@link #CONSTRUCTOR_REFERENCE CONSTRUCTOR_REFERENCE}, 339 * {@link #METHOD_REFERENCE METHOD_REFERENCE}, {@link #CAST CAST}, 340 * {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT 341 * CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT}, 342 * {@link #METHOD_INVOCATION_TYPE_ARGUMENT 343 * METHOD_INVOCATION_TYPE_ARGUMENT}, 344 * {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT 345 * CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or 346 * {@link #METHOD_REFERENCE_TYPE_ARGUMENT 347 * METHOD_REFERENCE_TYPE_ARGUMENT}. 348 */ getSort()349 public int getSort() { 350 return value >>> 24; 351 } 352 353 /** 354 * Returns the index of the type parameter referenced by this type 355 * reference. This method must only be used for type references whose sort 356 * is {@link #CLASS_TYPE_PARAMETER CLASS_TYPE_PARAMETER}, 357 * {@link #METHOD_TYPE_PARAMETER METHOD_TYPE_PARAMETER}, 358 * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or 359 * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}. 360 * 361 * @return a type parameter index. 362 */ getTypeParameterIndex()363 public int getTypeParameterIndex() { 364 return (value & 0x00FF0000) >> 16; 365 } 366 367 /** 368 * Returns the index of the type parameter bound, within the type parameter 369 * {@link #getTypeParameterIndex}, referenced by this type reference. This 370 * method must only be used for type references whose sort is 371 * {@link #CLASS_TYPE_PARAMETER_BOUND CLASS_TYPE_PARAMETER_BOUND} or 372 * {@link #METHOD_TYPE_PARAMETER_BOUND METHOD_TYPE_PARAMETER_BOUND}. 373 * 374 * @return a type parameter bound index. 375 */ getTypeParameterBoundIndex()376 public int getTypeParameterBoundIndex() { 377 return (value & 0x0000FF00) >> 8; 378 } 379 380 /** 381 * Returns the index of the "super type" of a class that is referenced by 382 * this type reference. This method must only be used for type references 383 * whose sort is {@link #CLASS_EXTENDS CLASS_EXTENDS}. 384 * 385 * @return the index of an interface in the 'implements' clause of a class, 386 * or -1 if this type reference references the type of the super 387 * class. 388 */ getSuperTypeIndex()389 public int getSuperTypeIndex() { 390 return (short) ((value & 0x00FFFF00) >> 8); 391 } 392 393 /** 394 * Returns the index of the formal parameter whose type is referenced by 395 * this type reference. This method must only be used for type references 396 * whose sort is {@link #METHOD_FORMAL_PARAMETER METHOD_FORMAL_PARAMETER}. 397 * 398 * @return a formal parameter index. 399 */ getFormalParameterIndex()400 public int getFormalParameterIndex() { 401 return (value & 0x00FF0000) >> 16; 402 } 403 404 /** 405 * Returns the index of the exception, in a 'throws' clause of a method, 406 * whose type is referenced by this type reference. This method must only be 407 * used for type references whose sort is {@link #THROWS THROWS}. 408 * 409 * @return the index of an exception in the 'throws' clause of a method. 410 */ getExceptionIndex()411 public int getExceptionIndex() { 412 return (value & 0x00FFFF00) >> 8; 413 } 414 415 /** 416 * Returns the index of the try catch block (using the order in which they 417 * are visited with visitTryCatchBlock), whose 'catch' type is referenced by 418 * this type reference. This method must only be used for type references 419 * whose sort is {@link #EXCEPTION_PARAMETER EXCEPTION_PARAMETER} . 420 * 421 * @return the index of an exception in the 'throws' clause of a method. 422 */ getTryCatchBlockIndex()423 public int getTryCatchBlockIndex() { 424 return (value & 0x00FFFF00) >> 8; 425 } 426 427 /** 428 * Returns the index of the type argument referenced by this type reference. 429 * This method must only be used for type references whose sort is 430 * {@link #CAST CAST}, {@link #CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT 431 * CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT}, 432 * {@link #METHOD_INVOCATION_TYPE_ARGUMENT METHOD_INVOCATION_TYPE_ARGUMENT}, 433 * {@link #CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT 434 * CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT}, or 435 * {@link #METHOD_REFERENCE_TYPE_ARGUMENT METHOD_REFERENCE_TYPE_ARGUMENT}. 436 * 437 * @return a type parameter index. 438 */ getTypeArgumentIndex()439 public int getTypeArgumentIndex() { 440 return value & 0xFF; 441 } 442 443 /** 444 * Returns the int encoded value of this type reference, suitable for use in 445 * visit methods related to type annotations, like visitTypeAnnotation. 446 * 447 * @return the int encoded value of this type reference. 448 */ getValue()449 public int getValue() { 450 return value; 451 } 452 } 453