T4777949.java revision 553:9d9f26857129
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 */
23
24import java.io.*;
25import java.util.*;
26import javax.tools.*;
27import com.sun.tools.javap.*;
28
29/*
30 * @test
31 * @bug 4777949
32 * @summary Warn javap usage on package with simple name
33 */
34public class T4777949 {
35    public static void main(String... args) throws Exception {
36        new T4777949().run();
37    }
38
39    void run() throws Exception {
40        File javaFile = writeTestFile();
41        File classFile = compileTestFile(javaFile);
42
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);
51
52        if (errors > 0)
53            throw new Exception(errors + " errors found");
54    }
55
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    }
65
66
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    }
75
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    }
83
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();
91
92        List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
93
94        if (!ok)
95            error("javap failed unexpectedly");
96
97        System.err.println("args=" + args + " classes=" + classes + "\n"
98                           + diags + "\n"
99                           + sw);
100
101        return diags;
102    }
103
104    void error(String msg) {
105        System.err.println("error: " + msg);
106        errors++;
107    }
108
109    int errors;
110}
111
112