6867671: javap whitespace formatting issues
authorjjg
Tue, 04 Aug 2009 17:26:41 -0700
changeset 3546 00b2974fdf04
parent 3545 8077dfd20d32
child 3547 4471bb5983d1
6867671: javap whitespace formatting issues Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/javap/AttributeWriter.java
langtools/src/share/classes/com/sun/tools/javap/BasicWriter.java
langtools/src/share/classes/com/sun/tools/javap/ClassWriter.java
langtools/src/share/classes/com/sun/tools/javap/CodeWriter.java
langtools/src/share/classes/com/sun/tools/javap/ConstantWriter.java
langtools/src/share/classes/com/sun/tools/javap/JavapTask.java
langtools/src/share/classes/com/sun/tools/javap/Options.java
--- a/langtools/src/share/classes/com/sun/tools/javap/AttributeWriter.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/AttributeWriter.java	Tue Aug 04 17:26:41 2009 -0700
@@ -41,7 +41,6 @@
 import com.sun.tools.classfile.Deprecated_attribute;
 import com.sun.tools.classfile.EnclosingMethod_attribute;
 import com.sun.tools.classfile.Exceptions_attribute;
-import com.sun.tools.classfile.Field;
 import com.sun.tools.classfile.InnerClasses_attribute;
 import com.sun.tools.classfile.LineNumberTable_attribute;
 import com.sun.tools.classfile.LocalVariableTable_attribute;
@@ -149,22 +148,26 @@
     }
 
     public Void visitAnnotationDefault(AnnotationDefault_attribute attr, Void ignore) {
-        println("  AnnotationDefault: ");
-        print("    default_value: ");
+        println("AnnotationDefault:");
+        indent(+1);
+        print("default_value: ");
         annotationWriter.write(attr.default_value);
+        indent(-1);
         return null;
     }
 
     public Void visitCharacterRangeTable(CharacterRangeTable_attribute attr, Void ignore) {
-        print("  CharacterRangeTable: ");
+        println("CharacterRangeTable:");
+        indent(+1);
         for (int i = 0; i < attr.character_range_table.length; i++) {
             CharacterRangeTable_attribute.Entry e = attr.character_range_table[i];
             print("    " + e.start_pc + ", " +
                     e.end_pc + ", " +
                     Integer.toHexString(e.character_range_start) + ", " +
                     Integer.toHexString(e.character_range_end) + ", " +
-                    Integer.toHexString(e.flags) +
-                    "\t// ");
+                    Integer.toHexString(e.flags));
+            tab();
+            print("// ");
             print(e.start_pc + ", " +
                     e.end_pc + ", " +
                     (e.character_range_start >> 10) + ":" + (e.character_range_start & 0x3ff) + ", " +
@@ -187,16 +190,13 @@
                 print(", branch-true");
             if ((e.flags & CharacterRangeTable_attribute.CRT_BRANCH_FALSE) != 0)
                 print(", branch-false");
-
-
-
         }
+        indent(-1);
         return null;
     }
 
     public Void visitCode(Code_attribute attr, Void ignore) {
         codeWriter.write(attr, constant_pool);
-        println();
         return null;
     }
 
@@ -207,25 +207,23 @@
 
     public Void visitConstantValue(ConstantValue_attribute attr, Void ignore) {
         if (options.compat) // BUG 6622216 javap names some attributes incorrectly
-            print("  Constant value: ");
+            print("Constant value: ");
         else
-            print("  ConstantValue: ");
+            print("ConstantValue: ");
         constantWriter.write(attr.constantvalue_index);
-        if (!options.compat) // BUG 6622232 javap gets whitespace confused
-            println();
+        println();
         return null;
     }
 
     public Void visitDeprecated(Deprecated_attribute attr, Void ignore) {
-        if (!(options.compat && owner instanceof Field)) // BUG 6622232 javap gets whitespace confused
-            print("  ");
         println("Deprecated: true");
         return null;
     }
 
     public Void visitEnclosingMethod(EnclosingMethod_attribute attr, Void ignore) {
-        print("  EnclosingMethod: #" + attr.class_index + ".#" + attr.method_index
-                + "\t// " + getJavaClassName(attr));
+        print("EnclosingMethod: #" + attr.class_index + ".#" + attr.method_index);
+        tab();
+        print("// " + getJavaClassName(attr));
         if (attr.method_index != 0)
             print("." + getMethodName(attr));
         println();
@@ -249,15 +247,16 @@
     }
 
     public Void visitExceptions(Exceptions_attribute attr, Void ignore) {
-        println("  Exceptions: ");
-        print("   throws ");
+        println("Exceptions:");
+        indent(+1);
+        print("throws ");
         for (int i = 0; i < attr.number_of_exceptions; i++) {
             if (i > 0)
                 print(", ");
             print(getJavaException(attr, i));
         }
-        if (!options.compat) // BUG 6622232 javap gets whitespace confused
-            println();
+        println();
+        indent(-1);
         return null;
     }
 
