author | trims |
Fri, 02 Jul 2010 01:37:36 -0700 | |
changeset 5910 | ca3d85421cbb |
parent 5520 | 86e4b9a9da40 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
3784
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
1 |
/* |
5520 | 2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
3784
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
4 |
* |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
8 |
* |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
14 |
* |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
182e4a28c0ce
6879371: javap does not close internal default file manager
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. |
|
3784
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
22 |
*/ |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
23 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
24 |
/* |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
25 |
* @test |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
26 |
* @bug 6879371 |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
27 |
* @summary javap does not close internal default file manager |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
28 |
*/ |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
29 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
30 |
import java.io.*; |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
31 |
import java.util.zip.*; |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
32 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
33 |
public class T6879371 { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
34 |
public static void main(String[] args) throws Exception { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
35 |
new T6879371().run(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
36 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
37 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
38 |
public void run() throws Exception { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
39 |
// create a simple test class which we can put into |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
40 |
// a test zip file and know that it will be used by |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
41 |
// javap. |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
42 |
File classDir = new File("classes"); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
43 |
classDir.mkdir(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
44 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
45 |
String className = "Test"; |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
46 |
File javaFile = writeTestFile(className); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
47 |
compileTestFile(classDir, javaFile); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
48 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
49 |
test(classDir, className, false); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
50 |
test(classDir, className, true); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
51 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
52 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
53 |
void test(File classDir, String className, boolean useJavaUtilZip) throws Exception { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
54 |
// javac should really not be using system properties like this |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
55 |
// -- it should really be using (hidden) options -- but until then |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
56 |
// take care to leave system properties as we find them, so as not |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
57 |
// to adversely affect other tests that might follow. |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
58 |
String prev = System.getProperty("useJavaUtilZip"); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
59 |
setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null)); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
60 |
try { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
61 |
File zipFile = zip(classDir, new File(classDir + ".zip")); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
62 |
javap("-classpath", zipFile.getPath(), className); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
63 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
64 |
if (!zipFile.delete()) |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
65 |
throw new Exception("failed to delete " + zipFile); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
66 |
} finally { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
67 |
setProperty("useJavaUtilZip", prev); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
68 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
69 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
70 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
71 |
File writeTestFile(String name) throws IOException { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
72 |
File f = new File(name + ".java"); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
73 |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
74 |
out.println("class " + name + " { }"); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
75 |
out.close(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
76 |
return f; |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
77 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
78 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
79 |
void compileTestFile(File classDir, File file) { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
80 |
int rc = com.sun.tools.javac.Main.compile( |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
81 |
new String[] { "-d", classDir.getPath(), file.getPath() }); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
82 |
if (rc != 0) |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
83 |
throw new Error("compilation failed. rc=" + rc); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
84 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
85 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
86 |
File zip(File dir, File zipFile) throws IOException { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
87 |
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
88 |
for (File file: dir.listFiles()) { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
89 |
if (file.isFile()) { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
90 |
byte[] data = new byte[(int) file.length()]; |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
91 |
DataInputStream in = new DataInputStream(new FileInputStream(file)); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
92 |
in.readFully(data); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
93 |
in.close(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
94 |
zipOut.putNextEntry(new ZipEntry(file.getName())); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
95 |
zipOut.write(data, 0, data.length); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
96 |
zipOut.closeEntry(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
97 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
98 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
99 |
zipOut.close(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
100 |
return zipFile; |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
101 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
102 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
103 |
String javap(String... args) { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
104 |
StringWriter sw = new StringWriter(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
105 |
PrintWriter out = new PrintWriter(sw); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
106 |
int rc = com.sun.tools.javap.Main.run(args, out); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
107 |
if (rc != 0) |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
108 |
throw new Error("javap failed. rc=" + rc); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
109 |
out.close(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
110 |
return sw.toString(); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
111 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
112 |
|
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
113 |
void setProperty(String key, String value) { |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
114 |
if (value != null) |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
115 |
System.setProperty(key, value); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
116 |
else |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
117 |
System.getProperties().remove(key); |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
118 |
} |
182e4a28c0ce
6879371: javap does not close internal default file manager
jjg
parents:
diff
changeset
|
119 |
} |