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

Thu, 23 Jul 2009 11:37:44 -0700

author
jjg
date
Thu, 23 Jul 2009 11:37:44 -0700
changeset 328
99b7a25185aa
parent 309
664edca41e34
child 338
777a3efad0d5
permissions
-rw-r--r--

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

mercurial