Tue, 28 Dec 2010 15:54:52 -0800
6962318: Update copyright year
Reviewed-by: xdono
1 /*
2 * Copyright (c) 2007, 2010, 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.CharacterRangeTable_attribute;
35 import com.sun.tools.classfile.Code_attribute;
36 import com.sun.tools.classfile.CompilationID_attribute;
37 import com.sun.tools.classfile.ConstantPool;
38 import com.sun.tools.classfile.ConstantPoolException;
39 import com.sun.tools.classfile.ConstantValue_attribute;
40 import com.sun.tools.classfile.DefaultAttribute;
41 import com.sun.tools.classfile.Deprecated_attribute;
42 import com.sun.tools.classfile.EnclosingMethod_attribute;
43 import com.sun.tools.classfile.Exceptions_attribute;
44 import com.sun.tools.classfile.InnerClasses_attribute;
45 import com.sun.tools.classfile.LineNumberTable_attribute;
46 import com.sun.tools.classfile.LocalVariableTable_attribute;
47 import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
48 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
49 import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
50 import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
51 import com.sun.tools.classfile.RuntimeVisibleAnnotations_attribute;
52 import com.sun.tools.classfile.RuntimeVisibleParameterAnnotations_attribute;
53 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
54 import com.sun.tools.classfile.Signature_attribute;
55 import com.sun.tools.classfile.SourceDebugExtension_attribute;
56 import com.sun.tools.classfile.SourceFile_attribute;
57 import com.sun.tools.classfile.SourceID_attribute;
58 import com.sun.tools.classfile.StackMapTable_attribute;
59 import com.sun.tools.classfile.StackMap_attribute;
60 import com.sun.tools.classfile.Synthetic_attribute;
62 import static com.sun.tools.classfile.AccessFlags.*;
64 /*
65 * A writer for writing Attributes as text.
66 *
67 * <p><b>This is NOT part of any supported API.
68 * If you write code that depends on this, you do so at your own risk.
69 * This code and its internal interfaces are subject to change or
70 * deletion without notice.</b>
71 */
72 public class AttributeWriter extends BasicWriter
73 implements Attribute.Visitor<Void,Void>
74 {
75 public static AttributeWriter instance(Context context) {
76 AttributeWriter instance = context.get(AttributeWriter.class);
77 if (instance == null)
78 instance = new AttributeWriter(context);
79 return instance;
80 }
82 protected AttributeWriter(Context context) {
83 super(context);
84 context.put(AttributeWriter.class, this);
85 annotationWriter = AnnotationWriter.instance(context);
86 codeWriter = CodeWriter.instance(context);
87 constantWriter = ConstantWriter.instance(context);
88 options = Options.instance(context);
89 }
91 public void write(Object owner, Attribute attr, ConstantPool constant_pool) {
92 if (attr != null) {
93 // null checks
94 owner.getClass();
95 constant_pool.getClass();
96 this.constant_pool = constant_pool;
97 this.owner = owner;
98 attr.accept(this, null);
99 }
100 }
102 public void write(Object owner, Attributes attrs, ConstantPool constant_pool) {
103 if (attrs != null) {
104 // null checks
105 owner.getClass();
106 constant_pool.getClass();
107 this.constant_pool = constant_pool;
108 this.owner = owner;
109 for (Attribute attr: attrs)
110 attr.accept(this, null);
111 }
112 }
114 public Void visitDefault(DefaultAttribute attr, Void ignore) {
115 byte[] data = attr.info;
116 int i = 0;
117 int j = 0;
118 print(" ");
119 try {
120 print(attr.getName(constant_pool));
121 } catch (ConstantPoolException e) {
122 report(e);
123 print("attribute name = #" + attr.attribute_name_index);
124 }
125 print(": ");
126 println("length = 0x" + toHex(attr.info.length));
128 print(" ");
130 while (i < data.length) {
131 print(toHex(data[i], 2));
133 j++;
134 if (j == 16) {
135 println();
136 print(" ");
137 j = 0;
138 } else {
139 print(" ");
140 }
141 i++;
142 }
143 println();
144 return null;
145 }
147 public Void visitAnnotationDefault(AnnotationDefault_attribute attr, Void ignore) {
148 println("AnnotationDefault:");
149 indent(+1);
150 print("default_value: ");
151 annotationWriter.write(attr.default_value);
152 indent(-1);
153 return null;
154 }
156 public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, Void ignore) {
157 println("CharacterRangeTable:");
158 indent(+1);
159 for (int i = 0; i < attr.character_range_table.length; i++) {
160 CharacterRangeTable_attribute.Entry e = attr.character_range_table[i];
161 print(String.format(" %2d, %2d, %6x, %6x, %4x",
162 e.start_pc, e.end_pc,
163 e.character_range_start, e.character_range_end,
164 e.flags));
165 tab();
166 print(String.format("// %2d, %2d, %4d:%02d, %4d:%02d",
167 e.start_pc, e.end_pc,
168 (e.character_range_start >> 10), (e.character_range_start & 0x3ff),
169 (e.character_range_end >> 10), (e.character_range_end & 0x3ff)));
170 if ((e.flags & CharacterRangeTable_attribute.CRT_STATEMENT) != 0)
171 print(", statement");
172 if ((e.flags & CharacterRangeTable_attribute.CRT_BLOCK) != 0)
173 print(", block");
174 if ((e.flags & CharacterRangeTable_attribute.CRT_ASSIGNMENT) != 0)
175 print(", assignment");
176 if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_CONTROLLER) != 0)
177 print(", flow-controller");
178 if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_TARGET) != 0)
179 print(", flow-target");
180 if ((e.flags & CharacterRangeTable_attribute.CRT_INVOKE) != 0)
181 print(", invoke");
182 if ((e.flags & CharacterRangeTable_attribute.CRT_CREATE) != 0)
183 print(", create");
184 if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_TRUE) != 0)
185 print(", branch-true");
186 if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_FALSE) != 0)
187 print(", branch-false");
188 println();
189 }
190 indent(-1);
191 return null;
192 }
194 public Void visitCode(Code_attribute attr, Void ignore) {
195 codeWriter.write(attr, constant_pool);
196 return null;
197 }
199 public Void visitCompilationID(CompilationID_attribute attr, Void ignore) {
200 constantWriter.write(attr.compilationID_index);
201 return null;
202 }
204 public Void visitConstantValue(ConstantValue_attribute attr, Void ignore) {
205 if (options.compat) // BUG 6622216 javap names some attributes incorrectly
206 print("Constant value: ");
207 else
208 print("ConstantValue: ");
209 constantWriter.write(attr.constantvalue_index);
210 println();
211 return null;
212 }
214 public Void visitDeprecated(Deprecated_attribute attr, Void ignore) {
215 println("Deprecated: true");
216 return null;
217 }
219 public Void visitEnclosingMethod(EnclosingMethod_attribute attr, Void ignore) {
220 print("EnclosingMethod: #" + attr.class_index + ".#" + attr.method_index);
221 tab();
222 print("// " + getJavaClassName(attr));
223 if (attr.method_index != 0)
224 print("." + getMethodName(attr));
225 println();
226 return null;
227 }
229 private String getJavaClassName(EnclosingMethod_attribute a) {
230 try {
231 return getJavaName(a.getClassName(constant_pool));
232 } catch (ConstantPoolException e) {
233 return report(e);
234 }
235 }
237 private String getMethodName(EnclosingMethod_attribute a) {
238 try {
239 return a.getMethodName(constant_pool);
240 } catch (ConstantPoolException e) {
241 return report(e);
242 }
243 }
245 public Void visitExceptions(Exceptions_attribute attr, Void ignore) {
246 println("Exceptions:");
247 indent(+1);
248 print("throws ");
249 for (int i = 0; i < attr.number_of_exceptions; i++) {
250 if (i > 0)
251 print(", ");
252 print(getJavaException(attr, i));
253 }
254 println();
255 indent(-1);
256 return null;
257 }
259 private String getJavaException(Exceptions_attribute attr, int index) {
260 try {
261 return getJavaName(attr.getException(index, constant_pool));
262 } catch (ConstantPoolException e) {
263 return report(e);
264 }
265 }
267 public Void visitInnerClasses(InnerClasses_attribute attr, Void ignore) {
268 boolean first = true;
269 if (options.compat) {
270 writeInnerClassHeader();
271 first = false;
272 }
273 for (int i = 0 ; i < attr.classes.length; i++) {
274 InnerClasses_attribute.Info info = attr.classes[i];
275 //access
276 AccessFlags access_flags = info.inner_class_access_flags;
277 if (options.compat) {
278 // BUG 6622215: javap ignores certain relevant access flags
279 access_flags = access_flags.ignore(ACC_STATIC | ACC_PROTECTED | ACC_PRIVATE | ACC_INTERFACE | ACC_SYNTHETIC | ACC_ENUM);
280 // BUG 6622232: javap gets whitespace confused
281 print(" ");
282 }
283 if (options.checkAccess(access_flags)) {
284 if (first) {
285 writeInnerClassHeader();
286 first = false;
287 }
288 print(" ");
289 for (String name: access_flags.getInnerClassModifiers())
290 print(name + " ");
291 if (info.inner_name_index!=0) {
292 print("#" + info.inner_name_index + "= ");
293 }
294 print("#" + info.inner_class_info_index);
295 if (info.outer_class_info_index != 0) {
296 print(" of #" + info.outer_class_info_index);
297 }
298 print("; //");
299 if (info.inner_name_index != 0) {
300 print(getInnerName(constant_pool, info) + "=");
301 }
302 constantWriter.write(info.inner_class_info_index);
303 if (info.outer_class_info_index != 0) {
304 print(" of ");
305 constantWriter.write(info.outer_class_info_index);
306 }
307 println();
308 }
309 }
310 if (!first)
311 indent(-1);
312 return null;
313 }
315 String getInnerName(ConstantPool constant_pool, InnerClasses_attribute.Info info) {
316 try {
317 return info.getInnerName(constant_pool);
318 } catch (ConstantPoolException e) {
319 return report(e);
320 }
321 }
323 private void writeInnerClassHeader() {
324 if (options.compat) // BUG 6622216: javap names some attributes incorrectly
325 print("InnerClass");
326 else
327 print("InnerClasses");
328 println(":");
329 indent(+1);
330 }
332 public Void visitLineNumberTable(LineNumberTable_attribute attr, Void ignore) {
333 println("LineNumberTable:");
334 indent(+1);
335 for (LineNumberTable_attribute.Entry entry: attr.line_number_table) {
336 println("line " + entry.line_number + ": " + entry.start_pc);
337 }
338 indent(-1);
339 return null;
340 }
342 public Void visitLocalVariableTable(LocalVariableTable_attribute attr, Void ignore) {
343 println("LocalVariableTable:");
344 indent(+1);
345 println("Start Length Slot Name Signature");
346 for (LocalVariableTable_attribute.Entry entry : attr.local_variable_table) {
347 Formatter formatter = new Formatter();
348 println(formatter.format("%8d %7d %5d %5s %s",
349 entry.start_pc, entry.length, entry.index,
350 constantWriter.stringValue(entry.name_index),
351 constantWriter.stringValue(entry.descriptor_index)));
352 }
353 indent(-1);
354 return null;
355 }
357 public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, Void ignore) {
358 println("LocalVariableTypeTable:");
359 indent(+1);
360 println("Start Length Slot Name Signature");
361 for (LocalVariableTypeTable_attribute.Entry entry : attr.local_variable_table) {
362 println(String.format("%5d %7d %5d %5s %s",
363 entry.start_pc, entry.length, entry.index,
364 constantWriter.stringValue(entry.name_index),
365 constantWriter.stringValue(entry.signature_index)));
366 }
367 indent(-1);
368 return null;
369 }
371 public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
372 println("RuntimeVisibleAnnotations:");
373 indent(+1);
374 for (int i = 0; i < attr.annotations.length; i++) {
375 print(i + ": ");
376 annotationWriter.write(attr.annotations[i]);
377 println();
378 }
379 indent(-1);
380 return null;
381 }
383 public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
384 println("RuntimeInvisibleAnnotations:");
385 indent(+1);
386 for (int i = 0; i < attr.annotations.length; i++) {
387 print(i + ": ");
388 annotationWriter.write(attr.annotations[i]);
389 println();
390 }
391 indent(-1);
392 return null;
393 }
395 public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, Void ignore) {
396 println("RuntimeVisibleTypeAnnotations:");
397 indent(+1);
398 for (int i = 0; i < attr.annotations.length; i++) {
399 print(i + ": ");
400 annotationWriter.write(attr.annotations[i]);
401 println();
402 }
403 indent(-1);
404 return null;
405 }
407 public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, Void ignore) {
408 println("RuntimeInvisibleTypeAnnotations:");
409 indent(+1);
410 for (int i = 0; i < attr.annotations.length; i++) {
411 print(i + ": ");
412 annotationWriter.write(attr.annotations[i]);
413 println();
414 }
415 indent(-1);
416 return null;
417 }
419 public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) {
420 println("RuntimeVisibleParameterAnnotations:");
421 indent(+1);
422 for (int param = 0; param < attr.parameter_annotations.length; param++) {
423 println("parameter " + param + ": ");
424 indent(+1);
425 for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
426 print(i + ": ");
427 annotationWriter.write(attr.parameter_annotations[param][i]);
428 println();
429 }
430 indent(-1);
431 }
432 indent(-1);
433 return null;
434 }
436 public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) {
437 println("RuntimeInvisibleParameterAnnotations:");
438 indent(+1);
439 for (int param = 0; param < attr.parameter_annotations.length; param++) {
440 println(param + ": ");
441 indent(+1);
442 for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
443 print(i + ": ");
444 annotationWriter.write(attr.parameter_annotations[param][i]);
445 println();
446 }
447 indent(-1);
448 }
449 indent(-1);
450 return null;
451 }
453 public Void visitSignature(Signature_attribute attr, Void ignore) {
454 print("Signature: #" + attr.signature_index);
455 tab();
456 println("// " + getSignature(attr));
457 return null;
458 }
460 String getSignature(Signature_attribute info) {
461 try {
462 return info.getSignature(constant_pool);
463 } catch (ConstantPoolException e) {
464 return report(e);
465 }
466 }
468 public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, Void ignore) {
469 println("SourceDebugExtension: " + attr.getValue());
470 return null;
471 }
473 public Void visitSourceFile(SourceFile_attribute attr, Void ignore) {
474 println("SourceFile: \"" + getSourceFile(attr) + "\"");
475 return null;
476 }
478 private String getSourceFile(SourceFile_attribute attr) {
479 try {
480 return attr.getSourceFile(constant_pool);
481 } catch (ConstantPoolException e) {
482 return report(e);
483 }
484 }
486 public Void visitSourceID(SourceID_attribute attr, Void ignore) {
487 constantWriter.write(attr.sourceID_index);
488 return null;
489 }
491 public Void visitStackMap(StackMap_attribute attr, Void ignore) {
492 println("StackMap: number_of_entries = " + attr.number_of_entries);
493 indent(+1);
494 StackMapTableWriter w = new StackMapTableWriter();
495 for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
496 w.write(entry);
497 }
498 println();
499 indent(-1);
500 return null;
501 }
503 public Void visitStackMapTable(StackMapTable_attribute attr, Void ignore) {
504 println("StackMapTable: number_of_entries = " + attr.number_of_entries);
505 indent(+1);
506 StackMapTableWriter w = new StackMapTableWriter();
507 for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
508 w.write(entry);
509 }
510 println();
511 indent(-1);
512 return null;
513 }
515 class StackMapTableWriter // also handles CLDC StackMap attributes
516 implements StackMapTable_attribute.stack_map_frame.Visitor<Void,Void> {
517 public void write(StackMapTable_attribute.stack_map_frame frame) {
518 frame.accept(this, null);
519 }
521 public Void visit_same_frame(StackMapTable_attribute.same_frame frame, Void p) {
522 printHeader(frame);
523 println(" /* same */");
524 return null;
525 }
527 public Void visit_same_locals_1_stack_item_frame(StackMapTable_attribute.same_locals_1_stack_item_frame frame, Void p) {
528 printHeader(frame);
529 println(" /* same_locals_1_stack_item */");
530 indent(+1);
531 printMap("stack", frame.stack);
532 indent(-1);
533 return null;
534 }
536 public Void visit_same_locals_1_stack_item_frame_extended(StackMapTable_attribute.same_locals_1_stack_item_frame_extended frame, Void p) {
537 printHeader(frame);
538 println(" /* same_locals_1_stack_item_frame_extended */");
539 indent(+1);
540 println("offset_delta = " + frame.offset_delta);
541 printMap("stack", frame.stack);
542 indent(-1);
543 return null;
544 }
546 public Void visit_chop_frame(StackMapTable_attribute.chop_frame frame, Void p) {
547 printHeader(frame);
548 println(" /* chop */");
549 indent(+1);
550 println("offset_delta = " + frame.offset_delta);
551 indent(-1);
552 return null;
553 }
555 public Void visit_same_frame_extended(StackMapTable_attribute.same_frame_extended frame, Void p) {
556 printHeader(frame);
557 println(" /* same_frame_extended */");
558 indent(+1);
559 println("offset_delta = " + frame.offset_delta);
560 indent(-1);
561 return null;
562 }
564 public Void visit_append_frame(StackMapTable_attribute.append_frame frame, Void p) {
565 printHeader(frame);
566 println(" /* append */");
567 println(" offset_delta = " + frame.offset_delta);
568 printMap("locals", frame.locals);
569 return null;
570 }
572 public Void visit_full_frame(StackMapTable_attribute.full_frame frame, Void p) {
573 printHeader(frame);
574 if (frame instanceof StackMap_attribute.stack_map_frame) {
575 indent(+1);
576 println(" offset = " + frame.offset_delta);
577 } else {
578 println(" /* full_frame */");
579 indent(+1);
580 println("offset_delta = " + frame.offset_delta);
581 }
582 printMap("locals", frame.locals);
583 printMap("stack", frame.stack);
584 indent(-1);
585 return null;
586 }
588 void printHeader(StackMapTable_attribute.stack_map_frame frame) {
589 print(" frame_type = " + frame.frame_type);
590 }
592 void printMap(String name, StackMapTable_attribute.verification_type_info[] map) {
593 print(name + " = [");
594 for (int i = 0; i < map.length; i++) {
595 StackMapTable_attribute.verification_type_info info = map[i];
596 int tag = info.tag;
597 switch (tag) {
598 case StackMapTable_attribute.verification_type_info.ITEM_Object:
599 print(" ");
600 constantWriter.write(((StackMapTable_attribute.Object_variable_info) info).cpool_index);
601 break;
602 case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized:
603 print(" " + mapTypeName(tag));
604 print(" " + ((StackMapTable_attribute.Uninitialized_variable_info) info).offset);
605 break;
606 default:
607 print(" " + mapTypeName(tag));
608 }
609 print(i == (map.length - 1) ? " " : ",");
610 }
611 println("]");
612 }
614 String mapTypeName(int tag) {
615 switch (tag) {
616 case StackMapTable_attribute.verification_type_info.ITEM_Top:
617 return "top";
619 case StackMapTable_attribute.verification_type_info.ITEM_Integer:
620 return "int";
622 case StackMapTable_attribute.verification_type_info.ITEM_Float:
623 return "float";
625 case StackMapTable_attribute.verification_type_info.ITEM_Long:
626 return "long";
628 case StackMapTable_attribute.verification_type_info.ITEM_Double:
629 return "double";
631 case StackMapTable_attribute.verification_type_info.ITEM_Null:
632 return "null";
634 case StackMapTable_attribute.verification_type_info.ITEM_UninitializedThis:
635 return "this";
637 case StackMapTable_attribute.verification_type_info.ITEM_Object:
638 return "CP";
640 case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized:
641 return "uninitialized";
643 default:
644 report("unrecognized verification_type_info tag: " + tag);
645 return "[tag:" + tag + "]";
646 }
647 }
648 }
650 public Void visitSynthetic(Synthetic_attribute attr, Void ignore) {
651 println("Synthetic: true");
652 return null;
653 }
655 static String getJavaName(String name) {
656 return name.replace('/', '.');
657 }
659 String toHex(byte b, int w) {
660 if (options.compat) // BUG 6622260: javap prints negative bytes incorrectly in hex
661 return toHex((int) b, w);
662 else
663 return toHex(b & 0xff, w);
664 }
666 static String toHex(int i) {
667 return Integer.toString(i, 16).toUpperCase();
668 }
670 static String toHex(int i, int w) {
671 String s = Integer.toHexString(i).toUpperCase();
672 while (s.length() < w)
673 s = "0" + s;
674 return s.toUpperCase();
675 }
677 private AnnotationWriter annotationWriter;
678 private CodeWriter codeWriter;
679 private ConstantWriter constantWriter;
680 private Options options;
682 private ConstantPool constant_pool;
683 private Object owner;
684 }