@@ -290,8 +289,7 @@
                     writeInnerClassHeader();
                     first = false;
                 }
-                if (!options.compat) // BUG 6622232: javap gets whitespace confused
-                    print("   ");
+                print("   ");
                 for (String name: access_flags.getInnerClassModifiers())
                     print(name + " ");
                 if (info.inner_name_index!=0) {
@@ -313,6 +311,8 @@
                 println();
             }
         }
+        if (!first)
+            indent(-1);
         return null;
     }
 
@@ -325,26 +325,28 @@
     }
 
     private void writeInnerClassHeader() {
-        print("  ");
         if (options.compat) // BUG 6622216: javap names some attributes incorrectly
             print("InnerClass");
         else
             print("InnerClasses");
         println(": ");
+        indent(+1);
     }
 
     public Void visitLineNumberTable(LineNumberTable_attribute attr, Void ignore) {
-        println("  LineNumberTable: ");
+        println("LineNumberTable:");
+        indent(+1);
         for (LineNumberTable_attribute.Entry entry: attr.line_number_table) {
-            println("   line " + entry.line_number + ": " + entry.start_pc);
+            println("line " + entry.line_number + ": " + entry.start_pc);
         }
+        indent(-1);
         return null;
     }
 
     public Void visitLocalVariableTable(LocalVariableTable_attribute attr, Void ignore) {
-        println("  LocalVariableTable: ");
-        println("   Start  Length  Slot  Name   Signature");
-
+        println("LocalVariableTable:");
+        indent(+1);
+        println("Start  Length  Slot  Name   Signature");
         for (LocalVariableTable_attribute.Entry entry : attr.local_variable_table) {
             Formatter formatter = new Formatter();
             println(formatter.format("%8d %7d %5d %5s   %s",
@@ -352,25 +354,28 @@
                     constantWriter.stringValue(entry.name_index),
                     constantWriter.stringValue(entry.descriptor_index)));
         }
+        indent(-1);
         return null;
     }
 
     public Void visitLocalVariableTypeTable(LocalVariableTypeTable_attribute attr, Void ignore) {
-        println("  LocalVariableTypeTable: ");
-        println("   Start  Length  Slot  Name   Signature");
-
+        println("LocalVariableTypeTable:");
+        indent(+1);
+        println("Start  Length  Slot  Name   Signature");
         for (LocalVariableTypeTable_attribute.Entry entry : attr.local_variable_table) {
-            Formatter formatter = new Formatter();
-            println(formatter.format("%8d %7d %5d %5s   %s",
+            println(String.format("%5d %7d %5d %5s   %s",
                     entry.start_pc, entry.length, entry.index,
                     constantWriter.stringValue(entry.name_index),
                     constantWriter.stringValue(entry.signature_index)));
         }
+        indent(-1);
         return null;
     }
 
     public Void visitModule(Module_attribute attr, Void ignore) {
-        println("  Module: #" + attr.module_name + "\t// " + getModuleName(attr));
+        print("Module: #" + attr.module_name);
+        tab();
+        println("// " + getModuleName(attr));
         return null;
     }
 
@@ -383,11 +388,15 @@
     }
 
     public Void visitModuleExportTable(ModuleExportTable_attribute attr, Void ignore) {
-        println("  ModuleExportTable:");
-        println("    Types: (" + attr.export_type_table.length + ")");
+        println("ModuleExportTable:");
+        indent(+1);
+        println("Types: (" + attr.export_type_table.length + ")");
         for (int i = 0; i < attr.export_type_table.length; i++) {
-            println("      #" + attr.export_type_table[i] + "\t// " + getExportTypeName(attr, i));
+            print("#" + attr.export_type_table[i]);
+            tab();
+            println("// " + getExportTypeName(attr, i));
         }
+        indent(-1);
         return null;
     }
 
@@ -400,11 +409,15 @@
     }
 
     public Void visitModuleMemberTable(ModuleMemberTable_attribute attr, Void ignore) {
-        println("  ModuleMemberTable:");
-        println("    Packages: (" + attr.package_member_table.length + ")");
+        println("ModuleMemberTable:");
+        indent(+1);
+        println("Packages: (" + attr.package_member_table.length + ")");
         for (int i = 0; i < attr.package_member_table.length; i++) {
-            println("      #" + attr.package_member_table[i] + "\t// " + getPackageMemberName(attr, i));
+            print("#" + attr.package_member_table[i]);
+            tab();
+            println("// " + getPackageMemberName(attr, i));
         }
+        indent(-1);
         return null;
     }
 
