T4880663.java revision 3294:9adfb22ff08f
1252206Sdavidcs/* 2252206Sdavidcs * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. 3252206Sdavidcs * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4252206Sdavidcs * 5252206Sdavidcs * This code is free software; you can redistribute it and/or modify it 6252206Sdavidcs * under the terms of the GNU General Public License version 2 only, as 7252206Sdavidcs * published by the Free Software Foundation. 8252206Sdavidcs * 9252206Sdavidcs * This code is distributed in the hope that it will be useful, but WITHOUT 10252206Sdavidcs * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11252206Sdavidcs * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12252206Sdavidcs * version 2 for more details (a copy is included in the LICENSE file that 13252206Sdavidcs * accompanied this code). 14252206Sdavidcs * 15252206Sdavidcs * You should have received a copy of the GNU General Public License version 16252206Sdavidcs * 2 along with this work; if not, write to the Free Software Foundation, 17252206Sdavidcs * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18252206Sdavidcs * 19252206Sdavidcs * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20252206Sdavidcs * or visit www.oracle.com if you need additional information or have any 21252206Sdavidcs * questions. 22252206Sdavidcs */ 23252206Sdavidcs 24252206Sdavidcs/* 25252206Sdavidcs * @test 26252206Sdavidcs * @bug 4880663 6715757 7031005 27252206Sdavidcs * @summary javap could output whitespace between class name and opening brace 28252206Sdavidcs * javap prints "extends java.lang.Object" 29252206Sdavidcs * @modules jdk.jdeps/com.sun.tools.javap 30252206Sdavidcs */ 31252206Sdavidcs 32252206Sdavidcs 33252206Sdavidcsimport java.io.*; 34252206Sdavidcs 35252206Sdavidcspublic class T4880663 { 36252206Sdavidcs public static void main(String[] args) throws Exception { 37252206Sdavidcs new T4880663().run(); 38252206Sdavidcs } 39252206Sdavidcs 40252206Sdavidcs public void run() throws IOException { 41252206Sdavidcs File javaFile = writeTestFile(); 42252206Sdavidcs File classFile = compileTestFile(javaFile); 43252206Sdavidcs verify(classFile, "class Test {"); 44252206Sdavidcs 45252206Sdavidcs if (errors > 0) 46252206Sdavidcs throw new Error(errors + " found."); 47252206Sdavidcs } 48252206Sdavidcs 49252206Sdavidcs File writeTestFile() throws IOException { 50252206Sdavidcs File f = new File("Test.java"); 51252206Sdavidcs PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 52 out.println("class Test { }"); 53 out.close(); 54 return f; 55 } 56 57 File compileTestFile(File f) { 58 int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); 59 if (rc != 0) 60 throw new Error("compilation failed. rc=" + rc); 61 String path = f.getPath(); 62 return new File(path.substring(0, path.length() - 5) + ".class"); 63 } 64 65 String javap(File classFile) { 66 StringWriter sw = new StringWriter(); 67 PrintWriter out = new PrintWriter(sw); 68 int rc = com.sun.tools.javap.Main.run(new String[] { classFile.getPath() }, out); 69 if (rc != 0) 70 throw new Error("javap failed. rc=" + rc); 71 out.close(); 72 System.out.println(sw.toString()); 73 return sw.toString(); 74 } 75 76 void verify(File classFile, String... expects) { 77 String output = javap(classFile); 78 for (String expect: expects) { 79 if (output.indexOf(expect)< 0) 80 error(expect + " not found"); 81 } 82 } 83 84 void error(String msg) { 85 System.err.println(msg); 86 errors++; 87 } 88 89 int errors; 90} 91