Fri, 31 Aug 2012 10:37:46 +0100
7151010: Add compiler support for repeating annotations
Reviewed-by: jjg, mcimadamore
1 /*
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.javap;
28 import java.util.Formatter;
30 import com.sun.tools.classfile.AccessFlags;
31 import com.sun.tools.classfile.AnnotationDefault_attribute;
32 import com.sun.tools.classfile.Attribute;
33 import com.sun.tools.classfile.Attributes;
34 import com.sun.tools.classfile.BootstrapMethods_attribute;
35 import com.sun.tools.classfile.CharacterRangeTable_attribute;
36 import com.sun.tools.classfile.Code_attribute;
37 import com.sun.tools.classfile.CompilationID_attribute;
38 import com.sun.tools.classfile.ConstantPool;
39 import com.sun.tools.classfile.ConstantPoolException;
40 import com.sun.tools.classfile.ConstantValue_attribute;
41 import com.sun.tools.classfile.DefaultAttribute;
42 import com.sun.tools.classfile.Deprecated_attribute;
43 import com.sun.tools.classfile.EnclosingMethod_attribute;
44 import com.sun.tools.classfile.Exceptions_attribute;
45 import com.sun.tools.classfile.InnerClasses_attribute;
46 import com.sun.tools.classfile.LineNumberTable_attribute;
47 import com.sun.tools.classfile.LocalVariableTable_attribute;
48 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
49 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
50 import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
51 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
52 import com.sun.tools.classfile.RuntimeVisibleParameterAnnotations_attribute;
53 import com.sun.tools.classfile.Signature_attribute;
54 import com.sun.tools.classfile.SourceDebugExtension_attribute;
55 import com.sun.tools.classfile.SourceFile_attribute;
56 import com.sun.tools.classfile.SourceID_attribute;
57 import com.sun.tools.classfile.StackMapTable_attribute;
58 import com.sun.tools.classfile.StackMap_attribute;
59 import com.sun.tools.classfile.Synthetic_attribute;
61 import static com.sun.tools.classfile.AccessFlags.*;
63 /*
64 * A writer for writing Attributes as text.
65 *
66 * <p><b>This is NOT part of any supported API.
67 * If you write code that depends on this, you do so at your own risk.
68 * This code and its internal interfaces are subject to change or
69 * deletion without notice.</b>
70 */
71 public class AttributeWriter extends BasicWriter
72 implements Attribute.Visitor<Void,Void>
73 {
74 public static AttributeWriter instance(Context context) {
75 AttributeWriter instance = context.get(AttributeWriter.class);
76 if (instance == null)
77 instance = new AttributeWriter(context);
78 return instance;
79 }
81 protected AttributeWriter(Context context) {
82 super(context);
83 context.put(AttributeWriter.class, this);
84 annotationWriter = AnnotationWriter.instance(context);
85 codeWriter = CodeWriter.instance(context);
86 constantWriter = ConstantWriter.instance(context);
87 options = Options.instance(context);
88 }
90 public void write(Object owner, Attribute attr, ConstantPool constant_pool) {
91 if (attr != null) {
92 // null checks
93 owner.getClass();
94 constant_pool.getClass();
95 this.constant_pool = constant_pool;
96 this.owner = owner;
97 attr.accept(this, null);
98 }
99 }
101 public void write(Object owner, Attributes attrs, ConstantPool constant_pool) {
102 if (attrs != null) {
103 // null checks
104 owner.getClass();
105 constant_pool.getClass();
106 this.constant_pool = constant_pool;
107 this.owner = owner;
108 for (Attribute attr: attrs)
109 attr.accept(this, null);
110 }
111 }
113 public Void visitDefault(DefaultAttribute attr, Void ignore) {
114 byte[] data = attr.info;
115 int i = 0;
116 int j = 0;
117 print(" ");
118 try {
119 print(attr.getName(constant_pool));
120 } catch (ConstantPoolException e) {
121 report(e);
122 print("attribute name = #" + attr.attribute_name_index);
123 }
124 print(": ");
125 println("length = 0x" + toHex(attr.info.length));
127 print(" ");
129 while (i < data.length) {
130 print(toHex(data[i], 2));
132 j++;
133 if (j == 16) {
134 println();
135 print(" ");
136 j = 0;
137 } else {
138 print(" ");
139 }
140 i++;
141 }
142 println();
143 return null;
144 }
146 public Void visitAnnotationDefault(AnnotationDefault_attribute attr, Void ignore) {
147 println("AnnotationDefault:");
148 indent(+1);
149 print("default_value: ");
150 annotationWriter.write(attr.default_value);
151 indent(-1);
152 return null;
153 }
155 public Void visitBootstrapMethods(BootstrapMethods_attribute attr, Void p) {
156 println(Attribute.BootstrapMethods + ":");
157 for (int i = 0; i < attr.bootstrap_method_specifiers.length ; i++) {
158 BootstrapMethods_attribute.BootstrapMethodSpecifier bsm = attr.bootstrap_method_specifiers[i];
159 indent(+1);
160 print(i + ": #" + bsm.bootstrap_method_ref + " ");
161 println(constantWriter.stringValue(bsm.bootstrap_method_ref));
162 indent(+1);
163 println("Method arguments:");
164 indent(+1);
165 for (int j = 0; j < bsm.bootstrap_arguments.length; j++) {
166 print("#" + bsm.bootstrap_arguments[j] + " ");
167 println(constantWriter.stringValue(bsm.bootstrap_arguments[j]));
168 }
169 indent(-3);
170 }
171 return null;
172 }
174 public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, Void ignore) {
175 println("CharacterRangeTable:");
176 indent(+1);
177 for (int i = 0; i < attr.character_range_table.length; i++) {
178 CharacterRangeTable_attribute.Entry e = attr.character_range_table[i];
179 print(String.format(" %2d, %2d, %6x, %6x, %4x",
180 e.start_pc, e.end_pc,
181 e.character_range_start, e.character_range_end,
182 e.flags));
183 tab();
184 print(String.format("// %2d, %2d, %4d:%02d, %4d:%02d",
185 e.start_pc, e.end_pc,
186 (e.character_range_start >> 10), (e.character_range_start & 0x3ff),
187 (e.character_range_end >> 10), (e.character_range_end & 0x3ff)));
188 if ((e.flags & CharacterRangeTable_attribute.CRT_STATEMENT) != 0)
189 print(", statement");
190 if ((e.flags & CharacterRangeTable_attribute.CRT_BLOCK) != 0)
191 print(", block");
192 if ((e.flags & CharacterRangeTable_attribute.CRT_ASSIGNMENT) != 0)
193 print(", assignment");
194 if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_CONTROLLER) != 0)
195 print(", flow-controller");
196 if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_TARGET) != 0)
197 print(", flow-target");
198 if ((e.flags & CharacterRangeTable_attribute.CRT_INVOKE) != 0)
199 print(", invoke");
200 if ((e.flags & CharacterRangeTable_attribute.CRT_CREATE) != 0)
201 print(", create");
202 if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_TRUE) != 0)
203 print(", branch-true");
204 if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_FALSE) != 0)
205 print(", branch-false");
206 println();
207 }
208 indent(-1);
209 return null;
210 }
212 public Void visitCode(Code_attribute attr, Void ignore) {
213 codeWriter.write(attr, constant_pool);
214 return null;
215 }
217 public Void visitCompilationID(CompilationID_attribute attr, Void ignore) {
218 constantWriter.write(attr.compilationID_index);
219 return null;
220 }
222 public Void visitConstantValue(ConstantValue_attribute attr, Void ignore) {
223 if (options.compat) // BUG 6622216 javap names some attributes incorrectly
224 print("Constant value: ");
225 else
226 print("ConstantValue: ");
227 constantWriter.write(attr.constantvalue_index);
228 println();
229 return null;
230 }
232 public Void visitDeprecated(Deprecated_attribute attr, Void ignore) {
233 println("Deprecated: true");
234 return null;
235 }
237 public Void visitEnclosingMethod(EnclosingMethod_attribute attr, Void ignore) {
238 print("EnclosingMethod: #" + attr.class_index + ".#" + attr.method_index);
239 tab();
240 print("// " + getJavaClassName(attr));
241 if (attr.method_index != 0)
242 print("." + getMethodName(attr));
243 println();
244 return null;
245 }
247 private String getJavaClassName(EnclosingMethod_attribute a) {
248 try {
249 return getJavaName(a.getClassName(constant_pool));
250 } catch (ConstantPoolException e) {
251 return report(e);
252 }
253 }
255 private String getMethodName(EnclosingMethod_attribute a) {
256 try {
257 return a.getMethodName(constant_pool);
258 } catch (ConstantPoolException e) {
259 return report(e);
260 }
261 }
263 public Void visitExceptions(Exceptions_attribute attr, Void ignore) {
264 println("Exceptions:");
265 indent(+1);
266 print("throws ");
267 for (int i = 0; i < attr.number_of_exceptions; i++) {
268 if (i > 0)
269 print(", ");
270 print(getJavaException(attr, i));
271 }
272 println();
273 indent(-1);
274 return null;
275 }
277 private String getJavaException(Exceptions_attribute attr, int index) {
278 try {
279 return getJavaName(attr.getException(index, constant_pool));
280 } catch (ConstantPoolException e) {
281 return report(e);
282 }
283 }
285 public Void visitInnerClasses(InnerClasses_attribute attr, Void ignore) {
286 boolean first = true;
287 if (options.compat) {
288 writeInnerClassHeader();
289 first = false;
290 }
291 for (int i = 0 ; i < attr.classes.length; i++) {
292 InnerClasses_attribute.Info info = attr.classes[i];
293 //access
294 AccessFlags access_flags = info.inner_class_access_flags;
295 if (options.compat) {
296 // BUG 6622215: javap ignores certain relevant access flags
297 access_flags = access_flags.ignore(ACC_STATIC | ACC_PROTECTED | ACC_PRIVATE | ACC_INTERFACE | ACC_SYNTHETIC | ACC_ENUM);
298 // BUG 6622232: javap gets whitespace confused
299 print(" ");
300 }
301 if (options.checkAccess(access_flags)) {
302 if (first) {
303 writeInnerClassHeader();
304 first = false;
305 }
306 print(" ");
307 for (String name: access_flags.getInnerClassModifiers())
308 print(name + " ");
309 if (info.inner_name_index!=0) {
310 print("#" + info.inner_name_index + "= ");
311 }
312 print("#" + info.inner_class_info_index);
313 if (info.outer_class_info_index != 0) {
314 print(" of #" + info.outer_class_info_index);
315 }
316 print("; //");
317 if (info.inner_name_index != 0) {
318 print(getInnerName(constant_pool, info) + "=");
319 }
320 constantWriter.write(info.inner_class_info_index);
321 if (info.outer_class_info_index != 0) {
322 print(" of ");
323 constantWriter.write(info.outer_class_info_index);
324 }
325 println();
326 }
327 }
328 if (!first)
329 indent(-1);
330 return null;
331 }
333 String getInnerName(ConstantPool constant_pool, InnerClasses_attribute.Info info) {
334 try {
335 return info.getInnerName(constant_pool);
336 } catch (ConstantPoolException e) {
337 return report(e);
338 }
339 }
341 private void writeInnerClassHeader() {
342 if (options.compat) // BUG 6622216: javap names some attributes incorrectly
343 print("InnerClass");
344 else
345 print("InnerClasses");
346 println(":");
347 indent(+1);
348 }
350 public Void visitLineNumberTable(LineNumberTable_attribute attr, Void ignore) {
351 println("LineNumberTable:");
352 indent(+1);
353 for (LineNumberTable_attribute.Entry entry: attr.line_number_table) {
354 println("line " + entry.line_number + ": " + entry.start_pc);
355 }
356 indent(-1);
357 return null;
358 }
360 public Void visitLocalVariableTable(LocalVariableTable_attribute attr, Void ignore) {
361 println("LocalVariableTable:");
362 indent(+1);
363 println("Start Length Slot Name Signature");
364 for (LocalVariableTable_attribute.Entry entry : attr.local_variable_table) {
365 Formatter formatter = new Formatter();
366 println(formatter.format("%8d %7d %5d %5s %s",
367 entry.start_pc, entry.length, entry.index,
368 constantWriter.stringValue(entry.name_index),
369 constantWriter.stringValue(entry.descriptor_index)));
370 }
371 indent(-1);
372 return null;
373 }
375 public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, Void ignore) {
376 println("LocalVariableTypeTable:");
377 indent(+1);
378 println("Start Length Slot Name Signature");
379 for (LocalVariableTypeTable_attribute.Entry entry : attr.local_variable_table) {
380 println(String.format("%5d %7d %5d %5s %s",
381 entry.start_pc, entry.length, entry.index,
382 constantWriter.stringValue(entry.name_index),
383 constantWriter.stringValue(entry.signature_index)));
384 }
385 indent(-1);
386 return null;
387 }
389 public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
390 println("RuntimeVisibleAnnotations:");
391 indent(+1);
392 for (int i = 0; i < attr.annotations.length; i++) {
393 print(i + ": ");
394 annotationWriter.write(attr.annotations[i]);
395 println();
396 }
397 indent(-1);
398 return null;
399 }
401 public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
402 println("RuntimeInvisibleAnnotations:");
403 indent(+1);
404 for (int i = 0; i < attr.annotations.length; i++) {
405 print(i + ": ");
406 annotationWriter.write(attr.annotations[i]);
407 println();
408 }
409 indent(-1);
410 return null;
411 }
413 public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) {
414 println("RuntimeVisibleParameterAnnotations:");
415 indent(+1);
416 for (int param = 0; param < attr.parameter_annotations.length; param++) {
417 println("parameter " + param + ": ");
418 indent(+1);
419 for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
420 print(i + ": ");
421 annotationWriter.write(attr.parameter_annotations[param][i]);
422 println();
423 }
424 indent(-1);
425 }
426 indent(-1);
427 return null;
428 }
430 public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) {
431 println("RuntimeInvisibleParameterAnnotations:");
432 indent(+1);
433 for (int param = 0; param < attr.parameter_annotations.length; param++) {
434 println(param + ": ");
435 indent(+1);
436 for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
437 print(i + ": ");
438 annotationWriter.write(attr.parameter_annotations[param][i]);
439 println();
440 }
441 indent(-1);
442 }
443 indent(-1);
444 return null;
445 }
447 public Void visitSignature(Signature_attribute attr, Void ignore) {
448 print("Signature: #" + attr.signature_index);
449 tab();
450 println("// " + getSignature(attr));
451 return null;
452 }
454 String getSignature(Signature_attribute info) {
455 try {
456 return info.getSignature(constant_pool);
457 } catch (ConstantPoolException e) {
458 return report(e);
459 }
460 }
462 public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, Void ignore) {
463 println("SourceDebugExtension: " + attr.getValue());
464 return null;
465 }
467 public Void visitSourceFile(SourceFile_attribute attr, Void ignore) {
468 println("SourceFile: \"" + getSourceFile(attr) + "\"");
469 return null;
470 }
472 private String getSourceFile(SourceFile_attribute attr) {
473 try {
474 return attr.getSourceFile(constant_pool);
475 } catch (ConstantPoolException e) {
476 return report(e);
477 }
478 }
480 public Void visitSourceID(SourceID_attribute attr, Void ignore) {
481 constantWriter.write(attr.sourceID_index);
482 return null;
483 }
485 public Void visitStackMap(StackMap_attribute attr, Void ignore) {
486 println("StackMap: number_of_entries = " + attr.number_of_entries);
487 indent(+1);
488 StackMapTableWriter w = new StackMapTableWriter();
489 for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
490 w.write(entry);
491 }
492 println();
493 indent(-1);
494 return null;
495 }
497 public Void visitStackMapTable(StackMapTable_attribute attr, Void ignore) {
498 println("StackMapTable: number_of_entries = " + attr.number_of_entries);
499 indent(+1);
500 StackMapTableWriter w = new StackMapTableWriter();
501 for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
502 w.write(entry);
503 }
504 println();
505 indent(-1);
506 return null;
507 }
509 class StackMapTableWriter // also handles CLDC StackMap attributes
510 implements StackMapTable_attribute.stack_map_frame.Visitor<Void,Void> {
511 public void write(StackMapTable_attribute.stack_map_frame frame) {
512 frame.accept(this, null);
513 }
515 public Void visit_same_frame(StackMapTable_attribute.same_frame frame, Void p) {
516 printHeader(frame);
517 println(" /* same */");
518 return null;
519 }
521 public Void visit_same_locals_1_stack_item_frame(StackMapTable_attribute.same_locals_1_stack_item_frame frame, Void p) {
522 printHeader(frame);
523 println(" /* same_locals_1_stack_item */");
524 indent(+1);
525 printMap("stack", frame.stack);
526 indent(-1);
527 return null;
528 }
530 public Void visit_same_locals_1_stack_item_frame_extended(StackMapTable_attribute.same_locals_1_stack_item_frame_extended frame, Void p) {
531 printHeader(frame);
532 println(" /* same_locals_1_stack_item_frame_extended */");
533 indent(+1);
534 println("offset_delta = " + frame.offset_delta);
535 printMap("stack", frame.stack);
536 indent(-1);
537 return null;
538 }
540 public Void visit_chop_frame(StackMapTable_attribute.chop_frame frame, Void p) {
541 printHeader(frame);
542 println(" /* chop */");
543 indent(+1);
544 println("offset_delta = " + frame.offset_delta);
545 indent(-1);
546 return null;
547 }
549 public Void visit_same_frame_extended(StackMapTable_attribute.same_frame_extended frame, Void p) {
550 printHeader(frame);
551 println(" /* same_frame_extended */");
552 indent(+1);
553 println("offset_delta = " + frame.offset_delta);
554 indent(-1);
555 return null;
556 }
558 public Void visit_append_frame(StackMapTable_attribute.append_frame frame, Void p) {
559 printHeader(frame);
560 println(" /* append */");
561 println(" offset_delta = " + frame.offset_delta);
562 printMap("locals", frame.locals);
563 return null;
564 }
566 public Void visit_full_frame(StackMapTable_attribute.full_frame frame, Void p) {
567 printHeader(frame);
568 if (frame instanceof StackMap_attribute.stack_map_frame) {
569 indent(+1);
570 println(" offset = " + frame.offset_delta);
571 } else {
572 println(" /* full_frame */");
573 indent(+1);
574 println("offset_delta = " + frame.offset_delta);
575 }
576 printMap("locals", frame.locals);
577 printMap("stack", frame.stack);
578 indent(-1);
579 return null;
580 }
582 void printHeader(StackMapTable_attribute.stack_map_frame frame) {
583 print(" frame_type = " + frame.frame_type);
584 }
586 void printMap(String name, StackMapTable_attribute.verification_type_info[] map) {
587 print(name + " = [");
588 for (int i = 0; i < map.length; i++) {
589 StackMapTable_attribute.verification_type_info info = map[i];
590 int tag = info.tag;
591 switch (tag) {
592 case StackMapTable_attribute.verification_type_info.ITEM_Object:
593 print(" ");
594 constantWriter.write(((StackMapTable_attribute.Object_variable_info) info).cpool_index);
595 break;
596 case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized:
597 print(" " + mapTypeName(tag));
598 print(" " + ((StackMapTable_attribute.Uninitialized_variable_info) info).offset);
599 break;
600 default:
601 print(" " + mapTypeName(tag));
602 }
603 print(i == (map.length - 1) ? " " : ",");
604 }
605 println("]");
606 }
608 String mapTypeName(int tag) {
609 switch (tag) {
610 case StackMapTable_attribute.verification_type_info.ITEM_Top:
611 return "top";
613 case StackMapTable_attribute.verification_type_info.ITEM_Integer:
614 return "int";
616 case StackMapTable_attribute.verification_type_info.ITEM_Float:
617 return "float";
619 case StackMapTable_attribute.verification_type_info.ITEM_Long:
620 return "long";
622 case StackMapTable_attribute.verification_type_info.ITEM_Double:
623 return "double";
625 case StackMapTable_attribute.verification_type_info.ITEM_Null:
626 return "null";
628 case StackMapTable_attribute.verification_type_info.ITEM_UninitializedThis:
629 return "this";
631 case StackMapTable_attribute.verification_type_info.ITEM_Object:
632 return "CP";
634 case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized:
635 return "uninitialized";
637 default:
638 report("unrecognized verification_type_info tag: " + tag);
639 return "[tag:" + tag + "]";
640 }
641 }
642 }
644 public Void visitSynthetic(Synthetic_attribute attr, Void ignore) {
645 println("Synthetic: true");
646 return null;
647 }
649 static String getJavaName(String name) {
650 return name.replace('/', '.');
651 }
653 String toHex(byte b, int w) {
654 if (options.compat) // BUG 6622260: javap prints negative bytes incorrectly in hex
655 return toHex((int) b, w);
656 else
657 return toHex(b & 0xff, w);
658 }
660 static String toHex(int i) {
661 return Integer.toString(i, 16).toUpperCase();
662 }
664 static String toHex(int i, int w) {
665 String s = Integer.toHexString(i).toUpperCase();
666 while (s.length() < w)
667 s = "0" + s;
668 return s.toUpperCase();
669 }
671 private AnnotationWriter annotationWriter;
672 private CodeWriter codeWriter;
673 private ConstantWriter constantWriter;
674 private Options options;
676 private ConstantPool constant_pool;
677 private Object owner;
678 }