Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2008, 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 */
24 import java.io.*;
25 import java.util.*;
26 import javax.tools.*;
27 import com.sun.tools.javap.*;
29 /*
30 * @test
31 * @bug 4777949
32 * @summary Warn javap usage on package with simple name
33 */
34 public class T4777949 {
35 public static void main(String... args) throws Exception {
36 new T4777949().run();
37 }
39 void run() throws Exception {
40 File javaFile = writeTestFile();
41 File classFile = compileTestFile(javaFile);
43 test(".", "p.q.r.Test", false);
44 test("p", "q.r.Test", true);
45 test("p/q", "r.Test", true);
46 test("p/q/r", "Test", true);
47 test(".", "p.q.r.Test.Inner", false);
48 test(".", "p.q.r.Test$Inner", false);
49 test("p", "q.r.Test.Inner", true);
50 test("p", "q.r.Test$Inner", true);
52 if (errors > 0)
53 throw new Exception(errors + " errors found");
54 }
56 void test(String classPath, String className, boolean expectWarnings) {
57 List<Diagnostic<? extends JavaFileObject>> diags =
58 javap(Arrays.asList("-classpath", classPath), Arrays.asList(className));
59 boolean foundWarnings = false;
60 for (Diagnostic<? extends JavaFileObject> d: diags) {
61 if (d.getKind() == Diagnostic.Kind.WARNING)
62 foundWarnings = true;
63 }
64 }
67 File writeTestFile() throws IOException {
68 File f = new File("Test.java");
69 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
70 out.println("package p.q.r;");
71 out.println("class Test { class Inner { } }");
72 out.close();
73 return f;
74 }
76 File compileTestFile(File f) {
77 int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() });
78 if (rc != 0)
79 throw new Error("compilation failed. rc=" + rc);
80 String path = f.getPath();
81 return new File(path.substring(0, path.length() - 5) + ".class");
82 }
84 List<Diagnostic<? extends JavaFileObject>> javap(List<String> args, List<String> classes) {
85 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
86 StringWriter sw = new StringWriter();
87 PrintWriter pw = new PrintWriter(sw);
88 JavaFileManager fm = JavapFileManager.create(dc, pw);
89 JavapTask t = new JavapTask(pw, fm, dc, args, classes);
90 boolean ok = t.run();
92 List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
94 if (!ok)
95 error("javap failed unexpectedly");
97 System.err.println("args=" + args + " classes=" + classes + "\n"
98 + diags + "\n"
99 + sw);
101 return diags;
102 }
104 void error(String msg) {
105 System.err.println("error: " + msg);
106 errors++;
107 }
109 int errors;
110 }