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

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
child 328
99b7a25185aa
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.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 // Tree position.
jjg@309 264 public int pos = -1;
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@309 270 public int[] lvarOffset = new int[] { -1 };
jjg@309 271 public int[] lvarLength = new int[] { -1 };
jjg@309 272 public int[] lvarIndex = new int[] { -1 };
jjg@309 273
jjg@309 274 // For type parameter bound
jjg@309 275 public int bound_index = -1;
jjg@309 276
jjg@309 277 // For type parameter and method parameter
jjg@309 278 public int parameter_index = -1;
jjg@309 279
jjg@309 280 // For class extends, implements, and throws classes
jjg@309 281 public int type_index = -2;
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@309 360 sb.append(", offset = ");
jjg@309 361 sb.append(offset);
jjg@309 362 break;
jjg@309 363 // method parameter: not specified
jjg@309 364 case METHOD_PARAMETER_GENERIC_OR_ARRAY:
jjg@309 365 sb.append(", param_index = ");
jjg@309 366 sb.append(parameter_index);
jjg@309 367 break;
jjg@309 368 // method type argument: wasn't specified
jjg@309 369 case METHOD_TYPE_ARGUMENT:
jjg@309 370 case METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY:
jjg@309 371 sb.append(", offset = ");
jjg@309 372 sb.append(offset);
jjg@309 373 sb.append(", type_index = ");
jjg@309 374 sb.append(type_index);
jjg@309 375 break;
jjg@309 376 // We don't need to worry abut these
jjg@309 377 case METHOD_RETURN_GENERIC_OR_ARRAY:
jjg@309 378 case FIELD_GENERIC_OR_ARRAY:
jjg@309 379 break;
jjg@309 380 case UNKNOWN:
jjg@309 381 break;
jjg@309 382 default:
jjg@309 383 throw new AssertionError("unknown type: " + type);
jjg@309 384 }
jjg@309 385
jjg@309 386 // Append location data for generics/arrays.
jjg@309 387 if (type.hasLocation()) {
jjg@309 388 sb.append(", location = (");
jjg@309 389 sb.append(location);
jjg@309 390 sb.append(")");
jjg@309 391 }
jjg@309 392
jjg@309 393 sb.append(", pos = ");
jjg@309 394 sb.append(pos);
jjg@309 395
jjg@309 396 sb.append(']');
jjg@309 397 return sb.toString();
jjg@309 398 }
jjg@309 399 }
jjg@309 400
jjg@309 401 // Code duplicated from com.sun.tools.javac.comp.TargetType
jjg@309 402 public enum TargetType {
jjg@309 403
jjg@309 404 /** For annotations on typecasts. */
jjg@309 405 TYPECAST(0x00),
jjg@309 406
jjg@309 407 /** For annotations on a type argument or nested array of a typecast. */
jjg@309 408 TYPECAST_GENERIC_OR_ARRAY(0x01, HasLocation),
jjg@309 409
jjg@309 410 /** For annotations on type tests. */
jjg@309 411 INSTANCEOF(0x02),
jjg@309 412
jjg@309 413 /** For annotations on a type argument or nested array of a type test. */
jjg@309 414 INSTANCEOF_GENERIC_OR_ARRAY(0x03, HasLocation),
jjg@309 415
jjg@309 416 /** For annotations on object creation expressions. */
jjg@309 417 NEW(0x04),
jjg@309 418
jjg@309 419 /**
jjg@309 420 * For annotations on a type argument or nested array of an object creation
jjg@309 421 * expression.
jjg@309 422 */
jjg@309 423 NEW_GENERIC_OR_ARRAY(0x05, HasLocation),
jjg@309 424
jjg@309 425
jjg@309 426 /** For annotations on the method receiver. */
jjg@309 427 METHOD_RECEIVER(0x06),
jjg@309 428
jjg@309 429 // invalid location
jjg@309 430 // METHOD_RECEIVER_GENERIC_OR_ARRAY(0x07, HasLocation),
jjg@309 431
jjg@309 432 /** For annotations on local variables. */
jjg@309 433 LOCAL_VARIABLE(0x08),
jjg@309 434
jjg@309 435 /** For annotations on a type argument or nested array of a local. */
jjg@309 436 LOCAL_VARIABLE_GENERIC_OR_ARRAY(0x09, HasLocation),
jjg@309 437
jjg@309 438 // already handled by regular annotations
jjg@309 439 // METHOD_RETURN(0x0A),
jjg@309 440
jjg@309 441 /**
jjg@309 442 * For annotations on a type argument or nested array of a method return
jjg@309 443 * type.
jjg@309 444 */
jjg@309 445 METHOD_RETURN_GENERIC_OR_ARRAY(0x0B, HasLocation),
jjg@309 446
jjg@309 447 // already handled by regular annotations
jjg@309 448 // METHOD_PARAMETER(0x0C),
jjg@309 449
jjg@309 450 /** For annotations on a type argument or nested array of a method parameter. */
jjg@309 451 METHOD_PARAMETER_GENERIC_OR_ARRAY(0x0D, HasLocation),
jjg@309 452
jjg@309 453 // already handled by regular annotations
jjg@309 454 // FIELD(0x0E),
jjg@309 455
jjg@309 456 /** For annotations on a type argument or nested array of a field. */
jjg@309 457 FIELD_GENERIC_OR_ARRAY(0x0F, HasLocation),
jjg@309 458
jjg@309 459 /** For annotations on a bound of a type parameter of a class. */
jjg@309 460 CLASS_TYPE_PARAMETER_BOUND(0x10, HasBound, HasParameter),
jjg@309 461
jjg@309 462 /**
jjg@309 463 * For annotations on a type argument or nested array of a bound of a type
jjg@309 464 * parameter of a class.
jjg@309 465 */
jjg@309 466 CLASS_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x11, HasBound, HasLocation, HasParameter),
jjg@309 467
jjg@309 468 /** For annotations on a bound of a type parameter of a method. */
jjg@309 469 METHOD_TYPE_PARAMETER_BOUND(0x12, HasBound, HasParameter),
jjg@309 470
jjg@309 471 /**
jjg@309 472 * For annotations on a type argument or nested array of a bound of a type
jjg@309 473 * parameter of a method.
jjg@309 474 */
jjg@309 475 METHOD_TYPE_PARAMETER_BOUND_GENERIC_OR_ARRAY(0x13, HasBound, HasLocation, HasParameter),
jjg@309 476
jjg@309 477 /** For annotations on the type of an "extends" or "implements" clause. */
jjg@309 478 CLASS_EXTENDS(0x14),
jjg@309 479
jjg@309 480 /** For annotations on the inner type of an "extends" or "implements" clause. */
jjg@309 481 CLASS_EXTENDS_GENERIC_OR_ARRAY(0x15, HasLocation),
jjg@309 482
jjg@309 483 /** For annotations on a throws clause in a method declaration. */
jjg@309 484 THROWS(0x16),
jjg@309 485
jjg@309 486 // invalid location
jjg@309 487 // THROWS_GENERIC_OR_ARRAY(0x17, HasLocation),
jjg@309 488
jjg@309 489 /** For annotations in type arguments of object creation expressions. */
jjg@309 490 NEW_TYPE_ARGUMENT(0x18),
jjg@309 491 NEW_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x19, HasLocation),
jjg@309 492
jjg@309 493 METHOD_TYPE_ARGUMENT(0x1A),
jjg@309 494 METHOD_TYPE_ARGUMENT_GENERIC_OR_ARRAY(0x1B, HasLocation),
jjg@309 495
jjg@309 496 WILDCARD_BOUND(0x1C, HasBound),
jjg@309 497 WILDCARD_BOUND_GENERIC_OR_ARRAY(0x1D, HasBound, HasLocation),
jjg@309 498
jjg@309 499 CLASS_LITERAL(0x1E),
jjg@309 500 CLASS_LITERAL_GENERIC_OR_ARRAY(0x1F, HasLocation),
jjg@309 501
jjg@309 502 METHOD_TYPE_PARAMETER(0x20, HasParameter),
jjg@309 503
jjg@309 504 // invalid location
jjg@309 505 // METHOD_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x21, HasLocation, HasParameter),
jjg@309 506
jjg@309 507 CLASS_TYPE_PARAMETER(0x22, HasParameter),
jjg@309 508
jjg@309 509 // invalid location
jjg@309 510 // CLASS_TYPE_PARAMETER_GENERIC_OR_ARRAY(0x23, HasLocation, HasParameter),
jjg@309 511
jjg@309 512 /** For annotations with an unknown target. */
jjg@309 513 UNKNOWN(-1);
jjg@309 514
jjg@309 515 static final int MAXIMUM_TARGET_TYPE_VALUE = 0x22;
jjg@309 516
jjg@309 517 private final int targetTypeValue;
jjg@309 518 private Set<TargetAttribute> flags;
jjg@309 519
jjg@309 520 TargetType(int targetTypeValue, TargetAttribute... attrs) {
jjg@309 521 if (targetTypeValue < Byte.MIN_VALUE
jjg@309 522 || targetTypeValue > Byte.MAX_VALUE)
jjg@309 523 throw new AssertionError("attribute type value needs to be a byte: " + targetTypeValue);
jjg@309 524 this.targetTypeValue = (byte)targetTypeValue;
jjg@309 525 this.flags = EnumSet.noneOf(TargetAttribute.class);
jjg@309 526 for (TargetAttribute attr : attrs)
jjg@309 527 this.flags.add(attr);
jjg@309 528 }
jjg@309 529
jjg@309 530 /**
jjg@309 531 * Returns whether or not this TargetType represents an annotation whose
jjg@309 532 * target is an inner type of a generic or array type.
jjg@309 533 *
jjg@309 534 * @return true if this TargetType represents an annotation on an inner
jjg@309 535 * type, false otherwise
jjg@309 536 */
jjg@309 537 public boolean hasLocation() {
jjg@309 538 return flags.contains(HasLocation);
jjg@309 539 }
jjg@309 540
jjg@309 541 public TargetType getGenericComplement() {
jjg@309 542 if (hasLocation())
jjg@309 543 return this;
jjg@309 544 else
jjg@309 545 return fromTargetTypeValue(targetTypeValue() + 1);
jjg@309 546 }
jjg@309 547
jjg@309 548 /**
jjg@309 549 * Returns whether or not this TargetType represents an annotation whose
jjg@309 550 * target has a parameter index.
jjg@309 551 *
jjg@309 552 * @return true if this TargetType has a parameter index,
jjg@309 553 * false otherwise
jjg@309 554 */
jjg@309 555 public boolean hasParameter() {
jjg@309 556 return flags.contains(HasParameter);
jjg@309 557 }
jjg@309 558
jjg@309 559 /**
jjg@309 560 * Returns whether or not this TargetType represents an annotation whose
jjg@309 561 * target is a type parameter bound.
jjg@309 562 *
jjg@309 563 * @return true if this TargetType represents an type parameter bound
jjg@309 564 * annotation, false otherwise
jjg@309 565 */
jjg@309 566 public boolean hasBound() {
jjg@309 567 return flags.contains(HasBound);
jjg@309 568 }
jjg@309 569
jjg@309 570 public int targetTypeValue() {
jjg@309 571 return this.targetTypeValue;
jjg@309 572 }
jjg@309 573
jjg@309 574 private static TargetType[] targets = null;
jjg@309 575
jjg@309 576 private static TargetType[] buildTargets() {
jjg@309 577 TargetType[] targets = new TargetType[MAXIMUM_TARGET_TYPE_VALUE + 1];
jjg@309 578 TargetType[] alltargets = values();
jjg@309 579 for (TargetType target : alltargets)
jjg@309 580 if (target.targetTypeValue >= 0)
jjg@309 581 targets[target.targetTypeValue] = target;
jjg@309 582 for (int i = 0; i <= MAXIMUM_TARGET_TYPE_VALUE; ++i)
jjg@309 583 if (targets[i] == null)
jjg@309 584 targets[i] = UNKNOWN;
jjg@309 585 return targets;
jjg@309 586 }
jjg@309 587
jjg@309 588 public static boolean isValidTargetTypeValue(int tag) {
jjg@309 589 if (targets == null)
jjg@309 590 targets = buildTargets();
jjg@309 591
jjg@309 592 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 593 return true;
jjg@309 594
jjg@309 595 return (tag >= 0 && tag < targets.length);
jjg@309 596 }
jjg@309 597
jjg@309 598 public static TargetType fromTargetTypeValue(int tag) {
jjg@309 599 if (targets == null)
jjg@309 600 targets = buildTargets();
jjg@309 601
jjg@309 602 if (((byte)tag) == ((byte)UNKNOWN.targetTypeValue))
jjg@309 603 return UNKNOWN;
jjg@309 604
jjg@309 605 if (tag < 0 || tag >= targets.length)
jjg@309 606 throw new IllegalArgumentException("Unknown TargetType: " + tag);
jjg@309 607 return targets[tag];
jjg@309 608 }
jjg@309 609 }
jjg@309 610
jjg@309 611 static enum TargetAttribute {
jjg@309 612 HasLocation, HasParameter, HasBound;
jjg@309 613 }
jjg@309 614 }

mercurial