@@ -417,73 +430,91 @@
     }
 
     public Void visitRuntimeVisibleAnnotations(RuntimeVisibleAnnotations_attribute attr, Void ignore) {
-        println("  RuntimeVisibleAnnotations: ");
+        println("RuntimeVisibleAnnotations:");
+        indent(+1);
         for (int i = 0; i < attr.annotations.length; i++) {
-            print("    " + i + ": ");
+            print(i + ": ");
             annotationWriter.write(attr.annotations[i]);
             println();
         }
+        indent(-1);
         return null;
     }
 
     public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, Void ignore) {
-        println("  RuntimeInvisibleAnnotations: ");
+        println("RuntimeInvisibleAnnotations:");
+        indent(+1);
         for (int i = 0; i < attr.annotations.length; i++) {
-            print("    " + i + ": ");
+            print(i + ": ");
             annotationWriter.write(attr.annotations[i]);
             println();
         }
+        indent(-1);
         return null;
     }
 
     public Void visitRuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotations_attribute attr, Void ignore) {
-        println("  RuntimeVisibleTypeAnnotations: ");
+        println("RuntimeVisibleTypeAnnotations:");
+        indent(+1);
         for (int i = 0; i < attr.annotations.length; i++) {
-            print("    " + i + ": ");
+            print(i + ": ");
             annotationWriter.write(attr.annotations[i]);
             println();
         }
+        indent(-1);
         return null;
     }
 
     public Void visitRuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotations_attribute attr, Void ignore) {
-        println("  RuntimeInvisibleTypeAnnotations: ");
+        println("RuntimeInvisibleTypeAnnotations:");
+        indent(+1);
         for (int i = 0; i < attr.annotations.length; i++) {
-            print("    " + i + ": ");
+            print(i + ": ");
             annotationWriter.write(attr.annotations[i]);
             println();
         }
+        indent(-1);
         return null;
     }
 
     public Void visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute attr, Void ignore) {
-        println("  RuntimeVisibleParameterAnnotations: ");
+        println("RuntimeVisibleParameterAnnotations:");
+        indent(+1);
         for (int param = 0; param < attr.parameter_annotations.length; param++) {
-            println("    parameter " + param + ": ");
+            println("parameter " + param + ": ");
+            indent(+1);
             for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
-                print("    " + i + ": ");
+                print(i + ": ");
                 annotationWriter.write(attr.parameter_annotations[param][i]);
                 println();
             }
+            indent(-1);
         }
+        indent(-1);
         return null;
     }
 
     public Void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute attr, Void ignore) {
-        println("  RuntimeInvisibleParameterAnnotations: ");
+        println("RuntimeInvisibleParameterAnnotations:");
+        indent(+1);
         for (int param = 0; param < attr.parameter_annotations.length; param++) {
-            println("    " + param + ": ");
+            println(param + ": ");
+            indent(+1);
             for (int i = 0; i < attr.parameter_annotations[param].length; i++) {
-                print("    " + i + ": ");
+                print(i + ": ");
                 annotationWriter.write(attr.parameter_annotations[param][i]);
                 println();
             }
+            indent(-1);
         }
+        indent(-1);
         return null;
     }
 
     public Void visitSignature(Signature_attribute attr, Void ignore) {
-        println("  Signature: #" + attr.signature_index + "\t// " + getSignature(attr));
+        print("Signature: #" + attr.signature_index);
+        tab();
+        println("// " + getSignature(attr));
         return null;
     }
 
@@ -496,12 +527,12 @@
     }
 
     public Void visitSourceDebugExtension(SourceDebugExtension_attribute attr, Void ignore) {
-        println("  SourceDebugExtension: " + attr.getValue());
+        println("SourceDebugExtension: " + attr.getValue());
         return null;
     }
 
     public Void visitSourceFile(SourceFile_attribute attr, Void ignore) {
-        println("  SourceFile: \"" + getSourceFile(attr) + "\"");
+        println("SourceFile: \"" + getSourceFile(attr) + "\"");
         return null;
     }
 
