1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8033581 8033798 8033726
  27  * @summary Check whitespace in generated output
  28  * @modules jdk.compiler
  29  */
  30 
  31 import java.io.*;
  32 import java.util.*;
  33 
  34 public class WhitespaceTest {
  35     public static void main(String... args) throws Exception {
  36         new WhitespaceTest().run();
  37     }
  38 
  39     void run() throws Exception {
  40         test("-v", "java.lang.String");
  41         test("-XDtab:1", "-v", "java.lang.String");
  42 
  43         String testClasses = System.getProperty("test.classes");
  44         for (int i = 10; i < 40; i++)
  45             test("-XDtab:" + i, "-v", "-classpath", testClasses, "WhitespaceTest$HelloWorld");
  46 
  47         if (errors > 0)
  48             throw new Exception(errors + " errors found");
  49     }
  50 
  51     void test(String... args) throws Exception {
  52         // need to avoid "//" appearing as a constant in the constant pool
  53         String slash = "/";
  54         String doubleSlash = slash + slash;
  55         System.out.println("test: " + Arrays.asList(args));
  56         String out = javap(args);
  57         for (String line: out.split("[\r\n]+")) {
  58             if (line.endsWith(" "))
  59                 error("line has trailing whitespace: " + line);
  60             int comment = line.indexOf(doubleSlash);
  61             if (comment > 0 && line.charAt(comment - 1) != ' ') {
  62                 // make allowance for URLs
  63                 if (!line.matches(".*\\bfile:/{3}.*"))
  64                     error("no space before comment: " + line);
  65             }
  66             if (line.matches(" +}"))
  67                 error("bad indentation: " + line);
  68         }
  69     }
  70 
  71     String javap(String... args) throws Exception {
  72         StringWriter sw = new StringWriter();
  73         PrintWriter out = new PrintWriter(sw);
  74         int rc = com.sun.tools.javap.Main.run(args, out);
  75         out.close();
  76         System.out.println(sw.toString());
  77         if (rc < 0)
  78             throw new Exception("javap exited, rc=" + rc);
  79         return sw.toString();
  80     }
  81 
  82     void error(String msg) {
  83         System.out.println("Error: " + msg);
  84         errors++;
  85     }
  86 
  87     int errors;
  88 
  89     // small class to test repeatedly with different tab values
  90     static class HelloWorld {
  91         public static void main(String... args) {
  92             System.out.println("Hello World!");
  93         }
  94     }
  95 }
  96 
  97