Path: blob/master/test/langtools/tools/javap/T4880663.java
41144 views
/*1* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4880663 6715757 703100526* @summary javap could output whitespace between class name and opening brace27* javap prints "extends java.lang.Object"28* @modules jdk.jdeps/com.sun.tools.javap29*/303132import java.io.*;3334public class T4880663 {35public static void main(String[] args) throws Exception {36new T4880663().run();37}3839public void run() throws IOException {40File javaFile = writeTestFile();41File classFile = compileTestFile(javaFile);42verify(classFile, "class Test {");4344if (errors > 0)45throw new Error(errors + " found.");46}4748File writeTestFile() throws IOException {49File f = new File("Test.java");50PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));51out.println("class Test { }");52out.close();53return f;54}5556File compileTestFile(File f) {57int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });58if (rc != 0)59throw new Error("compilation failed. rc=" + rc);60String path = f.getPath();61return new File(path.substring(0, path.length() - 5) + ".class");62}6364String javap(File classFile) {65StringWriter sw = new StringWriter();66PrintWriter out = new PrintWriter(sw);67int rc = com.sun.tools.javap.Main.run(new String[] { classFile.getPath() }, out);68if (rc != 0)69throw new Error("javap failed. rc=" + rc);70out.close();71System.out.println(sw.toString());72return sw.toString();73}7475void verify(File classFile, String... expects) {76String output = javap(classFile);77for (String expect: expects) {78if (output.indexOf(expect)< 0)79error(expect + " not found");80}81}8283void error(String msg) {84System.err.println(msg);85errors++;86}8788int errors;89}909192