Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/T6879371.java
32285 views
/*1* Copyright (c) 2009, 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*/2829import java.io.*;30import java.util.zip.*;3132public class T6879371 {33public static void main(String[] args) throws Exception {34new T6879371().run();35}3637public void run() throws Exception {38// create a simple test class which we can put into39// a test zip file and know that it will be used by40// javap.41File classDir = new File("classes");42classDir.mkdir();4344String className = "Test";45File javaFile = writeTestFile(className);46compileTestFile(classDir, javaFile);4748test(classDir, className, false);49test(classDir, className, true);50}5152void test(File classDir, String className, boolean useJavaUtilZip) throws Exception {53// javac should really not be using system properties like this54// -- it should really be using (hidden) options -- but until then55// take care to leave system properties as we find them, so as not56// to adversely affect other tests that might follow.57String prev = System.getProperty("useJavaUtilZip");58setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null));59try {60File zipFile = zip(classDir, new File(classDir + ".zip"));61javap("-classpath", zipFile.getPath(), className);6263if (!zipFile.delete())64throw new Exception("failed to delete " + zipFile);65} finally {66setProperty("useJavaUtilZip", prev);67}68}6970File writeTestFile(String name) throws IOException {71File f = new File(name + ".java");72PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));73out.println("class " + name + " { }");74out.close();75return f;76}7778void compileTestFile(File classDir, File file) {79int rc = com.sun.tools.javac.Main.compile(80new String[] { "-d", classDir.getPath(), file.getPath() });81if (rc != 0)82throw new Error("compilation failed. rc=" + rc);83}8485File zip(File dir, File zipFile) throws IOException {86ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));87for (File file: dir.listFiles()) {88if (file.isFile()) {89byte[] data = new byte[(int) file.length()];90DataInputStream in = new DataInputStream(new FileInputStream(file));91in.readFully(data);92in.close();93zipOut.putNextEntry(new ZipEntry(file.getName()));94zipOut.write(data, 0, data.length);95zipOut.closeEntry();96}97}98zipOut.close();99return zipFile;100}101102String javap(String... args) {103StringWriter sw = new StringWriter();104PrintWriter out = new PrintWriter(sw);105int rc = com.sun.tools.javap.Main.run(args, out);106if (rc != 0)107throw new Error("javap failed. rc=" + rc);108out.close();109return sw.toString();110}111112void setProperty(String key, String value) {113if (value != null)114System.setProperty(key, value);115else116System.getProperties().remove(key);117}118}119120121