@@ -519,24 +550,26 @@
     }
 
     public Void visitStackMap(StackMap_attribute attr, Void ignore) {
-        println("  StackMap: number_of_entries = " + attr.number_of_entries);
-
+        println("StackMap: number_of_entries = " + attr.number_of_entries);
+        indent(+1);
         StackMapTableWriter w = new StackMapTableWriter();
         for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
             w.write(entry);
         }
         println();
+        indent(-1);
         return null;
     }
 
     public Void visitStackMapTable(StackMapTable_attribute attr, Void ignore) {
-        println("  StackMapTable: number_of_entries = " + attr.number_of_entries);
-
+        println("StackMapTable: number_of_entries = " + attr.number_of_entries);
+        indent(+1);
         StackMapTableWriter w = new StackMapTableWriter();
         for (StackMapTable_attribute.stack_map_frame entry : attr.entries) {
             w.write(entry);
         }
         println();
+        indent(-1);
         return null;
     }
 
@@ -555,29 +588,37 @@
         public Void visit_same_locals_1_stack_item_frame(StackMapTable_attribute.same_locals_1_stack_item_frame frame, Void p) {
             printHeader(frame);
             println(" /* same_locals_1_stack_item */");
+            indent(+1);
             printMap("stack", frame.stack);
+            indent(-1);
             return null;
         }
 
         public Void visit_same_locals_1_stack_item_frame_extended(StackMapTable_attribute.same_locals_1_stack_item_frame_extended frame, Void p) {
             printHeader(frame);
             println(" /* same_locals_1_stack_item_frame_extended */");
-            println("     offset_delta = " + frame.offset_delta);
+            indent(+1);
+            println("offset_delta = " + frame.offset_delta);
             printMap("stack", frame.stack);
+            indent(-1);
             return null;
         }
 
         public Void visit_chop_frame(StackMapTable_attribute.chop_frame frame, Void p) {
             printHeader(frame);
             println(" /* chop */");
-            println("     offset_delta = " + frame.offset_delta);
+            indent(+1);
+            println("offset_delta = " + frame.offset_delta);
+            indent(-1);
             return null;
         }
 
         public Void visit_same_frame_extended(StackMapTable_attribute.same_frame_extended frame, Void p) {
             printHeader(frame);
             println(" /* same_frame_extended */");
-            println("     offset_delta = " + frame.offset_delta);
+            indent(+1);
+            println("offset_delta = " + frame.offset_delta);
+            indent(-1);
             return null;
         }
 
@@ -592,13 +633,16 @@
         public Void visit_full_frame(StackMapTable_attribute.full_frame frame, Void p) {
             printHeader(frame);
             if (frame instanceof StackMap_attribute.stack_map_frame) {
-                println("     offset = " + frame.offset_delta);
+                indent(+1);
+                println(" offset = " + frame.offset_delta);
             } else {
                 println(" /* full_frame */");
-                println("     offset_delta = " + frame.offset_delta);
+                indent(+1);
+                println("offset_delta = " + frame.offset_delta);
             }
             printMap("locals", frame.locals);
             printMap("stack", frame.stack);
+            indent(-1);
             return null;
         }
 
