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

changeset 1571
af8417e590f4
parent 1563
bc456436c613
child 1755
ddb4a2bfcd82
equal deleted inserted replaced
1570:f91144b7da75 1571:af8417e590f4
84 84
85 Position position = new Position(); 85 Position position = new Position();
86 position.type = type; 86 position.type = type;
87 87
88 switch (type) { 88 switch (type) {
89 // type cast
90 case CAST:
91 // instanceof 89 // instanceof
92 case INSTANCEOF: 90 case INSTANCEOF:
93 // new expression 91 // new expression
94 case NEW: 92 case NEW:
93 // constructor/method reference receiver
94 case CONSTRUCTOR_REFERENCE:
95 case METHOD_REFERENCE:
95 position.offset = cr.readUnsignedShort(); 96 position.offset = cr.readUnsignedShort();
96 break; 97 break;
97 // local variable 98 // local variable
98 case LOCAL_VARIABLE: 99 case LOCAL_VARIABLE:
99 // resource variable 100 // resource variable
140 break; 141 break;
141 // method parameter 142 // method parameter
142 case METHOD_FORMAL_PARAMETER: 143 case METHOD_FORMAL_PARAMETER:
143 position.parameter_index = cr.readUnsignedByte(); 144 position.parameter_index = cr.readUnsignedByte();
144 break; 145 break;
146 // type cast
147 case CAST:
145 // method/constructor/reference type argument 148 // method/constructor/reference type argument
146 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: 149 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
147 case METHOD_INVOCATION_TYPE_ARGUMENT: 150 case METHOD_INVOCATION_TYPE_ARGUMENT:
151 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
148 case METHOD_REFERENCE_TYPE_ARGUMENT: 152 case METHOD_REFERENCE_TYPE_ARGUMENT:
149 position.offset = cr.readUnsignedShort(); 153 position.offset = cr.readUnsignedShort();
150 position.type_index = cr.readUnsignedByte(); 154 position.type_index = cr.readUnsignedByte();
151 break; 155 break;
152 // We don't need to worry about these 156 // We don't need to worry about these
153 case METHOD_RETURN: 157 case METHOD_RETURN:
154 case FIELD: 158 case FIELD:
155 break;
156 // lambda formal parameter
157 case LAMBDA_FORMAL_PARAMETER:
158 position.parameter_index = cr.readUnsignedByte();
159 break; 159 break;
160 case UNKNOWN: 160 case UNKNOWN:
161 throw new AssertionError("TypeAnnotation: UNKNOWN target type should never occur!"); 161 throw new AssertionError("TypeAnnotation: UNKNOWN target type should never occur!");
162 default: 162 default:
163 throw new AssertionError("TypeAnnotation: Unknown target type: " + type); 163 throw new AssertionError("TypeAnnotation: Unknown target type: " + type);
175 175
176 private static int position_length(Position pos) { 176 private static int position_length(Position pos) {
177 int n = 0; 177 int n = 0;
178 n += 1; // TargetType tag is a byte 178 n += 1; // TargetType tag is a byte
179 switch (pos.type) { 179 switch (pos.type) {
180 // type cast
181 case CAST:
182 // instanceof 180 // instanceof
183 case INSTANCEOF: 181 case INSTANCEOF:
184 // new expression 182 // new expression
185 case NEW: 183 case NEW:
186 n += 2; 184 // constructor/method reference receiver
185 case CONSTRUCTOR_REFERENCE:
186 case METHOD_REFERENCE:
187 n += 2; // offset
187 break; 188 break;
188 // local variable 189 // local variable
189 case LOCAL_VARIABLE: 190 case LOCAL_VARIABLE:
190 // resource variable 191 // resource variable
191 case RESOURCE_VARIABLE: 192 case RESOURCE_VARIABLE:
192 n += 2; // table_length; 193 n += 2; // table_length;
193 int table_length = pos.lvarOffset.length; 194 int table_length = pos.lvarOffset.length;
194 n += 2 * table_length; // offset 195 n += 2 * table_length; // offset
195 n += 2 * table_length; // length; 196 n += 2 * table_length; // length
196 n += 2 * table_length; // index 197 n += 2 * table_length; // index
197 break; 198 break;
198 // exception parameter 199 // exception parameter
199 case EXCEPTION_PARAMETER: 200 case EXCEPTION_PARAMETER:
200 n += 1; // exception_index 201 n += 1; // exception_index
204 // Do nothing 205 // Do nothing
205 break; 206 break;
206 // type parameter 207 // type parameter
207 case CLASS_TYPE_PARAMETER: 208 case CLASS_TYPE_PARAMETER:
208 case METHOD_TYPE_PARAMETER: 209 case METHOD_TYPE_PARAMETER:
209 n += 1; // parameter_index; 210 n += 1; // parameter_index
210 break; 211 break;
211 // type parameter bound 212 // type parameter bound
212 case CLASS_TYPE_PARAMETER_BOUND: 213 case CLASS_TYPE_PARAMETER_BOUND:
213 case METHOD_TYPE_PARAMETER_BOUND: 214 case METHOD_TYPE_PARAMETER_BOUND:
214 n += 1; // parameter_index 215 n += 1; // parameter_index
224 break; 225 break;
225 // method parameter 226 // method parameter
226 case METHOD_FORMAL_PARAMETER: 227 case METHOD_FORMAL_PARAMETER:
227 n += 1; // parameter_index 228 n += 1; // parameter_index
228 break; 229 break;
230 // type cast
231 case CAST:
229 // method/constructor/reference type argument 232 // method/constructor/reference type argument
230 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: 233 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
231 case METHOD_INVOCATION_TYPE_ARGUMENT: 234 case METHOD_INVOCATION_TYPE_ARGUMENT:
235 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
232 case METHOD_REFERENCE_TYPE_ARGUMENT: 236 case METHOD_REFERENCE_TYPE_ARGUMENT:
233 n += 2; // offset 237 n += 2; // offset
234 n += 1; // type index 238 n += 1; // type index
235 break; 239 break;
236 // We don't need to worry about these 240 // We don't need to worry about these
237 case METHOD_RETURN: 241 case METHOD_RETURN:
238 case FIELD: 242 case FIELD:
239 break;
240 // lambda formal parameter
241 case LAMBDA_FORMAL_PARAMETER:
242 n += 1; // parameter_index
243 break; 243 break;
244 case UNKNOWN: 244 case UNKNOWN:
245 throw new AssertionError("TypeAnnotation: UNKNOWN target type should never occur!"); 245 throw new AssertionError("TypeAnnotation: UNKNOWN target type should never occur!");
246 default: 246 default:
247 throw new AssertionError("TypeAnnotation: Unknown target type: " + pos.type); 247 throw new AssertionError("TypeAnnotation: Unknown target type: " + pos.type);
375 StringBuilder sb = new StringBuilder(); 375 StringBuilder sb = new StringBuilder();
376 sb.append('['); 376 sb.append('[');
377 sb.append(type); 377 sb.append(type);
378 378
379 switch (type) { 379 switch (type) {
380 // type cast
381 case CAST:
382 // instanceof 380 // instanceof
383 case INSTANCEOF: 381 case INSTANCEOF:
384 // new expression 382 // new expression
385 case NEW: 383 case NEW:
384 // constructor/method reference receiver
385 case CONSTRUCTOR_REFERENCE:
386 case METHOD_REFERENCE:
386 sb.append(", offset = "); 387 sb.append(", offset = ");
387 sb.append(offset); 388 sb.append(offset);
388 break; 389 break;
389 // local variable 390 // local variable
390 case LOCAL_VARIABLE: 391 case LOCAL_VARIABLE:
442 // method parameter 443 // method parameter
443 case METHOD_FORMAL_PARAMETER: 444 case METHOD_FORMAL_PARAMETER:
444 sb.append(", param_index = "); 445 sb.append(", param_index = ");
445 sb.append(parameter_index); 446 sb.append(parameter_index);
446 break; 447 break;
448 // type cast
449 case CAST:
447 // method/constructor/reference type argument 450 // method/constructor/reference type argument
448 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: 451 case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
449 case METHOD_INVOCATION_TYPE_ARGUMENT: 452 case METHOD_INVOCATION_TYPE_ARGUMENT:
453 case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
450 case METHOD_REFERENCE_TYPE_ARGUMENT: 454 case METHOD_REFERENCE_TYPE_ARGUMENT:
451 sb.append(", offset = "); 455 sb.append(", offset = ");
452 sb.append(offset); 456 sb.append(offset);
453 sb.append(", type_index = "); 457 sb.append(", type_index = ");
454 sb.append(type_index); 458 sb.append(type_index);
455 break; 459 break;
456 // We don't need to worry about these 460 // We don't need to worry about these
457 case METHOD_RETURN: 461 case METHOD_RETURN:
458 case FIELD: 462 case FIELD:
459 break;
460 // lambda formal parameter
461 case LAMBDA_FORMAL_PARAMETER:
462 // TODO: also needs an offset?
463 sb.append(", param_index = ");
464 sb.append(parameter_index);
465 break; 463 break;
466 case UNKNOWN: 464 case UNKNOWN:
467 sb.append(", position UNKNOWN!"); 465 sb.append(", position UNKNOWN!");
468 break; 466 break;
469 default: 467 default:
562 RESOURCE_VARIABLE(0x41, true), 560 RESOURCE_VARIABLE(0x41, true),
563 561
564 /** For annotations on an exception parameter. */ 562 /** For annotations on an exception parameter. */
565 EXCEPTION_PARAMETER(0x42, true), 563 EXCEPTION_PARAMETER(0x42, true),
566 564
565 /** For annotations on a type test. */
566 INSTANCEOF(0x43, true),
567
568 /** For annotations on an object creation expression. */
569 NEW(0x44, true),
570
571 /** For annotations on a constructor reference receiver. */
572 CONSTRUCTOR_REFERENCE(0x45, true),
573
574 /** For annotations on a method reference receiver. */
575 METHOD_REFERENCE(0x46, true),
576
567 /** For annotations on a typecast. */ 577 /** For annotations on a typecast. */
568 CAST(0x43, true), 578 CAST(0x47, true),
569
570 /** For annotations on a type test. */
571 INSTANCEOF(0x44, true),
572
573 /** For annotations on an object creation expression. */
574 NEW(0x45, true),
575 579
576 /** For annotations on a type argument of an object creation expression. */ 580 /** For annotations on a type argument of an object creation expression. */
577 CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x46, true), 581 CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT(0x48, true),
578 582
579 /** For annotations on a type argument of a method call. */ 583 /** For annotations on a type argument of a method call. */
580 METHOD_INVOCATION_TYPE_ARGUMENT(0x47, true), 584 METHOD_INVOCATION_TYPE_ARGUMENT(0x49, true),
581 585
582 /** For annotations on a lambda parameter type. */ 586 /** For annotations on a type argument of a constructor reference. */
583 LAMBDA_FORMAL_PARAMETER(0x48, true), 587 CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT(0x4A, true),
584
585 /** For annotations on a method reference. */
586 METHOD_REFERENCE(0x49, true),
587 588
588 /** For annotations on a type argument of a method reference. */ 589 /** For annotations on a type argument of a method reference. */
589 METHOD_REFERENCE_TYPE_ARGUMENT(0x50, true), 590 METHOD_REFERENCE_TYPE_ARGUMENT(0x4B, true),
590 591
591 /** For annotations with an unknown target. */ 592 /** For annotations with an unknown target. */
592 UNKNOWN(0xFF); 593 UNKNOWN(0xFF);
593 594
594 private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x50; 595 private static final int MAXIMUM_TARGET_TYPE_VALUE = 0x4B;
595 596
596 private final int targetTypeValue; 597 private final int targetTypeValue;
597 private final boolean isLocal; 598 private final boolean isLocal;
598 599
599 private TargetType(int targetTypeValue) { 600 private TargetType(int targetTypeValue) {

mercurial