8033581: Incorrect comment aligment

Tue, 11 Feb 2014 19:05:50 +0400

author
kizune
date
Tue, 11 Feb 2014 19:05:50 +0400
changeset 2264
66245d9d84db
parent 2261
79dc4b992c0a
child 2265
483574623ca5

8033581: Incorrect comment aligment
8033798: javap output has unnecessary trailing whitespace
8033726: StackMapTable does not unindent properly
Reviewed-by: ksrini

src/share/classes/com/sun/tools/javap/AttributeWriter.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javap/BasicWriter.java file | annotate | diff | comparison | revisions
test/tools/javap/WhitespaceTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javap/AttributeWriter.java	Thu Feb 06 21:11:27 2014 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javap/AttributeWriter.java	Tue Feb 11 19:05:50 2014 +0400
     1.3 @@ -619,6 +619,7 @@
     1.4              indent(+1);
     1.5              println("offset_delta = " + frame.offset_delta);
     1.6              printMap("locals", frame.locals);
     1.7 +            indent(-1);
     1.8              return null;
     1.9          }
    1.10  
     2.1 --- a/src/share/classes/com/sun/tools/javap/BasicWriter.java	Thu Feb 06 21:11:27 2014 +0000
     2.2 +++ b/src/share/classes/com/sun/tools/javap/BasicWriter.java	Tue Feb 11 19:05:50 2014 +0400
     2.3 @@ -151,12 +151,22 @@
     2.4              for (int i = 0; i < s.length(); i++) {
     2.5                  char c = s.charAt(i);
     2.6                  switch (c) {
     2.7 +                    case ' ':
     2.8 +                        pendingSpaces++;
     2.9 +                        break;
    2.10 +
    2.11                      case '\n':
    2.12                          println();
    2.13                          break;
    2.14 +
    2.15                      default:
    2.16                          if (buffer.length() == 0)
    2.17                              indent();
    2.18 +                        if (pendingSpaces > 0) {
    2.19 +                            for (int sp = 0; sp < pendingSpaces; sp++)
    2.20 +                                buffer.append(' ');
    2.21 +                            pendingSpaces = 0;
    2.22 +                        }
    2.23                          buffer.append(c);
    2.24                  }
    2.25              }
    2.26 @@ -164,6 +174,8 @@
    2.27          }
    2.28  
    2.29          protected void println() {
    2.30 +            // ignore/discard pending spaces
    2.31 +            pendingSpaces = 0;
    2.32              out.println(buffer);
    2.33              buffer.setLength(0);
    2.34          }
    2.35 @@ -173,26 +185,21 @@
    2.36          }
    2.37  
    2.38          protected void tab() {
    2.39 -            if (buffer.length() == 0)
    2.40 -                indent();
    2.41 -            space(indentCount * indentWidth + tabColumn - buffer.length());
    2.42 +            int col = indentCount * indentWidth + tabColumn;
    2.43 +            pendingSpaces += (col <= buffer.length() ? 1 : col - buffer.length());
    2.44          }
    2.45  
    2.46          private void indent() {
    2.47 -            space(indentCount * indentWidth);
    2.48 +            pendingSpaces += (indentCount * indentWidth);
    2.49          }
    2.50  
    2.51 -        private void space(int n) {
    2.52 -            for (int i = 0; i < n; i++)
    2.53 -                buffer.append(' ');
    2.54 -        }
    2.55 -
    2.56 -        private PrintWriter out;
    2.57 -        private StringBuilder buffer;
    2.58 +        private final PrintWriter out;
    2.59 +        private final StringBuilder buffer;
    2.60          private int indentCount;
    2.61 -        private int indentWidth;
    2.62 -        private int tabColumn;
    2.63 +        private final int indentWidth;
    2.64 +        private final int tabColumn;
    2.65          private boolean pendingNewline;
    2.66 +        private int pendingSpaces;
    2.67      }
    2.68  }
    2.69  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javap/WhitespaceTest.java	Tue Feb 11 19:05:50 2014 +0400
     3.3 @@ -0,0 +1,91 @@
     3.4 +/*
     3.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 8033581 8033798 8033726
    3.30 + * @summary Check whitespace in generated output
    3.31 + */
    3.32 +
    3.33 +import java.io.*;
    3.34 +import java.util.*;
    3.35 +
    3.36 +public class WhitespaceTest {
    3.37 +    public static void main(String... args) throws Exception {
    3.38 +        new WhitespaceTest().run();
    3.39 +    }
    3.40 +
    3.41 +    void run() throws Exception {
    3.42 +        test("-v", "java.lang.String");
    3.43 +        test("-XDtab:1", "-v", "java.lang.String");
    3.44 +
    3.45 +        String testClasses = System.getProperty("test.classes");
    3.46 +        for (int i = 10; i < 40; i++)
    3.47 +            test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld");
    3.48 +
    3.49 +        if (errors > 0)
    3.50 +            throw new Exception(errors + " errors found");
    3.51 +    }
    3.52 +
    3.53 +    void test(String... args) throws Exception {
    3.54 +        // need to avoid "//" appearing as a constant in the constant pool
    3.55 +        String slash = "/";
    3.56 +        String doubleSlash = slash + slash;
    3.57 +        System.out.println("test: " + Arrays.asList(args));
    3.58 +        String out = javap(args);
    3.59 +        for (String line: out.split("[\r\n]+")) {
    3.60 +            if (line.endsWith(" "))
    3.61 +                error("line has trailing whitespace: " + line);
    3.62 +            int comment = line.indexOf(doubleSlash);
    3.63 +            if (comment > 0 && line.charAt(comment - 1) != ' ')
    3.64 +                error("no space before comment: " + line);
    3.65 +            if (line.matches(" +}"))
    3.66 +                error("bad indentation: " + line);
    3.67 +        }
    3.68 +    }
    3.69 +
    3.70 +    String javap(String... args) throws Exception {
    3.71 +        StringWriter sw = new StringWriter();
    3.72 +        PrintWriter out = new PrintWriter(sw);
    3.73 +        int rc = com.sun.tools.javap.Main.run(args, out);
    3.74 +        out.close();
    3.75 +        System.out.println(sw.toString());
    3.76 +        if (rc < 0)
    3.77 +            throw new Exception("javap exited, rc=" + rc);
    3.78 +        return sw.toString();
    3.79 +    }
    3.80 +
    3.81 +    void error(String msg) {
    3.82 +        System.out.println("Error: " + msg);
    3.83 +        errors++;
    3.84 +    }
    3.85 +
    3.86 +    int errors;
    3.87 +
    3.88 +    // small class to test repeatedly with different tab values
    3.89 +    static class HelloWorld {
    3.90 +        public static void main(String... args) {
    3.91 +            System.out.println("Hello World!");
    3.92 +        }
    3.93 +    }
    3.94 +}

mercurial