Path: blob/master/test/langtools/tools/javap/T6879371.java
41144 views
/*1* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 687937126* @summary javap does not close internal default file manager27* @modules jdk.jdeps/com.sun.tools.javap28*/2930import java.io.*;31import java.util.zip.*;3233public class T6879371 {34public static void main(String[] args) throws Exception {35new T6879371().run();36}3738public void run() throws Exception {39// create a simple test class which we can put into40// a test zip file and know that it will be used by41// javap.42File classDir = new File("classes");43classDir.mkdir();4445String className = "Test";46File javaFile = writeTestFile(className);47compileTestFile(classDir, javaFile);4849test(classDir, className, false);50test(classDir, className, true);51}5253void test(File classDir, String className, boolean useJavaUtilZip) throws Exception {54// javac should really not be using system properties like this55// -- it should really be using (hidden) options -- but until then56// take care to leave system properties as we find them, so as not57// to adversely affect other tests that might follow.58String prev = System.getProperty("useJavaUtilZip");59setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null));60try {61File zipFile = zip(classDir, new File(classDir + ".zip"));62javap("-classpath", zipFile.getPath(), className);6364if (!zipFile.delete())65throw new Exception("failed to delete " + zipFile);66} finally {67setProperty("useJavaUtilZip", prev);68}69}7071File writeTestFile(String name) throws IOException {72File f = new File(name + ".java");73PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));74out.println("class " + name + " { }");75out.close();76return f;77}7879void compileTestFile(File classDir, File file) {80int rc = com.sun.tools.javac.Main.compile(81new String[] { "-d", classDir.getPath(), file.getPath() });82if (rc != 0)83throw new Error("compilation failed. rc=" + rc);84}8586File zip(File dir, File zipFile) throws IOException {87ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));88for (File file: dir.listFiles()) {89if (file.isFile()) {90byte[] data = new byte[(int) file.length()];91DataInputStream in = new DataInputStream(new FileInputStream(file));92in.readFully(data);93in.close();94zipOut.putNextEntry(new ZipEntry(file.getName()));95zipOut.write(data, 0, data.length);96zipOut.closeEntry();97}98}99zipOut.close();100return zipFile;101}102103String javap(String... args) {104StringWriter sw = new StringWriter();105PrintWriter out = new PrintWriter(sw);106int rc = com.sun.tools.javap.Main.run(args, out);107if (rc != 0)108throw new Error("javap failed. rc=" + rc);109out.close();110return sw.toString();111}112113void setProperty(String key, String value) {114if (value != null)115System.setProperty(key, value);116else117System.getProperties().remove(key);118}119}120121122