src/share/classes/com/sun/tools/classfile/ExtendedAnnotation.java

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 798
4868a36f6fd8
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

jjg@309 1 /*
ohair@554 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
jjg@309 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@309 4 *
jjg@309 5 * This code is free software; you can redistribute it and/or modify it
jjg@309 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@309 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@309 10 *
jjg@309 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@309 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@309 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@309 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@309 15 * accompanied this code).
jjg@309 16 *
jjg@309 17 * You should have received a copy of the GNU General Public License version
jjg@309 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@309 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@309 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@309 24 */
jjg@309 25
jjg@309 26 package com.sun.tools.classfile;
jjg@309 27
jjg@309 28 import java.io.IOException;
jjg@309 29 import java.util.ArrayList;
jjg@309 30 import java.util.EnumSet;
jjg@309 31 import java.util.List;
jjg@309 32 import java.util.Set;
jjg@309 33
jjg@309 34 import static com.sun.tools.classfile.ExtendedAnnotation.TargetAttribute.*;
jjg@309 35
jjg@309 36 /**
jjg@309 37 * See JSR 308 specification, section 4.1
jjg@309 38 *
jjg@581 39 * <p><b>This is NOT part of any supported API.
jjg@581 40 * If you write code that depends on this, you do so at your own risk.
jjg@309 41 * This code and its internal interfaces are subject to change or
jjg@309 42 * deletion without notice.</b>
jjg@309 43 */
jjg@309 44 public class ExtendedAnnotation {
jjg@309 45 ExtendedAnnotation(ClassReader cr) throws IOException, Annotation.InvalidAnnotation {
jjg@309 46 annotation = new Annotation(cr);
jjg@309 47 position = read_position(cr);
jjg@309 48 }
jjg@309 49
jjg@309 50 public ExtendedAnnotation(ConstantPool constant_pool,
jjg@309 51 Annotation annotation, Position position) {
jjg@309 52 this.annotation = annotation;
jjg@309 53 this.position = position;
jjg@309 54 }
jjg@309 55
jjg@309 56 public int length() {
jjg@309 57 int n = annotation.length();
jjg@309 58 n += position_length(position);
jjg@309 59 return n;
jjg@309 60 }
jjg@309 61
jjg@309 62 public final Annotation annotation;
jjg@309 63 public final Position position;
jjg@309 64
jjg@309 65 private static Position read_position(ClassReader cr) throws IOException, Annotation.InvalidAnnotation {
jjg@309 66 // Copied from ClassReader
jjg@309 67 int tag = (byte)cr.readUnsignedByte(); // cast to introduce signedness
jjg@309 68 if (!TargetType.isValidTargetTypeValue(tag))
jjg@309 69 throw new Annotation.InvalidAnnotation("invalid type annotation target type value: " + tag);
jjg@309 70
jjg@309 71 TargetType type = TargetType.fromTargetTypeValue(tag);
jjg@309 72
jjg@309 73 Position position = new Position();
jjg@309 74 position.type = type;
jjg@309 75
jjg@309 76 switch (type) {
jjg@309 77 // type case
jjg@309 78 case TYPECAST:
jjg@309 79 case TYPECAST_GENERIC_OR_ARRAY:
jjg@309 80 // object creation
jjg@309 81 case INSTANCEOF:
jjg@309 82 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@309 83 // new expression
jjg@309 84 case NEW:
jjg@309 85 case NEW_GENERIC_OR_ARRAY:
jjg@309 86 position.offset = cr.readUnsignedShort();
jjg@309 87 break;
jjg@309 88 // local variable
jjg@309 89 case LOCAL_VARIABLE:
jjg@309 90 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@309 91 int table_length = cr.readUnsignedShort();
jjg@309 92 position.lvarOffset = new int[table_length];
jjg@309 93 position.lvarLength = new int[table_length];
jjg@309 94 position.lvarIndex = new int[table_length];
jjg@309 95 for (int i = 0; i < table_length; ++i) {
jjg@309 96 position.lvarOffset[i] = cr.readUnsignedShort();
jjg@309 97 position.lvarLength[i] = cr.readUnsignedShort();
jjg@309 98 position.lvarIndex[i] = cr.readUnsignedShort();
jjg@309 99 }
jjg@309 100 break;
jjg@309 101 // method receiver
jjg@309 102 case METHOD_RECEIVER:
jjg@309 103 // Do nothing
jjg@309 104 break;
jjg@309 105 // type parameters
jjg@309 106 case CLASS_TYPE_PARAMETER:
jjg@309 107 case METHOD_TYPE_PARAMETER:
jjg@309 108 position.parameter_index = cr.readUnsignedByte();
jjg@309 109 break;
jjg@309 110 // type parameter bounds
jjg@309 111 case CLASS_TYPE_PARAMETER_BOUND:
jjg@309 112 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 113 case METHOD_TYPE_PARAMETER_BOUND:
jjg@309 114 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 115 position.parameter_index = cr.readUnsignedByte();
jjg@309 116 position.bound_index = cr.readUnsignedByte();
jjg@309 117 break;
jjg@309 118 // wildcards
jjg@309 119 case WILDCARD_BOUND:
jjg@309 120 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@309 121 position.wildcard_position = read_position(cr);
jjg@309 122 break;
jjg@309 123 // Class extends and implements clauses
jjg@309 124 case CLASS_EXTENDS:
jjg@309 125 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@484 126 int in = cr.readUnsignedShort();
jjg@484 127 if (in == 0xFFFF)
jjg@484 128 in = -1;
jjg@484 129 position.type_index = in;
jjg@309 130 break;
jjg@309 131 // throws
jjg@309 132 case THROWS:
jjg@484 133 position.type_index = cr.readUnsignedShort();
jjg@309 134 break;
jjg@309 135 case CLASS_LITERAL:
jjg@309 136 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@309 137 position.offset = cr.readUnsignedShort();
jjg@309 138 break;
jjg@309 139 // method parameter: not specified
jjg@309 140 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 141 position.parameter_index = cr.readUnsignedByte();
jjg@309 142 break;
jjg@309 143 // method type argument: wasn't specified
jjg@309 144 case NEW_TYPE_ARGUMENT:
jjg@309 145 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 146 case METHOD_TYPE_ARGUMENT:
jjg@309 147 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 148 position.offset = cr.readUnsignedShort();
jjg@309 149 position.type_index = cr.readUnsignedByte();
jjg@309 150 break;
jjg@309 151 // We don't need to worry abut these
jjg@309 152 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 153 case FIELD_GENERIC_OR_ARRAY:
jjg@309 154 break;
jjg@309 155 case UNKNOWN:
jjg@309 156 break;
jjg@309 157 default:
jjg@309 158 throw new AssertionError("Cannot be here");
jjg@309 159 }
jjg@309 160
jjg@309 161 if (type.hasLocation()) {
jjg@309 162 int len = cr.readUnsignedShort();
jjg@309 163 List<Integer> loc = new ArrayList<Integer>(len);
jjg@309 164 for (int i = 0; i < len; i++)
jjg@309 165 loc.add(cr.readUnsignedByte());
jjg@309 166 position.location = loc;
jjg@309 167 }
jjg@309 168 return position;
jjg@309 169 }
jjg@309 170
jjg@309 171 private static int position_length(Position pos) {
jjg@309 172 int n = 0;
jjg@309 173 n += 1; // target_type
jjg@309 174 switch (pos.type) {
jjg@309 175 // type case
jjg@309 176 case TYPECAST:
jjg@309 177 case TYPECAST_GENERIC_OR_ARRAY:
jjg@309 178 // object creation
jjg@309 179 case INSTANCEOF:
jjg@309 180 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@309 181 // new expression
jjg@309 182 case NEW:
jjg@309 183 case NEW_GENERIC_OR_ARRAY:
jjg@309 184 n += 2;
jjg@309 185 break;
jjg@309 186 // local variable
jjg@309 187 case LOCAL_VARIABLE:
jjg@309 188 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@309 189 n += 2; // table_length;
jjg@309 190 int table_length = pos.lvarOffset.length;
jjg@309 191 n += 2 * table_length; // offset
jjg@309 192 n += 2 * table_length; // length;
jjg@309 193 n += 2 * table_length; // index
jjg@309 194 break;
jjg@309 195 // method receiver
jjg@309 196 case METHOD_RECEIVER:
jjg@309 197 // Do nothing
jjg@309 198 break;
jjg@309 199 // type parameters
jjg@309 200 case CLASS_TYPE_PARAMETER:
jjg@309 201 case METHOD_TYPE_PARAMETER:
jjg@309 202 n += 1; // parameter_index;
jjg@309 203 break;
jjg@309 204 // type parameter bounds
jjg@309 205 case CLASS_TYPE_PARAMETER_BOUND:
jjg@309 206 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 207 case METHOD_TYPE_PARAMETER_BOUND:
jjg@309 208 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 209 n += 1; // parameter_index
jjg@309 210 n += 1; // bound_index
jjg@309 211 break;
jjg@309 212 case WILDCARD_BOUND:
jjg@309 213 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@309 214 n += position_length(pos.wildcard_position);
jjg@309 215 break;
jjg@309 216 // Class extends and implements clauses
jjg@309 217 case CLASS_EXTENDS:
jjg@309 218 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@484 219 n += 2; // type_index
jjg@309 220 break;
jjg@309 221 // throws
jjg@309 222 case THROWS:
jjg@484 223 n += 2; // type_index
jjg@309 224 break;
jjg@309 225 case CLASS_LITERAL:
jjg@309 226 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@309 227 n += 1; // offset
jjg@309 228 break;
jjg@309 229 // method parameter: not specified
jjg@309 230 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 231 n += 1; // parameter_index
jjg@309 232 break;
jjg@309 233 // method type argument: wasn't specified
jjg@309 234 case NEW_TYPE_ARGUMENT:
jjg@309 235 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 236 case METHOD_TYPE_ARGUMENT:
jjg@309 237 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 238 n += 2; // offset
jjg@309 239 n += 1; // type index
jjg@309 240 break;
jjg@309 241 // We don't need to worry abut these
jjg@309 242 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 243 case FIELD_GENERIC_OR_ARRAY:
jjg@309 244 break;
jjg@309 245 case UNKNOWN:
jjg@309 246 break;
jjg@309 247 default:
jjg@309 248 }
jjg@309 249
jjg@309 250 if (pos.type.hasLocation()) {
jjg@309 251 n += 2; // length
jjg@309 252 n += 1 * pos.location.size(); // actual array size
jjg@309 253 }
jjg@309 254
jjg@309 255 return n;
jjg@309 256 }
jjg@309 257
jjg@309 258 // Code duplicated from com.sun.tools.javac.code.TypeAnnotations.Position
jjg@309 259 public static class Position {
jjg@309 260
jjg@309 261 public TargetType type = TargetType.UNKNOWN;
jjg@309 262
jjg@309 263 // For generic/array types.
jjg@309 264 public List<Integer> location = new ArrayList<Integer>();
jjg@309 265
jjg@309 266 // For typecasts, type tests, new (and locals, as start_pc).
jjg@309 267 public int offset = -1;
jjg@309 268
jjg@309 269 // For locals.
jjg@478 270 public int[] lvarOffset = null;
jjg@478 271 public int[] lvarLength = null;
jjg@478 272 public int[] lvarIndex = null;
jjg@309 273
jjg@309 274 // For type parameter bound
jjg@478 275 public int bound_index = Integer.MIN_VALUE;
jjg@309 276
jjg@309 277 // For type parameter and method parameter
jjg@478 278 public int parameter_index = Integer.MIN_VALUE;
jjg@309 279
jjg@309 280 // For class extends, implements, and throws classes
jjg@478 281 public int type_index = Integer.MIN_VALUE;
jjg@309 282
jjg@309 283 // For wildcards
jjg@309 284 public Position wildcard_position = null;
jjg@309 285
jjg@309 286 @Override
jjg@309 287 public String toString() {
jjg@309 288 StringBuilder sb = new StringBuilder();
jjg@309 289 sb.append('[');
jjg@309 290 sb.append(type);
jjg@309 291
jjg@309 292 switch (type) {
jjg@309 293 // type case
jjg@309 294 case TYPECAST:
jjg@309 295 case TYPECAST_GENERIC_OR_ARRAY:
jjg@309 296 // object creation
jjg@309 297 case INSTANCEOF:
jjg@309 298 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@309 299 // new expression
jjg@309 300 case NEW:
jjg@309 301 case NEW_GENERIC_OR_ARRAY:
jjg@309 302 case NEW_TYPE_ARGUMENT:
jjg@309 303 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 304 sb.append(", offset = ");
jjg@309 305 sb.append(offset);
jjg@309 306 break;
jjg@309 307 // local variable
jjg@309 308 case LOCAL_VARIABLE:
jjg@309 309 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@309 310 sb.append(", {");
jjg@309 311 for (int i = 0; i < lvarOffset.length; ++i) {
jjg@309 312 if (i != 0) sb.append("; ");
jjg@309 313 sb.append(", start_pc = ");
jjg@309 314 sb.append(lvarOffset[i]);
jjg@309 315 sb.append(", length = ");
jjg@309 316 sb.append(lvarLength[i]);
jjg@309 317 sb.append(", index = ");
jjg@309 318 sb.append(lvarIndex[i]);
jjg@309 319 }
jjg@309 320 sb.append("}");
jjg@309 321 break;
jjg@309 322 // method receiver
jjg@309 323 case METHOD_RECEIVER:
jjg@309 324 // Do nothing
jjg@309 325 break;
jjg@309 326 // type parameters
jjg@309 327 case CLASS_TYPE_PARAMETER:
jjg@309 328 case METHOD_TYPE_PARAMETER:
jjg@309 329 sb.append(", param_index = ");
jjg@309 330 sb.append(parameter_index);
jjg@309 331 break;
jjg@309 332 // type parameters bound
jjg@309 333 case CLASS_TYPE_PARAMETER_BOUND:
jjg@309 334 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 335 case METHOD_TYPE_PARAMETER_BOUND:
jjg@309 336 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 337 sb.append(", param_index = ");
jjg@309 338 sb.append(parameter_index);
jjg@309 339 sb.append(", bound_index = ");
jjg@309 340 sb.append(bound_index);
jjg@309 341 break;
jjg@309 342 // wildcard
jjg@309 343 case WILDCARD_BOUND:
jjg@309 344 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@309 345 sb.append(", wild_card = ");
jjg@309 346 sb.append(wildcard_position);
jjg@309 347 break;
jjg@309 348 // Class extends and implements clauses
jjg@309 349 case CLASS_EXTENDS:
jjg@309 350 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@309 351 sb.append(", type_index = ");
jjg@309 352 sb.append(type_index);
jjg@309 353 break;
jjg@309 354 // throws
jjg@309 355 case THROWS:
jjg@309 356 sb.append(", type_index = ");
jjg@309 357 sb.append(type_index);
jjg@309 358 break;
jjg@309 359 case CLASS_LITERAL:
jjg@328 360 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@309 361 sb.append(", offset = ");
jjg@309 362 sb.append(offset);
jjg@309 363 break;
jjg@309 364 // method parameter: not specified
jjg@309 365 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 366 sb.append(", param_index = ");
jjg@309 367 sb.append(parameter_index);
jjg@309 368 break;
jjg@309 369 // method type argument: wasn't specified
jjg@309 370 case METHOD_TYPE_ARGUMENT:
jjg@309 371 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 372 sb.append(", offset = ");
jjg@309 373 sb.append(offset);
jjg@309 374 sb.append(", type_index = ");
jjg@309 375 sb.append(type_index);
jjg@309 376 break;
jjg@309 377 // We don't need to worry abut these
jjg@309 378 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 379 case FIELD_GENERIC_OR_ARRAY:
jjg@309 380 break;
jjg@309 381 case UNKNOWN:
jjg@309 382 break;
jjg@309 383 default:
jjg@309 384 throw new AssertionError("unknown type: " + type);
jjg@309 385 }
jjg@309 386
jjg@309 387 // Append location data for generics/arrays.
jjg@309 388 if (type.hasLocation()) {
jjg@309 389 sb.append(", location = (");
jjg@309 390 sb.append(location);
jjg@309 391 sb.append(")");
jjg@309 392 }
jjg@309 393
jjg@309 394 sb.append(']');
jjg@309 395 return sb.toString();
jjg@309 396 }
jjg@309 397 }
jjg@309 398
jjg@309 399 // Code duplicated from com.sun.tools.javac.comp.TargetType
jjg@309 400 public enum TargetType {
jjg@309 401
jjg@309 402 /** For annotations on typecasts. */
jjg@309 403 TYPECAST(0x00),
jjg@309 404
jjg@309 405 /** For annotations on a type argument or nested array of a typecast. */
jjg@309 406 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
jjg@309 407
jjg@309 408 /** For annotations on type tests. */
jjg@309 409 INSTANCEOF(0x02),
jjg@309 410
jjg@309 411 /** For annotations on a type argument or nested array of a type test. */
jjg@309 412 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
jjg@309 413
jjg@309 414 /** For annotations on object creation expressions. */
jjg@309 415 NEW(0x04),
jjg@309 416
jjg@309 417 /**
jjg@309 418 * For annotations on a type argument or nested array of an object creation
jjg@309 419 * expression.
jjg@309 420 */
jjg@309 421 NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
jjg@309 422
jjg@309 423
jjg@309 424 /** For annotations on the method receiver. */
jjg@309 425 METHOD_RECEIVER(0x06),
jjg@309 426
jjg@309 427 // invalid location
jjg@309 428 // METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
jjg@309 429
jjg@309 430 /** For annotations on local variables. */
jjg@309 431 LOCAL_VARIABLE(0x08),
jjg@309 432
jjg@309 433 /** For annotations on a type argument or nested array of a local. */
jjg@309 434 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
jjg@309 435
jjg@309 436 // already handled by regular annotations
jjg@309 437 // METHOD_RETURN(0x0A),
jjg@309 438
jjg@309 439 /**
jjg@309 440 * For annotations on a type argument or nested array of a method return
jjg@309 441 * type.
jjg@309 442 */
jjg@309 443 METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
jjg@309 444
jjg@309 445 // already handled by regular annotations
jjg@309 446 // METHOD_PARAMETER(0x0C),
jjg@309 447
jjg@309 448 /** For annotations on a type argument or nested array of a method parameter. */
jjg@309 449 METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
jjg@309 450
jjg@309 451 // already handled by regular annotations
jjg@309 452 // FIELD(0x0E),
jjg@309 453
jjg@309 454 /** For annotations on a type argument or nested array of a field. */
jjg@309 455 FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
jjg@309 456
jjg@309 457 /** For annotations on a bound of a type parameter of a class. */
jjg@309 458 CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
jjg@309 459
jjg@309 460 /**
jjg@309 461 * For annotations on a type argument or nested array of a bound of a type
jjg@309 462 * parameter of a class.
jjg@309 463 */
jjg@309 464 CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
jjg@309 465
jjg@309 466 /** For annotations on a bound of a type parameter of a method. */
jjg@309 467 METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
jjg@309 468
jjg@309 469 /**
jjg@309 470 * For annotations on a type argument or nested array of a bound of a type
jjg@309 471 * parameter of a method.
jjg@309 472 */
jjg@309 473 METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
jjg@309 474
jjg@309 475 /** For annotations on the type of an "extends" or "implements" clause. */
jjg@309 476 CLASS_EXTENDS(0x14),
jjg@309 477
jjg@309 478 /** For annotations on the inner type of an "extends" or "implements" clause. */
jjg@309 479 CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
jjg@309 480
jjg@309 481 /** For annotations on a throws clause in a method declaration. */
jjg@309 482 THROWS(0x16),
jjg@309 483
jjg@309 484 // invalid location
jjg@309 485 // THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
jjg@309 486
jjg@309 487 /** For annotations in type arguments of object creation expressions. */
jjg@309 488 NEW_TYPE_ARGUMENT(0x18),
jjg@309 489 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
jjg@309 490
jjg@309 491 METHOD_TYPE_ARGUMENT(0x1A),
jjg@309 492 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
jjg@309 493
jjg@309 494 WILDCARD_BOUND(0x1C, HasBound),
jjg@309 495 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
jjg@309 496
jjg@309 497 CLASS_LITERAL(0x1E),
jjg@309 498 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
jjg@309 499
jjg@309 500 METHOD_TYPE_PARAMETER(0x20, HasParameter),
jjg@309 501
jjg@309 502 // invalid location
jjg@309 503 // METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
jjg@309 504
jjg@309 505 CLASS_TYPE_PARAMETER(0x22, HasParameter),
jjg@309 506
jjg@309 507 // invalid location
jjg@309 508 // CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
jjg@309 509
jjg@309 510 /** For annotations with an unknown target. */
jjg@309 511 UNKNOWN(-1);
jjg@309 512
jjg@309 513 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
jjg@309 514
jjg@309 515 private final int targetTypeValue;
jjg@309 516 private Set<TargetAttribute> flags;
jjg@309 517
jjg@309 518 TargetType(int targetTypeValue, TargetAttribute... attrs) {
jjg@309 519 if (targetTypeValue < Byte.MIN_VALUE
jjg@309 520 || targetTypeValue > Byte.MAX_VALUE)
jjg@309 521 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
jjg@309 522 this.targetTypeValue = (byte)targetTypeValue;
jjg@309 523 this.flags = EnumSet.noneOf(TargetAttribute.class);
jjg@309 524 for (TargetAttribute attr : attrs)
jjg@309 525 this.flags.add(attr);
jjg@309 526 }
jjg@309 527
jjg@309 528 /**
jjg@309 529 * Returns whether or not this TargetType represents an annotation whose
jjg@309 530 * target is an inner type of a generic or array type.
jjg@309 531 *
jjg@309 532 * @return true if this TargetType represents an annotation on an inner
jjg@309 533 * type, false otherwise
jjg@309 534 */
jjg@309 535 public boolean hasLocation() {
jjg@309 536 return flags.contains(HasLocation);
jjg@309 537 }
jjg@309 538
jjg@309 539 public TargetType getGenericComplement() {
jjg@309 540 if (hasLocation())
jjg@309 541 return this;
jjg@309 542 else
jjg@309 543 return fromTargetTypeValue(targetTypeValue() + 1);
jjg@309 544 }
jjg@309 545
jjg@309 546 /**
jjg@309 547 * Returns whether or not this TargetType represents an annotation whose
jjg@309 548 * target has a parameter index.
jjg@309 549 *
jjg@309 550 * @return true if this TargetType has a parameter index,
jjg@309 551 * false otherwise
jjg@309 552 */
jjg@309 553 public boolean hasParameter() {
jjg@309 554 return flags.contains(HasParameter);
jjg@309 555 }
jjg@309 556
jjg@309 557 /**
jjg@309 558 * Returns whether or not this TargetType represents an annotation whose
jjg@309 559 * target is a type parameter bound.
jjg@309 560 *
jjg@309 561 * @return true if this TargetType represents an type parameter bound
jjg@309 562 * annotation, false otherwise
jjg@309 563 */
jjg@309 564 public boolean hasBound() {
jjg@309 565 return flags.contains(HasBound);
jjg@309 566 }
jjg@309 567
jjg@309 568 public int targetTypeValue() {
jjg@309 569 return this.targetTypeValue;
jjg@309 570 }
jjg@309 571
jjg@309 572 private static TargetType[] targets = null;
jjg@309 573
jjg@309 574 private static TargetType[] buildTargets() {
jjg@309 575 TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
jjg@309 576 TargetType[] alltargets = values();
jjg@309 577 for (TargetType target : alltargets)
jjg@309 578 if (target.targetTypeValue >= 0)
jjg@309 579 targets[target.targetTypeValue] = target;
jjg@309 580 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i)
jjg@309 581 if (targets[i] == null)
jjg@309 582 targets[i] = UNKNOWN;
jjg@309 583 return targets;
jjg@309 584 }
jjg@309 585
jjg@309 586 public static boolean isValidTargetTypeValue(int tag) {
jjg@309 587 if (targets == null)
jjg@309 588 targets = buildTargets();
jjg@309 589
jjg@309 590 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 591 return true;
jjg@309 592
jjg@309 593 return (tag >= 0 && tag < targets.length);
jjg@309 594 }
jjg@309 595
jjg@309 596 public static TargetType fromTargetTypeValue(int tag) {
jjg@309 597 if (targets == null)
jjg@309 598 targets = buildTargets();
jjg@309 599
jjg@309 600 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 601 return UNKNOWN;
jjg@309 602
jjg@309 603 if (tag < 0 || tag >= targets.length)
jjg@309 604 throw new IllegalArgumentException("Unknown TargetType: " + tag);
jjg@309 605 return targets[tag];
jjg@309 606 }
jjg@309 607 }
jjg@309 608
jjg@309 609 static enum TargetAttribute {
jjg@309 610 HasLocation, HasParameter, HasBound;
jjg@309 611 }
jjg@309 612 }

mercurial