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

Wed, 20 Jan 2010 16:12:26 -0800

author
jjg
date
Wed, 20 Jan 2010 16:12:26 -0800
changeset 478
0eaf89e08564
parent 338
777a3efad0d5
child 484
732510cc3538
permissions
-rw-r--r--

6918127: improve handling of TypeAnnotationPosition fields
Reviewed-by: jjg, darcy
Contributed-by: mali@csail.mit.edu, mernst@cs.washington.edu

jjg@309 1 /*
jjg@309 2 * Copyright 2009 Sun Microsystems, Inc. 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
jjg@309 7 * published by the Free Software Foundation. Sun designates this
jjg@309 8 * particular file as subject to the "Classpath" exception as provided
jjg@309 9 * by Sun 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 *
jjg@309 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@309 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@309 23 * have any 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@309 39 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@309 40 * 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@309 126 position.type_index = cr.readUnsignedByte();
jjg@309 127 break;
jjg@309 128 // throws
jjg@309 129 case THROWS:
jjg@309 130 position.type_index = cr.readUnsignedByte();
jjg@309 131 break;
jjg@309 132 case CLASS_LITERAL:
jjg@309 133 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@309 134 position.offset = cr.readUnsignedShort();
jjg@309 135 break;
jjg@309 136 // method parameter: not specified
jjg@309 137 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 138 position.parameter_index = cr.readUnsignedByte();
jjg@309 139 break;
jjg@309 140 // method type argument: wasn't specified
jjg@309 141 case NEW_TYPE_ARGUMENT:
jjg@309 142 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 143 case METHOD_TYPE_ARGUMENT:
jjg@309 144 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 145 position.offset = cr.readUnsignedShort();
jjg@309 146 position.type_index = cr.readUnsignedByte();
jjg@309 147 break;
jjg@309 148 // We don't need to worry abut these
jjg@309 149 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 150 case FIELD_GENERIC_OR_ARRAY:
jjg@309 151 break;
jjg@309 152 case UNKNOWN:
jjg@309 153 break;
jjg@309 154 default:
jjg@309 155 throw new AssertionError("Cannot be here");
jjg@309 156 }
jjg@309 157
jjg@309 158 if (type.hasLocation()) {
jjg@309 159 int len = cr.readUnsignedShort();
jjg@309 160 List<Integer> loc = new ArrayList<Integer>(len);
jjg@309 161 for (int i = 0; i < len; i++)
jjg@309 162 loc.add(cr.readUnsignedByte());
jjg@309 163 position.location = loc;
jjg@309 164 }
jjg@309 165 return position;
jjg@309 166 }
jjg@309 167
jjg@309 168 private static int position_length(Position pos) {
jjg@309 169 int n = 0;
jjg@309 170 n += 1; // target_type
jjg@309 171 switch (pos.type) {
jjg@309 172 // type case
jjg@309 173 case TYPECAST:
jjg@309 174 case TYPECAST_GENERIC_OR_ARRAY:
jjg@309 175 // object creation
jjg@309 176 case INSTANCEOF:
jjg@309 177 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@309 178 // new expression
jjg@309 179 case NEW:
jjg@309 180 case NEW_GENERIC_OR_ARRAY:
jjg@309 181 n += 2;
jjg@309 182 break;
jjg@309 183 // local variable
jjg@309 184 case LOCAL_VARIABLE:
jjg@309 185 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@309 186 n += 2; // table_length;
jjg@309 187 int table_length = pos.lvarOffset.length;
jjg@309 188 n += 2 * table_length; // offset
jjg@309 189 n += 2 * table_length; // length;
jjg@309 190 n += 2 * table_length; // index
jjg@309 191 break;
jjg@309 192 // method receiver
jjg@309 193 case METHOD_RECEIVER:
jjg@309 194 // Do nothing
jjg@309 195 break;
jjg@309 196 // type parameters
jjg@309 197 case CLASS_TYPE_PARAMETER:
jjg@309 198 case METHOD_TYPE_PARAMETER:
jjg@309 199 n += 1; // parameter_index;
jjg@309 200 break;
jjg@309 201 // type parameter bounds
jjg@309 202 case CLASS_TYPE_PARAMETER_BOUND:
jjg@309 203 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 204 case METHOD_TYPE_PARAMETER_BOUND:
jjg@309 205 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 206 n += 1; // parameter_index
jjg@309 207 n += 1; // bound_index
jjg@309 208 break;
jjg@309 209 case WILDCARD_BOUND:
jjg@309 210 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@309 211 n += position_length(pos.wildcard_position);
jjg@309 212 break;
jjg@309 213 // Class extends and implements clauses
jjg@309 214 case CLASS_EXTENDS:
jjg@309 215 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@309 216 n += 1; // type_index
jjg@309 217 break;
jjg@309 218 // throws
jjg@309 219 case THROWS:
jjg@309 220 n += 1; // type_index
jjg@309 221 break;
jjg@309 222 case CLASS_LITERAL:
jjg@309 223 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@309 224 n += 1; // offset
jjg@309 225 break;
jjg@309 226 // method parameter: not specified
jjg@309 227 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 228 n += 1; // parameter_index
jjg@309 229 break;
jjg@309 230 // method type argument: wasn't specified
jjg@309 231 case NEW_TYPE_ARGUMENT:
jjg@309 232 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 233 case METHOD_TYPE_ARGUMENT:
jjg@309 234 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 235 n += 2; // offset
jjg@309 236 n += 1; // type index
jjg@309 237 break;
jjg@309 238 // We don't need to worry abut these
jjg@309 239 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 240 case FIELD_GENERIC_OR_ARRAY:
jjg@309 241 break;
jjg@309 242 case UNKNOWN:
jjg@309 243 break;
jjg@309 244 default:
jjg@309 245 }
jjg@309 246
jjg@309 247 if (pos.type.hasLocation()) {
jjg@309 248 n += 2; // length
jjg@309 249 n += 1 * pos.location.size(); // actual array size
jjg@309 250 }
jjg@309 251
jjg@309 252 return n;
jjg@309 253 }
jjg@309 254
jjg@309 255 // Code duplicated from com.sun.tools.javac.code.TypeAnnotations.Position
jjg@309 256 public static class Position {
jjg@309 257
jjg@309 258 public TargetType type = TargetType.UNKNOWN;
jjg@309 259
jjg@309 260 // For generic/array types.
jjg@309 261 public List<Integer> location = new ArrayList<Integer>();
jjg@309 262
jjg@309 263 // For typecasts, type tests, new (and locals, as start_pc).
jjg@309 264 public int offset = -1;
jjg@309 265
jjg@309 266 // For locals.
jjg@478 267 public int[] lvarOffset = null;
jjg@478 268 public int[] lvarLength = null;
jjg@478 269 public int[] lvarIndex = null;
jjg@309 270
jjg@309 271 // For type parameter bound
jjg@478 272 public int bound_index = Integer.MIN_VALUE;
jjg@309 273
jjg@309 274 // For type parameter and method parameter
jjg@478 275 public int parameter_index = Integer.MIN_VALUE;
jjg@309 276
jjg@309 277 // For class extends, implements, and throws classes
jjg@478 278 public int type_index = Integer.MIN_VALUE;
jjg@309 279
jjg@309 280 // For wildcards
jjg@309 281 public Position wildcard_position = null;
jjg@309 282
jjg@309 283 @Override
jjg@309 284 public String toString() {
jjg@309 285 StringBuilder sb = new StringBuilder();
jjg@309 286 sb.append('[');
jjg@309 287 sb.append(type);
jjg@309 288
jjg@309 289 switch (type) {
jjg@309 290 // type case
jjg@309 291 case TYPECAST:
jjg@309 292 case TYPECAST_GENERIC_OR_ARRAY:
jjg@309 293 // object creation
jjg@309 294 case INSTANCEOF:
jjg@309 295 case INSTANCEOF_GENERIC_OR_ARRAY:
jjg@309 296 // new expression
jjg@309 297 case NEW:
jjg@309 298 case NEW_GENERIC_OR_ARRAY:
jjg@309 299 case NEW_TYPE_ARGUMENT:
jjg@309 300 case NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 301 sb.append(", offset = ");
jjg@309 302 sb.append(offset);
jjg@309 303 break;
jjg@309 304 // local variable
jjg@309 305 case LOCAL_VARIABLE:
jjg@309 306 case LOCAL_VARIABLE_GENERIC_OR_ARRAY:
jjg@309 307 sb.append(", {");
jjg@309 308 for (int i = 0; i < lvarOffset.length; ++i) {
jjg@309 309 if (i != 0) sb.append("; ");
jjg@309 310 sb.append(", start_pc = ");
jjg@309 311 sb.append(lvarOffset[i]);
jjg@309 312 sb.append(", length = ");
jjg@309 313 sb.append(lvarLength[i]);
jjg@309 314 sb.append(", index = ");
jjg@309 315 sb.append(lvarIndex[i]);
jjg@309 316 }
jjg@309 317 sb.append("}");
jjg@309 318 break;
jjg@309 319 // method receiver
jjg@309 320 case METHOD_RECEIVER:
jjg@309 321 // Do nothing
jjg@309 322 break;
jjg@309 323 // type parameters
jjg@309 324 case CLASS_TYPE_PARAMETER:
jjg@309 325 case METHOD_TYPE_PARAMETER:
jjg@309 326 sb.append(", param_index = ");
jjg@309 327 sb.append(parameter_index);
jjg@309 328 break;
jjg@309 329 // type parameters bound
jjg@309 330 case CLASS_TYPE_PARAMETER_BOUND:
jjg@309 331 case CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 332 case METHOD_TYPE_PARAMETER_BOUND:
jjg@309 333 case METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY:
jjg@309 334 sb.append(", param_index = ");
jjg@309 335 sb.append(parameter_index);
jjg@309 336 sb.append(", bound_index = ");
jjg@309 337 sb.append(bound_index);
jjg@309 338 break;
jjg@309 339 // wildcard
jjg@309 340 case WILDCARD_BOUND:
jjg@309 341 case WILDCARD_BOUND_GENERIC_OR_ARRAY:
jjg@309 342 sb.append(", wild_card = ");
jjg@309 343 sb.append(wildcard_position);
jjg@309 344 break;
jjg@309 345 // Class extends and implements clauses
jjg@309 346 case CLASS_EXTENDS:
jjg@309 347 case CLASS_EXTENDS_GENERIC_OR_ARRAY:
jjg@309 348 sb.append(", type_index = ");
jjg@309 349 sb.append(type_index);
jjg@309 350 break;
jjg@309 351 // throws
jjg@309 352 case THROWS:
jjg@309 353 sb.append(", type_index = ");
jjg@309 354 sb.append(type_index);
jjg@309 355 break;
jjg@309 356 case CLASS_LITERAL:
jjg@328 357 case CLASS_LITERAL_GENERIC_OR_ARRAY:
jjg@309 358 sb.append(", offset = ");
jjg@309 359 sb.append(offset);
jjg@309 360 break;
jjg@309 361 // method parameter: not specified
jjg@309 362 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 363 sb.append(", param_index = ");
jjg@309 364 sb.append(parameter_index);
jjg@309 365 break;
jjg@309 366 // method type argument: wasn't specified
jjg@309 367 case METHOD_TYPE_ARGUMENT:
jjg@309 368 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 369 sb.append(", offset = ");
jjg@309 370 sb.append(offset);
jjg@309 371 sb.append(", type_index = ");
jjg@309 372 sb.append(type_index);
jjg@309 373 break;
jjg@309 374 // We don't need to worry abut these
jjg@309 375 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 376 case FIELD_GENERIC_OR_ARRAY:
jjg@309 377 break;
jjg@309 378 case UNKNOWN:
jjg@309 379 break;
jjg@309 380 default:
jjg@309 381 throw new AssertionError("unknown type: " + type);
jjg@309 382 }
jjg@309 383
jjg@309 384 // Append location data for generics/arrays.
jjg@309 385 if (type.hasLocation()) {
jjg@309 386 sb.append(", location = (");
jjg@309 387 sb.append(location);
jjg@309 388 sb.append(")");
jjg@309 389 }
jjg@309 390
jjg@309 391 sb.append(']');
jjg@309 392 return sb.toString();
jjg@309 393 }
jjg@309 394 }
jjg@309 395
jjg@309 396 // Code duplicated from com.sun.tools.javac.comp.TargetType
jjg@309 397 public enum TargetType {
jjg@309 398
jjg@309 399 /** For annotations on typecasts. */
jjg@309 400 TYPECAST(0x00),
jjg@309 401
jjg@309 402 /** For annotations on a type argument or nested array of a typecast. */
jjg@309 403 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
jjg@309 404
jjg@309 405 /** For annotations on type tests. */
jjg@309 406 INSTANCEOF(0x02),
jjg@309 407
jjg@309 408 /** For annotations on a type argument or nested array of a type test. */
jjg@309 409 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
jjg@309 410
jjg@309 411 /** For annotations on object creation expressions. */
jjg@309 412 NEW(0x04),
jjg@309 413
jjg@309 414 /**
jjg@309 415 * For annotations on a type argument or nested array of an object creation
jjg@309 416 * expression.
jjg@309 417 */
jjg@309 418 NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
jjg@309 419
jjg@309 420
jjg@309 421 /** For annotations on the method receiver. */
jjg@309 422 METHOD_RECEIVER(0x06),
jjg@309 423
jjg@309 424 // invalid location
jjg@309 425 // METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
jjg@309 426
jjg@309 427 /** For annotations on local variables. */
jjg@309 428 LOCAL_VARIABLE(0x08),
jjg@309 429
jjg@309 430 /** For annotations on a type argument or nested array of a local. */
jjg@309 431 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
jjg@309 432
jjg@309 433 // already handled by regular annotations
jjg@309 434 // METHOD_RETURN(0x0A),
jjg@309 435
jjg@309 436 /**
jjg@309 437 * For annotations on a type argument or nested array of a method return
jjg@309 438 * type.
jjg@309 439 */
jjg@309 440 METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
jjg@309 441
jjg@309 442 // already handled by regular annotations
jjg@309 443 // METHOD_PARAMETER(0x0C),
jjg@309 444
jjg@309 445 /** For annotations on a type argument or nested array of a method parameter. */
jjg@309 446 METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
jjg@309 447
jjg@309 448 // already handled by regular annotations
jjg@309 449 // FIELD(0x0E),
jjg@309 450
jjg@309 451 /** For annotations on a type argument or nested array of a field. */
jjg@309 452 FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
jjg@309 453
jjg@309 454 /** For annotations on a bound of a type parameter of a class. */
jjg@309 455 CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
jjg@309 456
jjg@309 457 /**
jjg@309 458 * For annotations on a type argument or nested array of a bound of a type
jjg@309 459 * parameter of a class.
jjg@309 460 */
jjg@309 461 CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
jjg@309 462
jjg@309 463 /** For annotations on a bound of a type parameter of a method. */
jjg@309 464 METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
jjg@309 465
jjg@309 466 /**
jjg@309 467 * For annotations on a type argument or nested array of a bound of a type
jjg@309 468 * parameter of a method.
jjg@309 469 */
jjg@309 470 METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
jjg@309 471
jjg@309 472 /** For annotations on the type of an "extends" or "implements" clause. */
jjg@309 473 CLASS_EXTENDS(0x14),
jjg@309 474
jjg@309 475 /** For annotations on the inner type of an "extends" or "implements" clause. */
jjg@309 476 CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
jjg@309 477
jjg@309 478 /** For annotations on a throws clause in a method declaration. */
jjg@309 479 THROWS(0x16),
jjg@309 480
jjg@309 481 // invalid location
jjg@309 482 // THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
jjg@309 483
jjg@309 484 /** For annotations in type arguments of object creation expressions. */
jjg@309 485 NEW_TYPE_ARGUMENT(0x18),
jjg@309 486 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
jjg@309 487
jjg@309 488 METHOD_TYPE_ARGUMENT(0x1A),
jjg@309 489 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
jjg@309 490
jjg@309 491 WILDCARD_BOUND(0x1C, HasBound),
jjg@309 492 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
jjg@309 493
jjg@309 494 CLASS_LITERAL(0x1E),
jjg@309 495 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
jjg@309 496
jjg@309 497 METHOD_TYPE_PARAMETER(0x20, HasParameter),
jjg@309 498
jjg@309 499 // invalid location
jjg@309 500 // METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
jjg@309 501
jjg@309 502 CLASS_TYPE_PARAMETER(0x22, HasParameter),
jjg@309 503
jjg@309 504 // invalid location
jjg@309 505 // CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
jjg@309 506
jjg@309 507 /** For annotations with an unknown target. */
jjg@309 508 UNKNOWN(-1);
jjg@309 509
jjg@309 510 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
jjg@309 511
jjg@309 512 private final int targetTypeValue;
jjg@309 513 private Set<TargetAttribute> flags;
jjg@309 514
jjg@309 515 TargetType(int targetTypeValue, TargetAttribute... attrs) {
jjg@309 516 if (targetTypeValue < Byte.MIN_VALUE
jjg@309 517 || targetTypeValue > Byte.MAX_VALUE)
jjg@309 518 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
jjg@309 519 this.targetTypeValue = (byte)targetTypeValue;
jjg@309 520 this.flags = EnumSet.noneOf(TargetAttribute.class);
jjg@309 521 for (TargetAttribute attr : attrs)
jjg@309 522 this.flags.add(attr);
jjg@309 523 }
jjg@309 524
jjg@309 525 /**
jjg@309 526 * Returns whether or not this TargetType represents an annotation whose
jjg@309 527 * target is an inner type of a generic or array type.
jjg@309 528 *
jjg@309 529 * @return true if this TargetType represents an annotation on an inner
jjg@309 530 * type, false otherwise
jjg@309 531 */
jjg@309 532 public boolean hasLocation() {
jjg@309 533 return flags.contains(HasLocation);
jjg@309 534 }
jjg@309 535
jjg@309 536 public TargetType getGenericComplement() {
jjg@309 537 if (hasLocation())
jjg@309 538 return this;
jjg@309 539 else
jjg@309 540 return fromTargetTypeValue(targetTypeValue() + 1);
jjg@309 541 }
jjg@309 542
jjg@309 543 /**
jjg@309 544 * Returns whether or not this TargetType represents an annotation whose
jjg@309 545 * target has a parameter index.
jjg@309 546 *
jjg@309 547 * @return true if this TargetType has a parameter index,
jjg@309 548 * false otherwise
jjg@309 549 */
jjg@309 550 public boolean hasParameter() {
jjg@309 551 return flags.contains(HasParameter);
jjg@309 552 }
jjg@309 553
jjg@309 554 /**
jjg@309 555 * Returns whether or not this TargetType represents an annotation whose
jjg@309 556 * target is a type parameter bound.
jjg@309 557 *
jjg@309 558 * @return true if this TargetType represents an type parameter bound
jjg@309 559 * annotation, false otherwise
jjg@309 560 */
jjg@309 561 public boolean hasBound() {
jjg@309 562 return flags.contains(HasBound);
jjg@309 563 }
jjg@309 564
jjg@309 565 public int targetTypeValue() {
jjg@309 566 return this.targetTypeValue;
jjg@309 567 }
jjg@309 568
jjg@309 569 private static TargetType[] targets = null;
jjg@309 570
jjg@309 571 private static TargetType[] buildTargets() {
jjg@309 572 TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
jjg@309 573 TargetType[] alltargets = values();
jjg@309 574 for (TargetType target : alltargets)
jjg@309 575 if (target.targetTypeValue >= 0)
jjg@309 576 targets[target.targetTypeValue] = target;
jjg@309 577 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i)
jjg@309 578 if (targets[i] == null)
jjg@309 579 targets[i] = UNKNOWN;
jjg@309 580 return targets;
jjg@309 581 }
jjg@309 582
jjg@309 583 public static boolean isValidTargetTypeValue(int tag) {
jjg@309 584 if (targets == null)
jjg@309 585 targets = buildTargets();
jjg@309 586
jjg@309 587 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 588 return true;
jjg@309 589
jjg@309 590 return (tag >= 0 && tag < targets.length);
jjg@309 591 }
jjg@309 592
jjg@309 593 public static TargetType fromTargetTypeValue(int tag) {
jjg@309 594 if (targets == null)
jjg@309 595 targets = buildTargets();
jjg@309 596
jjg@309 597 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 598 return UNKNOWN;
jjg@309 599
jjg@309 600 if (tag < 0 || tag >= targets.length)
jjg@309 601 throw new IllegalArgumentException("Unknown TargetType: " + tag);
jjg@309 602 return targets[tag];
jjg@309 603 }
jjg@309 604 }
jjg@309 605
jjg@309 606 static enum TargetAttribute {
jjg@309 607 HasLocation, HasParameter, HasBound;
jjg@309 608 }
jjg@309 609 }

mercurial