author | duke |
Wed, 05 Jul 2017 17:36:43 +0200 | |
changeset 8448 | 58a1c90ce7dc |
parent 5520 | 86e4b9a9da40 |
child 18388 | 7d67f9206d8f |
permissions | -rw-r--r-- |
3538
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
1 |
/* |
5520 | 2 |
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
3538
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
4 |
* |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
8 |
* |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
14 |
* |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
18 |
* |
5520 | 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. |
|
3538
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
22 |
*/ |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
23 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
24 |
import java.io.*; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
25 |
import java.util.*; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
26 |
import javax.tools.*; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
27 |
import com.sun.tools.javap.*; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
28 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
29 |
/* |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
30 |
* @test |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
31 |
* @bug 4777949 |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
32 |
* @summary Warn javap usage on package with simple name |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
33 |
*/ |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
34 |
public class T4777949 { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
35 |
public static void main(String... args) throws Exception { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
36 |
new T4777949().run(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
37 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
38 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
39 |
void run() throws Exception { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
40 |
File javaFile = writeTestFile(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
41 |
File classFile = compileTestFile(javaFile); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
42 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
43 |
test(".", "p.q.r.Test", false); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
44 |
test("p", "q.r.Test", true); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
45 |
test("p/q", "r.Test", true); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
46 |
test("p/q/r", "Test", true); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
47 |
test(".", "p.q.r.Test.Inner", false); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
48 |
test(".", "p.q.r.Test$Inner", false); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
49 |
test("p", "q.r.Test.Inner", true); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
50 |
test("p", "q.r.Test$Inner", true); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
51 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
52 |
if (errors > 0) |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
53 |
throw new Exception(errors + " errors found"); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
54 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
55 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
56 |
void test(String classPath, String className, boolean expectWarnings) { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
57 |
List<Diagnostic<? extends JavaFileObject>> diags = |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
58 |
javap(Arrays.asList("-classpath", classPath), Arrays.asList(className)); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
59 |
boolean foundWarnings = false; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
60 |
for (Diagnostic<? extends JavaFileObject> d: diags) { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
61 |
if (d.getKind() == Diagnostic.Kind.WARNING) |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
62 |
foundWarnings = true; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
63 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
64 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
65 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
66 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
67 |
File writeTestFile() throws IOException { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
68 |
File f = new File("Test.java"); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
69 |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
70 |
out.println("package p.q.r;"); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
71 |
out.println("class Test { class Inner { } }"); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
72 |
out.close(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
73 |
return f; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
74 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
75 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
76 |
File compileTestFile(File f) { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
77 |
int rc = com.sun.tools.javac.Main.compile(new String[] { "-d", ".", f.getPath() }); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
78 |
if (rc != 0) |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
79 |
throw new Error("compilation failed. rc=" + rc); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
80 |
String path = f.getPath(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
81 |
return new File(path.substring(0, path.length() - 5) + ".class"); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
82 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
83 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
84 |
List<Diagnostic<? extends JavaFileObject>> javap(List<String> args, List<String> classes) { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
85 |
DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
86 |
StringWriter sw = new StringWriter(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
87 |
PrintWriter pw = new PrintWriter(sw); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
88 |
JavaFileManager fm = JavapFileManager.create(dc, pw); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
89 |
JavapTask t = new JavapTask(pw, fm, dc, args, classes); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
90 |
boolean ok = t.run(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
91 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
92 |
List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics(); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
93 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
94 |
if (!ok) |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
95 |
error("javap failed unexpectedly"); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
96 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
97 |
System.err.println("args=" + args + " classes=" + classes + "\n" |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
98 |
+ diags + "\n" |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
99 |
+ sw); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
100 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
101 |
return diags; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
102 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
103 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
104 |
void error(String msg) { |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
105 |
System.err.println("error: " + msg); |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
106 |
errors++; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
107 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
108 |
|
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
109 |
int errors; |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
110 |
} |
f2f87f1bc5ad
4777949: Javap Rewrite : Warn javap usage on package classes with simple name.
jjg
parents:
diff
changeset
|
111 |