Path: blob/master/test/langtools/tools/javap/T6622232.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 662223226* @summary javap gets whitespace confused27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;3132public class T6622232 {33public static void main(String[] args) throws Exception {34new T6622232().run();35}3637public void run() throws IOException {38File javaFile = writeTestFile();39File classFile = compileTestFile(javaFile);40String output = javap(classFile);4142// these are all examples of bad whitespace from old javap43verifyNot(output,44"\\Q Constant value: int 3Deprecated: true\\E",45"^Deprecated: true",46"\\Q throws java.lang.Exception, java.lang.Error Deprecated: true\\E"47);4849if (errors > 0)50throw new Error(errors + " found.");51}5253File writeTestFile() throws IOException {54File f = new File("Test.java");55PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));56out.println("class Test { ");57out.println(" @Deprecated static final int f1 = 3;");58out.println(" @Deprecated int f2;");59out.println(" @Deprecated void m() throws Exception, Error { }");60out.println("}");61out.close();62return f;63}6465File compileTestFile(File f) {66int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() });67if (rc != 0)68throw new Error("compilation failed. rc=" + rc);69String path = f.getPath();70return new File(path.substring(0, path.length() - 5) + ".class");71}7273String javap(File f) {74StringWriter sw = new StringWriter();75PrintWriter out = new PrintWriter(sw);76int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out);77if (rc != 0)78throw new Error("javap failed. rc=" + rc);79out.close();80System.out.println(sw.toString());81return sw.toString();82}8384void verifyNot(String output, String... unexpects) {85for (String unexpect: unexpects) {86if (output.matches(unexpect))87error(unexpect + " unexpectedly found");88}89}9091void error(String msg) {92System.err.println(msg);93errors++;94}9596int errors;97}9899100