test/tools/javac/T6558476.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1669
d3648557391b
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

jjg@372 1 /*
jjg@1669 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@372 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@372 4 *
jjg@372 5 * This code is free software; you can redistribute it and/or modify it
jjg@372 6 * under the terms of the GNU General Public License version 2 only, as
jjg@372 7 * published by the Free Software Foundation.
jjg@372 8 *
jjg@372 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@372 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@372 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@372 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@372 13 * accompanied this code).
jjg@372 14 *
jjg@372 15 * You should have received a copy of the GNU General Public License version
jjg@372 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@372 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@372 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@372 22 */
jjg@372 23
jjg@372 24 /*
jjg@372 25 * @test
jjg@1669 26 * @bug 6558476
jjg@1669 27 * @summary com/sun/tools/javac/Main.compile don't release file handles on return
jjg@372 28 * @run main/othervm -Xmx512m -Xms512m T6558476
jjg@372 29 */
jjg@372 30
jjg@372 31 import java.io.File;
jjg@372 32 import java.io.FileInputStream;
jjg@372 33 import java.io.FileOutputStream;
jjg@372 34 import java.io.IOException;
jjg@372 35 import java.util.Random;
jjg@372 36
jjg@372 37 import com.sun.tools.javac.Main;
jjg@372 38
jjg@372 39 public class T6558476 {
jjg@372 40 private static File copyFileTo(File file, File directory) throws IOException {
jjg@372 41 File newFile = new File(directory, file.getName());
jjg@372 42 FileInputStream fis = null;
jjg@372 43 FileOutputStream fos = null;
jjg@372 44 try {
jjg@372 45 fis = new FileInputStream(file);
jjg@372 46 fos = new FileOutputStream(newFile);
jjg@372 47 byte buff[] = new byte[1024];
jjg@372 48 int val;
jjg@372 49 while ((val = fis.read(buff)) > 0)
jjg@372 50 fos.write(buff, 0, val);
jjg@372 51 } finally {
jjg@372 52 if (fis != null)
jjg@372 53 fis.close();
jjg@372 54 if (fos != null)
jjg@372 55 fos.close();
jjg@372 56 }
jjg@372 57 return newFile;
jjg@372 58 }
jjg@372 59
jjg@372 60 private static String generateJavaClass(String className) {
jjg@372 61 StringBuffer sb = new StringBuffer();
jjg@372 62 sb.append("import sun.net.spi.nameservice.dns.DNSNameService;\n");
jjg@372 63 sb.append("public class ");
jjg@372 64 sb.append(className);
jjg@372 65 sb.append(" {\n");
jjg@372 66 sb.append(" public void doStuff() {\n");
jjg@372 67 sb.append(" DNSNameService dns = null;\n");
jjg@372 68 sb.append(" }\n");
jjg@372 69 sb.append("}\n");
jjg@372 70 return sb.toString();
jjg@372 71 }
jjg@372 72
jjg@372 73 public static void main(String[] args) throws IOException {
jjg@372 74 File javaHomeDir = new File(System.getProperty("java.home"));
jjg@1669 75 File outputDir = new File("outputDir" + new Random().nextInt(65536));
jjg@372 76 outputDir.mkdir();
jjg@372 77 outputDir.deleteOnExit();
jjg@372 78
jjg@372 79 File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar");
jjg@372 80 File tmpJar = copyFileTo(dnsjarfile, outputDir);
jjg@372 81 String className = "TheJavaFile";
jjg@372 82 File javaFile = new File(outputDir, className + ".java");
jjg@372 83 javaFile.deleteOnExit();
jjg@372 84 FileOutputStream fos = new FileOutputStream(javaFile);
jjg@372 85 fos.write(generateJavaClass(className).getBytes());
jjg@372 86 fos.close();
jjg@372 87
jjg@372 88 int rc = Main.compile(new String[]{"-d", outputDir.getPath(),
jjg@372 89 "-classpath",
jjg@372 90 tmpJar.getPath(),
jjg@372 91 javaFile.getAbsolutePath()});
jjg@372 92 if (rc != 0) {
jjg@372 93 throw new Error("Couldn't compile the file (exit code=" + rc + ")");
jjg@372 94 }
jjg@372 95
jjg@372 96 if (tmpJar.delete()) {
jjg@372 97 System.out.println("jar file successfully deleted");
jjg@372 98 } else {
jjg@372 99 throw new Error("Error deleting file \"" + tmpJar.getPath() + "\"");
jjg@372 100 }
jjg@372 101 }
jjg@372 102 }

mercurial