@@ -607,7 +651,7 @@
         }
 
         void printMap(String name, StackMapTable_attribute.verification_type_info[] map) {
-            print("     " + name + " = [");
+            print(name + " = [");
             for (int i = 0; i < map.length; i++) {
                 StackMapTable_attribute.verification_type_info info = map[i];
                 int tag = info.tag;
--- a/langtools/src/share/classes/com/sun/tools/javap/BasicWriter.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/BasicWriter.java	Tue Aug 04 17:26:41 2009 -0700
@@ -71,6 +71,18 @@
         lineWriter.println();
     }
 
+    protected void indent(int delta) {
+        lineWriter.indent(delta);
+    }
+
+    protected void tab() {
+        lineWriter.tab();
+    }
+
+    protected void setPendingNewline(boolean b) {
+        lineWriter.pendingNewline = b;
+    }
+
     protected String report(AttributeException e) {
         out.println("Error: " + e.getMessage()); // i18n?
         return "???";
@@ -122,19 +134,30 @@
 
         protected LineWriter(Context context) {
             context.put(LineWriter.class, this);
+            Options options = Options.instance(context);
+            indentWidth = options.indentWidth;
+            tabColumn = options.tabColumn;
             out = context.get(PrintWriter.class);
             buffer = new StringBuilder();
         }
 
         protected void print(String s) {
+            if (pendingNewline) {
+                println();
+                pendingNewline = false;
+            }
             if (s == null)
                 s = "null";
             for (int i = 0; i < s.length(); i++) {
                 char c = s.charAt(i);
-                if (c == '\n') {
-                    println();
-                } else {
-                    buffer.append(c);
+                switch (c) {
+                    case '\n':
+                        println();
+                        break;
+                    default:
+                        if (buffer.length() == 0)
+                            indent();
+                        buffer.append(c);
                 }
             }
 
@@ -145,8 +168,31 @@
             buffer.setLength(0);
         }
 
+        protected void indent(int delta) {
+            indentCount += delta;
+        }
+
+        protected void tab() {
+            if (buffer.length() == 0)
+                indent();
+            space(indentCount * indentWidth + tabColumn - buffer.length());
+        }
+
+        private void indent() {
+            space(indentCount * indentWidth);
+        }
+
+        private void space(int n) {
+            for (int i = 0; i < n; i++)
+                buffer.append(' ');
+        }
+
         private PrintWriter out;
         private StringBuilder buffer;
+        private int indentCount;
+        private int indentWidth;
+        private int tabColumn;
+        private boolean pendingNewline;
     }
 }
 
--- a/langtools/src/share/classes/com/sun/tools/javap/ClassWriter.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/ClassWriter.java	Tue Aug 04 17:26:41 2009 -0700
@@ -120,6 +120,7 @@
                 else
                     println("Classfile " + uri);
             }
+            indent(+1);
             if (lastModified != -1) {
                 Date lm = new Date(lastModified);
                 DateFormat df = DateFormat.getDateInstance();
@@ -144,6 +145,10 @@
             println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\"");
         }
 
+        if ((options.sysInfo || options.verbose) && !options.compat) {
+            indent(-1);
+        }
+
         String name = getJavaName(classFile);
         AccessFlags flags = cf.access_flags;
 
@@ -186,23 +191,24 @@
 
         if (options.verbose) {
             println();
+            indent(+1);
             attrWriter.write(cf, cf.attributes, constant_pool);
-            println("  minor version: " + cf.minor_version);
-            println("  major version: " + cf.major_version);
+            println("minor version: " + cf.minor_version);
+            println("major version: " + cf.major_version);
             if (!options.compat)
-              writeList("  flags: ", flags.getClassFlags(), NEWLINE);
+              writeList("flags: ", flags.getClassFlags(), NEWLINE);
+            indent(-1);
             constantWriter.writeConstantPool();
-            println();
         } else {
-            if (!options.compat)
-                print(" ");
+            print(" ");
         }
 
         println("{");
+        indent(+1);
         writeFields();
         writeMethods();
+        indent(-1);
         println("}");
-        println();
     }
 
     protected void writeFields() {
@@ -215,14 +221,6 @@
         if (!options.checkAccess(f.access_flags))
             return;
 
-        if (!(options.showLineAndLocalVariableTables
-                || options.showDisassembled
-                || options.verbose
-                || options.showInternalSignatures
-                || options.showAllAttrs)) {
-            print("    ");
-        }
-
         AccessFlags flags = f.access_flags;
         writeModifiers(flags.getFieldModifiers());
         Signature_attribute sigAttr = getSignature(f.attributes);
@@ -251,11 +249,13 @@
         print(";");
         println();
 
+        indent(+1);
+
         if (options.showInternalSignatures)
-            println("  Signature: " + getValue(f.descriptor));
+            println("Signature: " + getValue(f.descriptor));
 
         if (options.verbose && !options.compat)
-            writeList("  flags: ", flags.getFieldFlags(), NEWLINE);
+            writeList("flags: ", flags.getFieldFlags(), NEWLINE);
 
         if (options.showAllAttrs) {
             for (Attribute attr: f.attributes)
@@ -263,6 +263,8 @@
             println();
         }
 
+        indent(-1);
+
         if (options.showDisassembled || options.showLineAndLocalVariableTables)
             println();
     }
