author | ohair |
Tue, 25 May 2010 15:54:51 -0700 | |
changeset 5520 | 86e4b9a9da40 |
parent 727 | cb50c1ae7bab |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
727
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
1 |
/* |
5520 | 2 |
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. |
727
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
4 |
* |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
8 |
* |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
14 |
* |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
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. |
|
727
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
22 |
*/ |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
23 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
24 |
/* |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
25 |
* @test |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
26 |
* @bug 6622232 |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
27 |
* @summary javap gets whitespace confused |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
28 |
*/ |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
29 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
30 |
import java.io.*; |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
31 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
32 |
public class T6622232 { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
33 |
public static void main(String[] args) throws Exception { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
34 |
new T6622232().run(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
35 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
36 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
37 |
public void run() throws IOException { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
38 |
File javaFile = writeTestFile(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
39 |
File classFile = compileTestFile(javaFile); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
40 |
String output = javap(classFile); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
41 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
42 |
// these are all examples of bad whitespace from old javap |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
43 |
verifyNot(output, |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
44 |
"\\Q Constant value: int 3Deprecated: true\\E", |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
45 |
"^Deprecated: true", |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
46 |
"\\Q throws java.lang.Exception, java.lang.Error Deprecated: true\\E" |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
47 |
); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
48 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
49 |
if (errors > 0) |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
50 |
throw new Error(errors + " found."); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
51 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
52 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
53 |
File writeTestFile() throws IOException { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
54 |
File f = new File("Test.java"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
55 |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
56 |
out.println("class Test { "); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
57 |
out.println(" @Deprecated static final int f1 = 3;"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
58 |
out.println(" @Deprecated int f2;"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
59 |
out.println(" @Deprecated void m() throws Exception, Error { }"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
60 |
out.println("}"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
61 |
out.close(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
62 |
return f; |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
63 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
64 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
65 |
File compileTestFile(File f) { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
66 |
int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
67 |
if (rc != 0) |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
68 |
throw new Error("compilation failed. rc=" + rc); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
69 |
String path = f.getPath(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
70 |
return new File(path.substring(0, path.length() - 5) + ".class"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
71 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
72 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
73 |
String javap(File f) { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
74 |
StringWriter sw = new StringWriter(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
75 |
PrintWriter out = new PrintWriter(sw); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
76 |
int rc = com.sun.tools.javap.Main.run(new String[] { "-v", f.getPath() }, out); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
77 |
if (rc != 0) |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
78 |
throw new Error("javap failed. rc=" + rc); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
79 |
out.close(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
80 |
System.out.println(sw.toString()); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
81 |
return sw.toString(); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
82 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
83 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
84 |
void verifyNot(String output, String... unexpects) { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
85 |
for (String unexpect: unexpects) { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
86 |
if (output.matches(unexpect)) |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
87 |
error(unexpect + " unexpectedly found"); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
88 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
89 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
90 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
91 |
void error(String msg) { |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
92 |
System.err.println(msg); |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
93 |
errors++; |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
94 |
} |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
95 |
|
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
96 |
int errors; |
cb50c1ae7bab
4075303: Use javap to enquire aboput a specific inner class
jjg
parents:
diff
changeset
|
97 |
} |