Thu, 19 Nov 2009 11:38:38 -0800
6902264: fix indentation of tableswitch and lookupswitch
Reviewed-by: ksrini
1 /*
2 * Copyright 2007-2008 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 API supported by Sun Microsystems. If
68 * 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(" " + e.start_pc + ", " +
162 e.end_pc + ", " +
163 Integer.toHexString(e.character_range_start) + ", " +
164 Integer.toHexString(e.character_range_end) + ", " +
165 Integer.toHexString(e.flags));
166 tab();
167 print("// ");
168 print(e.start_pc + ", " +
169 e.end_pc + ", " +
170 (e.character_range_start >> 10) + ":" + (e.character_range_start & 0x3ff) + ", " +
171 (e.character_range_end >> 10) + ":" + (e.character_range_end & 0x3ff));
172 if ((e.flags & CharacterRangeTable_attribute.CRT_STATEMENT) != 0)
173 print(", statement");
174 if ((e.flags & CharacterRangeTable_attribute.CRT_BLOCK) != 0)
175 print(", block");
176 if ((e.flags & CharacterRangeTable_attribute.CRT_ASSIGNMENT) != 0)
177 print(", assignment");
178 if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_CONTROLLER) != 0)
179 print(", flow-controller");
180 if ((e.flags & CharacterRangeTable_attribute.CRT_FLOW_TARGET) != 0)
181 print(", flow-target");
182 if ((e.flags & CharacterRangeTable_attribute.CRT_INVOKE) != 0)
183 print(", invoke");
184 if ((e.flags & CharacterRangeTable_attribute.CRT_CREATE) != 0)
185 print(", create");
186 if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_TRUE) != 0)
187 print(", branch-true");
188 if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_FALSE) != 0)
189 print(", branch-false");
190 }
191 indent(-1);
192 return null;
193 }
195 public Void visitCode(Code_attribute attr, Void ignore) {
196 codeWriter.write(attr, constant_pool);
197 return null;
198 }
200 public Void visitCompilationID(CompilationID_attribute attr, Void ignore) {
201 constantWriter.write(attr.compilationID_index);
202 return null;
203 }
205 public Void visitConstantValue(ConstantValue_attribute attr, Void ignore) {
206 if (options.compat) // BUG 6622216 javap names some attributes incorrectly
207 print("Constant value: ");
208 else
209 print("ConstantValue: ");
210 constantWriter.write(attr.constantvalue_index);
211 println();
212 return null;
213 }
215 public Void visitDeprecated(Deprecated_attribute attr, Void ignore) {
216 println("Deprecated: true");
217 return null;
218 }
220 public Void visitEnclosingMethod(EnclosingMethod_attribute attr, Void ignore) {
221 print("EnclosingMethod: #" + attr.class_index + ".#" + attr.method_index);
222 tab();
223 print("// " + getJavaClassName(attr));
224 if (attr.method_index != 0)
225 print("." + getMethodName(attr));
226 println();
227 return null;
228 }
230 private String getJavaClassName(EnclosingMethod_attribute a) {
231 try {
232 return getJavaName(a.getClassName(constant_pool));
233 } catch (ConstantPoolException e) {
234 return report(e);
235 }
236 }
238 private String getMethodName(EnclosingMethod_attribute a) {
239 try {
240 return a.getMethodName(constant_pool);
241 } catch (ConstantPoolException e) {
242 return report(e);
243 }
244 }
246 public Void visitExceptions(Exceptions_attribute attr, Void ignore) {
247 println("Exceptions:");
248 indent(+1);
249 print("throws ");
250 for (int i = 0; i < attr.number_of_exceptions; i++) {
251 if (i > 0)
252 print(", ");
253 print(getJavaException(attr, i));
254 }
255 println();
256 indent(-1);
257 return null;
258 }
260 private String getJavaException(Exceptions_attribute attr, int index) {
261 try {
262 return getJavaName(attr.getException(index, constant_pool));
263 } catch (ConstantPoolException e) {
264 return report(e);
265 }
266 }
268 public Void visitInnerClasses(InnerClasses_attribute attr, Void ignore) {
269 boolean first = true;
270 if (options.compat) {
271 writeInnerClassHeader();
272 first = false;
273 }
274 for (int i = 0 ; i < attr.classes.length; i++) {
275 InnerClasses_attribute.Info info = attr.classes[i];
276 //access
277 AccessFlags access_flags = info.inner_class_access_flags;
278 if (options.compat) {
279 // BUG 6622215: javap ignores certain relevant access flags
280 access_flags = access_flags.ignore(ACC_STATIC | ACC_PROTECTED | ACC_PRIVATE | ACC_INTERFACE | ACC_SYNTHETIC | ACC_ENUM);
281 // BUG 6622232: javap gets whitespace confused
282 print(" ");
283 }
284 if (options.checkAccess(access_flags)) {
285 if (first) {
286 writeInnerClassHeader();
287 first = false;
288 }
289 print(" ");
290 for (String name: access_flags.getInnerClassModifiers())
291 print(name + " ");
292 if (info.inner_name_index!=0) {
293 print("#" + info.inner_name_index + "= ");
294 }
295 print("#" + info.inner_class_info_index);
296 if (info.outer_class_info_index != 0) {
297 print(" of #" + info.outer_class_info_index);
298 }
299 print("; //");
300 if (info.inner_name_index != 0) {
301 print(getInnerName(constant_pool, info) + "=");
302 }
303 constantWriter.write(info.inner_class_info_index);
304 if (info.outer_class_info_index != 0) {
305 print(" of ");
306 constantWriter.write(info.outer_class_info_index);
307 }
308 println();
309 }
310 }
311 if (!first)
312 indent(-1);
313 return null;
314 }
316 String getInnerName(ConstantPool constant_pool, InnerClasses_attribute.Info info) {
317 try {
318 return info.getInnerName(constant_pool);
319 } catch (ConstantPoolException e) {
320 return report(e);
321 }
322 }
324 private void writeInnerClassHeader() {
325 if (options.compat) // BUG 6622216: javap names some attributes incorrectly
326 print("InnerClass");
327 else
328 print("InnerClasses");
329 println(":");
330 indent(+1);
331 }
333 public Void visitLineNumberTable(LineNumberTable_attribute attr, Void ignore) {
334 println("LineNumberTable:");
335 indent(+1);
336 for (LineNumberTable_attribute.Entry entry: attr.line_number_table) {
337 println("line " + entry.line_number + ": " + entry.start_pc);
338 }
339 indent(-1);
340 return null;
341 }
343 public Void visitLocalVariableTable(LocalVariableTable_attribute attr, Void ignore) {
344 println("LocalVariableTable:");
345 indent(+1);
346 println("Start Length Slot Name Signature");
347 for (LocalVariableTable_attribute.Entry entry : attr.local_variable_table) {
348 Formatter formatter = new Formatter();
349 println(formatter.format("%8d %7d %5d %5s %s",
350 entry.start_pc, entry.length, entry.index,
351 constantWriter.stringValue(entry.name_index),
352 constantWriter.stringValue(entry.descriptor_index)));
353 }
354 indent(-1);
355 return null;
356 }
358 public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, Void ignore) {
359 println("LocalVariableTypeTable:");
360 indent(+1);
361 println("Start Length Slot Name Signature");
362 for (LocalVariableTypeTable_attribute.Entry entry : attr.local_variable_table) {
363 println(String.format("%5d %7d %5d %5s %s",
364 entry.start_pc, entry.length, entry.index,
365 constantWriter.stringValue(entry.name_index),
366 constantWriter.stringValue(entry.signature_index)));
367 }
368 indent(-1);
369 return null;
370 }
372 public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
373 println("RuntimeVisibleAnnotations:");
374 indent(+1);
375 for (int i = 0; i < attr.annotations.length; i++) {
376 print(i + ": ");
377 annotationWriter.write(attr.annotations[i]);
378 println();
379 }
380 indent(-1);
381 return null;
382 }
384 public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
385 println("RuntimeInvisibleAnnotations:");
386 indent(+1);
387 for (int i = 0; i < attr.annotations.length; i++) {
388 print(i + ": ");
389 annotationWriter.write(attr.annotations[i]);
390 println();
391 }
392 indent(-1);
393 return null;
394 }
396 public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, Void ignore) {
397 println("RuntimeVisibleTypeAnnotations:");
398 indent(+1);
399 for (int i = 0; i < attr.annotations.length; i++) {
400 print(i + ": ");
401 annotationWriter.write(attr.annotations[i]);
402 println();
403 }
404 indent(-1);
405 return null;
406 }
408 public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, Void ignore) {
409 println("RuntimeInvisibleTypeAnnotations:");
410 indent(+1);
411 for (int i = 0; i < attr.annotations.length; i++) {
412 print(i + ": ");
413 annotationWriter.write(attr.annotations[i]);
414 println();
415 }
416 indent(-1);
417 return null;
418 }
420 public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) {
421 println("RuntimeVisibleParameterAnnotations:");
422 indent(+1);
423 for (int param = 0; param < attr.parameter_annotations.length; param++) {
424 println("parameter " + param + ": ");
425 indent(+1);
426 for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
427 print(i + ": ");
428 annotationWriter.write(attr.parameter_annotations[param][i]);
429 println();
430 }
431 indent(-1);
432 }
433 indent(-1);
434 return null;
435 }
437 public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) {
438 println("RuntimeInvisibleParameterAnnotations:");
439 indent(+1);
440 for (int param = 0; param < attr.parameter_annotations.length; param++) {
441 println(param + ": ");
442 indent(+1);
443 for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
444 print(i + ": ");
445 annotationWriter.write(attr.parameter_annotations[param][i]);
446 println();
447 }
448 indent(-1);
449 }
450 indent(-1);
451 return null;
452 }
454 public Void visitSignature(Signature_attribute attr, Void ignore) {
455 print("Signature: #" + attr.signature_index);
456 tab();
457 println("// " + getSignature(attr));
458 return null;
459 }
461 String getSignature(Signature_attribute info) {
462 try {
463 return info.getSignature(constant_pool);
464 } catch (ConstantPoolException e) {
465 return report(e);
466 }
467 }
469 public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, Void ignore) {
470 println("SourceDebugExtension: " + attr.getValue());
471 return null;
472 }
474 public Void visitSourceFile(SourceFile_attribute attr, Void ignore) {
475 println("SourceFile: \"" + getSourceFile(attr) + "\"");
476 return null;
477 }
479 private String getSourceFile(SourceFile_attribute attr) {
480 try {
481 return attr.getSourceFile(constant_pool);
482 } catch (ConstantPoolException e) {
483 return report(e);
484 }
485 }
487 public Void visitSourceID(SourceID_attribute attr, Void ignore) {
488 constantWriter.write(attr.sourceID_index);
489 return null;
490 }
492 public Void visitStackMap(StackMap_attribute attr, Void ignore) {
493 println("StackMap: number_of_entries = " + attr.number_of_entries);
494 indent(+1);
495 StackMapTableWriter w = new StackMapTableWriter();
496 for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
497 w.write(entry);
498 }
499 println();
500 indent(-1);
501 return null;
502 }
504 public Void visitStackMapTable(StackMapTable_attribute attr, Void ignore) {
505 println("StackMapTable: number_of_entries = " + attr.number_of_entries);
506 indent(+1);
507 StackMapTableWriter w = new StackMapTableWriter();
508 for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
509 w.write(entry);
510 }
511 println();
512 indent(-1);
513 return null;
514 }
516 class StackMapTableWriter // also handles CLDC StackMap attributes
517 implements StackMapTable_attribute.stack_map_frame.Visitor<Void,Void> {
518 public void write(StackMapTable_attribute.stack_map_frame frame) {
519 frame.accept(this, null);
520 }
522 public Void visit_same_frame(StackMapTable_attribute.same_frame frame, Void p) {
523 printHeader(frame);
524 println(" /* same */");
525 return null;
526 }
528 public Void visit_same_locals_1_stack_item_frame(StackMapTable_attribute.same_locals_1_stack_item_frame frame, Void p) {
529 printHeader(frame);
530 println(" /* same_locals_1_stack_item */");
531 indent(+1);
532 printMap("stack", frame.stack);
533 indent(-1);
534 return null;
535 }
537 public Void visit_same_locals_1_stack_item_frame_extended(StackMapTable_attribute.same_locals_1_stack_item_frame_extended frame, Void p) {
538 printHeader(frame);
539 println(" /* same_locals_1_stack_item_frame_extended */");
540 indent(+1);
541 println("offset_delta = " + frame.offset_delta);
542 printMap("stack", frame.stack);
543 indent(-1);
544 return null;
545 }
547 public Void visit_chop_frame(StackMapTable_attribute.chop_frame frame, Void p) {
548 printHeader(frame);
549 println(" /* chop */");
550 indent(+1);
551 println("offset_delta = " + frame.offset_delta);
552 indent(-1);
553 return null;
554 }
556 public Void visit_same_frame_extended(StackMapTable_attribute.same_frame_extended frame, Void p) {
557 printHeader(frame);
558 println(" /* same_frame_extended */");
559 indent(+1);
560 println("offset_delta = " + frame.offset_delta);
561 indent(-1);
562 return null;
563 }
565 public Void visit_append_frame(StackMapTable_attribute.append_frame frame, Void p) {
566 printHeader(frame);
567 println(" /* append */");
568 println(" offset_delta = " + frame.offset_delta);
569 printMap("locals", frame.locals);
570 return null;
571 }
573 public Void visit_full_frame(StackMapTable_attribute.full_frame frame, Void p) {
574 printHeader(frame);
575 if (frame instanceof StackMap_attribute.stack_map_frame) {
576 indent(+1);
577 println(" offset = " + frame.offset_delta);
578 } else {
579 println(" /* full_frame */");
580 indent(+1);
581 println("offset_delta = " + frame.offset_delta);
582 }
583 printMap("locals", frame.locals);
584 printMap("stack", frame.stack);
585 indent(-1);
586 return null;
587 }
589 void printHeader(StackMapTable_attribute.stack_map_frame frame) {
590 print(" frame_type = " + frame.frame_type);
591 }
593 void printMap(String name, StackMapTable_attribute.verification_type_info[] map) {
594 print(name + " = [");
595 for (int i = 0; i < map.length; i++) {
596 StackMapTable_attribute.verification_type_info info = map[i];
597 int tag = info.tag;
598 switch (tag) {
599 case StackMapTable_attribute.verification_type_info.ITEM_Object:
600 print(" ");
601 constantWriter.write(((StackMapTable_attribute.Object_variable_info) info).cpool_index);
602 break;
603 case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized:
604 print(" " + mapTypeName(tag));
605 print(" " + ((StackMapTable_attribute.Uninitialized_variable_info) info).offset);
606 break;
607 default:
608 print(" " + mapTypeName(tag));
609 }
610 print(i == (map.length - 1) ? " " : ",");
611 }
612 println("]");
613 }
615 String mapTypeName(int tag) {
616 switch (tag) {
617 case StackMapTable_attribute.verification_type_info.ITEM_Top:
618 return "top";
620 case StackMapTable_attribute.verification_type_info.ITEM_Integer:
621 return "int";
623 case StackMapTable_attribute.verification_type_info.ITEM_Float:
624 return "float";
626 case StackMapTable_attribute.verification_type_info.ITEM_Long:
627 return "long";
629 case StackMapTable_attribute.verification_type_info.ITEM_Double:
630 return "double";
632 case StackMapTable_attribute.verification_type_info.ITEM_Null:
633 return "null";
635 case StackMapTable_attribute.verification_type_info.ITEM_UninitializedThis:
636 return "this";
638 case StackMapTable_attribute.verification_type_info.ITEM_Object:
639 return "CP";
641 case StackMapTable_attribute.verification_type_info.ITEM_Uninitialized:
642 return "uninitialized";
644 default:
645 report("unrecognized verification_type_info tag: " + tag);
646 return "[tag:" + tag + "]";
647 }
648 }
649 }
651 public Void visitSynthetic(Synthetic_attribute attr, Void ignore) {
652 println("Synthetic: true");
653 return null;
654 }
656 static String getJavaName(String name) {
657 return name.replace('/', '.');
658 }
660 String toHex(byte b, int w) {
661 if (options.compat) // BUG 6622260: javap prints negative bytes incorrectly in hex
662 return toHex((int) b, w);
663 else
664 return toHex(b & 0xff, w);
665 }
667 static String toHex(int i) {
668 return Integer.toString(i, 16).toUpperCase();
669 }
671 static String toHex(int i, int w) {
672 String s = Integer.toHexString(i).toUpperCase();
673 while (s.length() < w)
674 s = "0" + s;
675 return s.toUpperCase();
676 }
678 private AnnotationWriter annotationWriter;
679 private CodeWriter codeWriter;
680 private ConstantWriter constantWriter;
681 private Options options;
683 private ConstantPool constant_pool;
684 private Object owner;
685 }