@@ -270,6 +272,7 @@
     protected void writeMethods() {
         for (Method m: classFile.methods)
             writeMethod(m);
+        setPendingNewline(false);
     }
 
     protected void writeMethod(Method m) {
@@ -278,14 +281,6 @@
 
         method = m;
 
-        if (!(options.showLineAndLocalVariableTables
-                || options.showDisassembled
-                || options.verbose
-                || options.showInternalSignatures
-                || options.showAllAttrs)) {
-            print("    ");
-        }
-
         AccessFlags flags = m.access_flags;
 
         Descriptor d;
@@ -333,16 +328,6 @@
         if (e_attr != null) { // if there are generic exceptions, there must be erased exceptions
             if (e_attr instanceof Exceptions_attribute) {
                 Exceptions_attribute exceptions = (Exceptions_attribute) e_attr;
-                if (options.compat) { // Bug XXXXXXX whitespace
-                    if (!(options.showLineAndLocalVariableTables
-                            || options.showDisassembled
-                            || options.verbose
-                            || options.showInternalSignatures
-                            || options.showAllAttrs)) {
-                        print("    ");
-                    }
-                    print("  ");
-                }
                 print(" throws ");
                 if (methodExceptions != null) { // use generic list if available
                     writeList("", methodExceptions, "");
@@ -358,14 +343,17 @@
             }
         }
 
-        print(";");
-        println();
+        println(";");
+
+        indent(+1);
 
-        if (options.showInternalSignatures)
-            println("  Signature: " + getValue(m.descriptor));
+        if (options.showInternalSignatures) {
+            println("Signature: " + getValue(m.descriptor));
+        }
 
-        if (options.verbose && !options.compat)
-            writeList("  flags: ", flags.getMethodFlags(), NEWLINE);
+        if (options.verbose && !options.compat) {
+            writeList("flags: ", flags.getMethodFlags(), NEWLINE);
+        }
 
         Code_attribute code = null;
         Attribute c_attr = m.attributes.get(Attribute.Code);
@@ -378,33 +366,35 @@
 
         if (options.showDisassembled && !options.showAllAttrs) {
             if (code != null) {
-                println("  Code:");
+                println("Code:");
                 codeWriter.writeInstrs(code);
                 codeWriter.writeExceptionTable(code);
             }
-            println();
         }
 
         if (options.showLineAndLocalVariableTables) {
-            if (code != null)
+            if (code != null) {
                 attrWriter.write(code, code.attributes.get(Attribute.LineNumberTable), constant_pool);
-            println();
-            if (code != null)
                 attrWriter.write(code, code.attributes.get(Attribute.LocalVariableTable), constant_pool);
-            println();
-            println();
+            }
         }
 
         if (options.showAllAttrs) {
             Attribute[] attrs = m.attributes.attrs;
             for (Attribute attr: attrs)
                 attrWriter.write(m, attr, constant_pool);
+        }
 
-//            // the following condition is to mimic old javap
-//            if (!(attrs.length > 0 &&
-//                    attrs[attrs.length - 1] instanceof Exceptions_attribute))
-            println();
-        }
+        indent(-1);
+
+        // set pendingNewline to write a newline before the next method (if any)
+        // if a separator is desired
+        setPendingNewline(
+                options.showDisassembled ||
+                options.showAllAttrs ||
+                options.showInternalSignatures ||
+                options.showLineAndLocalVariableTables ||
+                options.verbose);
     }
 
     void writeModifiers(Collection<String> items) {
--- a/langtools/src/share/classes/com/sun/tools/javap/CodeWriter.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/CodeWriter.java	Tue Aug 04 17:26:41 2009 -0700
@@ -69,11 +69,13 @@
     }
 
     void write(Code_attribute attr, ConstantPool constant_pool) {
-        println("  Code:");
+        println("Code:");
+        indent(+1);
         writeVerboseHeader(attr, constant_pool);
         writeInstrs(attr);
         writeExceptionTable(attr);
         attrWriter.write(attr, attr.attributes, constant_pool);
+        indent(-1);
     }
 
     public void writeVerboseHeader(Code_attribute attr, ConstantPool constant_pool) {
@@ -90,9 +92,9 @@
             argCount = report(e);
         }
 
-        println("   Stack=" + attr.max_stack +
-                ", Locals=" + attr.max_locals +
-                ", Args_size=" + argCount);
+        println("stack=" + attr.max_stack +
+                ", locals=" + attr.max_locals +
+                ", args_size=" + argCount);
 
     }
 
@@ -115,8 +117,7 @@
     }
 
     public void writeInstr(Instruction instr) {
-        print("   " + instr.getPC() + ":\t");
-        print(instr.getMnemonic());
+        print(String.format("%4d: %-12s ", instr.getPC(), instr.getMnemonic()));
         instr.accept(instructionPrinter, null);
         println();
     }
