langtools/test/tools/javap/WhitespaceTest.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 23110 4d5b75ae3018
child 27225 8369cde9152a
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23110
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     1
/*
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     4
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     8
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    13
 * accompanied this code).
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    14
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    18
 *
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    21
 * questions.
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    22
 */
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    23
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    24
/*
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    25
 * @test
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    26
 * @bug 8033581 8033798 8033726
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    27
 * @summary Check whitespace in generated output
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    28
 */
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    29
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    30
import java.io.*;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    31
import java.util.*;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    32
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    33
public class WhitespaceTest {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    34
    public static void main(String... args) throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    35
        new WhitespaceTest().run();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    36
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    37
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    38
    void run() throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    39
        test("-v", "java.lang.String");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    40
        test("-XDtab:1", "-v", "java.lang.String");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    41
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    42
        String testClasses = System.getProperty("test.classes");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    43
        for (int i = 10; i < 40; i++)
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    44
            test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    45
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    46
        if (errors > 0)
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    47
            throw new Exception(errors + " errors found");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    48
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    49
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    50
    void test(String... args) throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    51
        // need to avoid "//" appearing as a constant in the constant pool
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    52
        String slash = "/";
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    53
        String doubleSlash = slash + slash;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    54
        System.out.println("test: " + Arrays.asList(args));
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    55
        String out = javap(args);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    56
        for (String line: out.split("[\r\n]+")) {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    57
            if (line.endsWith(" "))
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    58
                error("line has trailing whitespace: " + line);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    59
            int comment = line.indexOf(doubleSlash);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    60
            if (comment > 0 && line.charAt(comment - 1) != ' ')
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    61
                error("no space before comment: " + line);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    62
            if (line.matches(" +}"))
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    63
                error("bad indentation: " + line);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    64
        }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    65
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    66
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    67
    String javap(String... args) throws Exception {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    68
        StringWriter sw = new StringWriter();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    69
        PrintWriter out = new PrintWriter(sw);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    70
        int rc = com.sun.tools.javap.Main.run(args, out);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    71
        out.close();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    72
        System.out.println(sw.toString());
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    73
        if (rc < 0)
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    74
            throw new Exception("javap exited, rc=" + rc);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    75
        return sw.toString();
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    76
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    77
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    78
    void error(String msg) {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    79
        System.out.println("Error: " + msg);
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    80
        errors++;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    81
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    82
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    83
    int errors;
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    84
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    85
    // small class to test repeatedly with different tab values
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    86
    static class HelloWorld {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    87
        public static void main(String... args) {
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    88
            System.out.println("Hello World!");
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    89
        }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    90
    }
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    91
}
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    92
4d5b75ae3018 8033581: Incorrect comment aligment
jjg
parents:
diff changeset
    93