aoqi@0: /* aoqi@0: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8033581 8033798 8033726 aoqi@0: * @summary Check whitespace in generated output aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: aoqi@0: public class WhitespaceTest { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new WhitespaceTest().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: test("-v", "java.lang.String"); aoqi@0: test("-XDtab:1", "-v", "java.lang.String"); aoqi@0: aoqi@0: String testClasses = System.getProperty("test.classes"); aoqi@0: for (int i = 10; i < 40; i++) aoqi@0: test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld"); aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors found"); aoqi@0: } aoqi@0: aoqi@0: void test(String... args) throws Exception { aoqi@0: // need to avoid "//" appearing as a constant in the constant pool aoqi@0: String slash = "/"; aoqi@0: String doubleSlash = slash + slash; aoqi@0: System.out.println("test: " + Arrays.asList(args)); aoqi@0: String out = javap(args); aoqi@0: for (String line: out.split("[\r\n]+")) { aoqi@0: if (line.endsWith(" ")) aoqi@0: error("line has trailing whitespace: " + line); aoqi@0: int comment = line.indexOf(doubleSlash); aoqi@0: if (comment > 0 && line.charAt(comment - 1) != ' ') aoqi@0: error("no space before comment: " + line); aoqi@0: if (line.matches(" +}")) aoqi@0: error("bad indentation: " + line); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: String javap(String... args) throws Exception { aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter out = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javap.Main.run(args, out); aoqi@0: out.close(); aoqi@0: System.out.println(sw.toString()); aoqi@0: if (rc < 0) aoqi@0: throw new Exception("javap exited, rc=" + rc); aoqi@0: return sw.toString(); aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.out.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: // small class to test repeatedly with different tab values aoqi@0: static class HelloWorld { aoqi@0: public static void main(String... args) { aoqi@0: System.out.println("Hello World!"); aoqi@0: } aoqi@0: } aoqi@0: }