@@ -134,54 +135,62 @@
         }
 
         public Void visitBranch(Instruction instr, int offset, Void p) {
-            print("\t" + (instr.getPC() + offset));
+            print((instr.getPC() + offset));
             return null;
         }
 
         public Void visitConstantPoolRef(Instruction instr, int index, Void p) {
-            print("\t#" + index + "; //");
+            print("#" + index + ";");
+            tab();
+            print("// ");
             printConstant(index);
             return null;
         }
 
         public Void visitConstantPoolRefAndValue(Instruction instr, int index, int value, Void p) {
-            print("\t#" + index + ",  " + value + "; //");
+            print("#" + index + ",  " + value + ";");
+            tab();
+            print("// ");
             printConstant(index);
             return null;
         }
 
         public Void visitLocal(Instruction instr, int index, Void p) {
-            print("\t" + index);
+            print(index);
             return null;
         }
 
         public Void visitLocalAndValue(Instruction instr, int index, int value, Void p) {
-            print("\t" + index + ", " + value);
+            print(index + ", " + value);
             return null;
         }
 
         public Void visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets) {
             int pc = instr.getPC();
-            print("{ //" + npairs);
+            print("{ // " + npairs);
+            indent(+1);
             for (int i = 0; i < npairs; i++) {
-                print("\n\t\t" + matches[i] + ": " + (pc + offsets[i]) + ";");
+                print("\n" + matches[i] + ": " + (pc + offsets[i]) + ";");
             }
-            print("\n\t\tdefault: " + (pc + default_) + " }");
+            print("\ndefault: " + (pc + default_) + " }");
+            indent(-1);
             return null;
         }
 
         public Void visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets) {
             int pc = instr.getPC();
             print("{ //" + low + " to " + high);
+            indent(+1);
             for (int i = 0; i < offsets.length; i++) {
-                print("\n\t\t" + (low + i) + ": " + (pc + offsets[i]) + ";");
+                print("\n" + (low + i) + ": " + (pc + offsets[i]) + ";");
             }
-            print("\n\t\tdefault: " + (pc + default_) + " }");
+            print("\ndefault: " + (pc + default_) + " }");
+            indent(-1);
             return null;
         }
 
         public Void visitValue(Instruction instr, int value, Void p) {
-            print("\t" + value);
+            print(value);
             return null;
         }
 
@@ -193,13 +202,13 @@
 
     public void writeExceptionTable(Code_attribute attr) {
         if (attr.exception_table_langth > 0) {
-            println("  Exception table:");
-            println("   from   to  target type");
+            println("Exception table:");
+            indent(+1);
+            println(" from    to  target type");
             for (int i = 0; i < attr.exception_table.length; i++) {
                 Code_attribute.Exception_data handler = attr.exception_table[i];
-                printFixedWidthInt(handler.start_pc, 6);
-                printFixedWidthInt(handler.end_pc, 6);
-                printFixedWidthInt(handler.handler_pc, 6);
+                print(String.format(" %5d %5d %5d",
+                        handler.start_pc, handler.end_pc, handler.handler_pc));
                 print("   ");
                 int catch_type = handler.catch_type;
                 if (catch_type == 0) {
@@ -207,9 +216,9 @@
                 } else {
                     print("Class ");
                     println(constantWriter.stringValue(catch_type));
-                    println("");
                 }
             }
+            indent(-1);
         }
 
     }
@@ -218,13 +227,6 @@
         constantWriter.write(index);
     }
 
-    private void printFixedWidthInt(int n, int width) {
-        String s = String.valueOf(n);
-        for (int i = s.length(); i < width; i++)
-            print(" ");
-        print(s);
-    }
-
     private List<InstructionDetailWriter> getDetailWriters(Code_attribute attr) {
         List<InstructionDetailWriter> detailWriters =
                 new ArrayList<InstructionDetailWriter>();
--- a/langtools/src/share/classes/com/sun/tools/javap/ConstantWriter.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/ConstantWriter.java	Tue Aug 04 17:26:41 2009 -0700
@@ -62,7 +62,9 @@
     protected void writeConstantPool(ConstantPool constant_pool) {
         ConstantPool.Visitor<Integer, Void> v = new ConstantPool.Visitor<Integer,Void>() {
             public Integer visitClass(CONSTANT_Class_info info, Void p) {
-                println("#" + info.name_index + ";\t//  " + stringValue(info));
+                print("#" + info.name_index + ";");
+                tab();
+                println("//  " + stringValue(info));
                 return 1;
             }
 
@@ -72,7 +74,9 @@
             }
 
             public Integer visitFieldref(CONSTANT_Fieldref_info info, Void p) {
-                println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t//  " + stringValue(info));
+                print("#" + info.class_index + ".#" + info.name_and_type_index + ";");
+                tab();
+                println("//  " + stringValue(info));
                 return 1;
             }
 
@@ -87,7 +91,9 @@
             }
 
             public Integer visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info info, Void p) {
-                println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t//  " + stringValue(info));
+                print("#" + info.class_index + ".#" + info.name_and_type_index + ";");
+                tab();
+                println("//  " + stringValue(info));
                 return 1;
             }
 
@@ -97,18 +103,23 @@
             }
 
             public Integer visitNameAndType(CONSTANT_NameAndType_info info, Void p) {
-                String tab = (options.compat ? "" : "\t"); // BUG 6622232 javap gets whitespace confused
-                println("#" + info.name_index + ":#" + info.type_index + ";" + tab + "//  " + stringValue(info));
+                print("#" + info.name_index + ":#" + info.type_index + ";");
+                tab();
+                println("//  " + stringValue(info));
                 return 1;
             }
 
             public Integer visitMethodref(CONSTANT_Methodref_info info, Void p) {
-                println("#" + info.class_index + ".#" + info.name_and_type_index + ";\t//  " + stringValue(info));
+                print("#" + info.class_index + ".#" + info.name_and_type_index + ";");
+                tab();
+                println("//  " + stringValue(info));
                 return 1;
             }
 
             public Integer visitString(CONSTANT_String_info info, Void p) {
-                println("#" + info.string_index + ";\t//  " + stringValue(info));
+                print("#" + info.string_index + ";");
+                tab();
+                println("//  " + stringValue(info));
                 return 1;
             }
 
@@ -118,17 +129,21 @@
             }
 
         };
-        println("  Constant pool:");
+        println("Constant pool:");
+        indent(+1);
+        int width = String.valueOf(constant_pool.size()).length() + 1;
         int cpx = 1;
         while (cpx < constant_pool.size()) {
+            print(String.format("const %" + width + "s", ("#" + cpx)));
             try {
                 CPInfo cpInfo = constant_pool.get(cpx);
-                print("const #" + cpx + " = " + tagName(cpInfo.getTag()) + "\t");
+                print(String.format(" = %-15s ", tagName(cpInfo.getTag())));
                 cpx += cpInfo.accept(v, null);
             } catch (ConstantPool.InvalidIndex ex) {
-                print("const #" + cpx); // should not happen
+                // should not happen
             }
         }
+        indent(-1);
     }
 
     protected void write(int cpx) {
--- a/langtools/src/share/classes/com/sun/tools/javap/JavapTask.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Aug 04 17:26:41 2009 -0700
@@ -295,6 +295,38 @@
             void process(JavapTask task, String opt, String arg) {
                 task.options.showInnerClasses = true;
             }
+        },
+
+        new Option(false, "-XDindent:") {
+            @Override
+            boolean matches(String opt) {
+                int sep = opt.indexOf(":");
+                return sep != -1 && super.matches(opt.substring(0, sep + 1));
+            }
+
+            void process(JavapTask task, String opt, String arg) throws BadArgs {
+                int sep = opt.indexOf(":");
+                try {
+                    task.options.indentWidth = Integer.valueOf(opt.substring(sep + 1));
+                } catch (NumberFormatException e) {
+                }
+            }
+        },
+
+        new Option(false, "-XDtab:") {
+            @Override
+            boolean matches(String opt) {
+                int sep = opt.indexOf(":");
+                return sep != -1 && super.matches(opt.substring(0, sep + 1));
+            }
+
+            void process(JavapTask task, String opt, String arg) throws BadArgs {
+                int sep = opt.indexOf(":");
+                try {
+                    task.options.tabColumn = Integer.valueOf(opt.substring(sep + 1));
+                } catch (NumberFormatException e) {
+                }
+            }
         }
 
     };
--- a/langtools/src/share/classes/com/sun/tools/javap/Options.java	Fri Jul 31 17:20:06 2009 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javap/Options.java	Tue Aug 04 17:26:41 2009 -0700
@@ -86,6 +86,8 @@
     public boolean showConstants;
     public boolean sysInfo;
     public boolean showInnerClasses;
+    public int indentWidth = 2;   // #spaces per indentWidth level
+    public int tabColumn = 40;    // column number for comments
 
     public boolean compat;             // bug-for-bug compatibility mode with old javap
     public boolean jsr277;