test/tools/javac/T6558476.java

Wed, 12 Aug 2009 10:34:13 -0700

author
jjg
date
Wed, 12 Aug 2009 10:34:13 -0700
changeset 372
7dbb79875a63
child 554
9d9f26857129
permissions
-rw-r--r--

6558476: com/sun/tools/javac/Main.compile don't release file handles on return
Reviewed-by: darcy

jjg@372 1 /*
jjg@372 2 * Copyright 2008 Sun Microsystems, Inc. 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 *
jjg@372 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@372 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@372 21 * have any questions.
jjg@372 22 */
jjg@372 23
jjg@372 24 /*
jjg@372 25 * @test
jjg@372 26 * @run main/othervm -Xmx512m -Xms512m T6558476
jjg@372 27 */
jjg@372 28
jjg@372 29 import java.io.File;
jjg@372 30 import java.io.FileInputStream;
jjg@372 31 import java.io.FileOutputStream;
jjg@372 32 import java.io.IOException;
jjg@372 33 import java.util.Random;
jjg@372 34
jjg@372 35 import com.sun.tools.javac.Main;
jjg@372 36
jjg@372 37 public class T6558476 {
jjg@372 38 private static File copyFileTo(File file, File directory) throws IOException {
jjg@372 39 File newFile = new File(directory, file.getName());
jjg@372 40 FileInputStream fis = null;
jjg@372 41 FileOutputStream fos = null;
jjg@372 42 try {
jjg@372 43 fis = new FileInputStream(file);
jjg@372 44 fos = new FileOutputStream(newFile);
jjg@372 45 byte buff[] = new byte[1024];
jjg@372 46 int val;
jjg@372 47 while ((val = fis.read(buff)) > 0)
jjg@372 48 fos.write(buff, 0, val);
jjg@372 49 } finally {
jjg@372 50 if (fis != null)
jjg@372 51 fis.close();
jjg@372 52 if (fos != null)
jjg@372 53 fos.close();
jjg@372 54 }
jjg@372 55 return newFile;
jjg@372 56 }
jjg@372 57
jjg@372 58 private static String generateJavaClass(String className) {
jjg@372 59 StringBuffer sb = new StringBuffer();
jjg@372 60 sb.append("import sun.net.spi.nameservice.dns.DNSNameService;\n");
jjg@372 61 sb.append("public class ");
jjg@372 62 sb.append(className);
jjg@372 63 sb.append(" {\n");
jjg@372 64 sb.append(" public void doStuff() {\n");
jjg@372 65 sb.append(" DNSNameService dns = null;\n");
jjg@372 66 sb.append(" }\n");
jjg@372 67 sb.append("}\n");
jjg@372 68 return sb.toString();
jjg@372 69 }
jjg@372 70
jjg@372 71 public static void main(String[] args) throws IOException {
jjg@372 72 File javaHomeDir = new File(System.getProperty("java.home"));
jjg@372 73 File tmpDir = new File(System.getProperty("java.io.tmpdir"));
jjg@372 74 File outputDir = new File(tmpDir, "outputDir" + new Random().nextInt(65536));
jjg@372 75 outputDir.mkdir();
jjg@372 76 outputDir.deleteOnExit();
jjg@372 77
jjg@372 78 File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar");
jjg@372 79 File tmpJar = copyFileTo(dnsjarfile, outputDir);
jjg@372 80 String className = "TheJavaFile";
jjg@372 81 File javaFile = new File(outputDir, className + ".java");
jjg@372 82 javaFile.deleteOnExit();
jjg@372 83 FileOutputStream fos = new FileOutputStream(javaFile);
jjg@372 84 fos.write(generateJavaClass(className).getBytes());
jjg@372 85 fos.close();
jjg@372 86
jjg@372 87 int rc = Main.compile(new String[]{"-d", outputDir.getPath(),
jjg@372 88 "-classpath",
jjg@372 89 tmpJar.getPath(),
jjg@372 90 javaFile.getAbsolutePath()});
jjg@372 91 if (rc != 0) {
jjg@372 92 throw new Error("Couldn't compile the file (exit code=" + rc + ")");
jjg@372 93 }
jjg@372 94
jjg@372 95 if (tmpJar.delete()) {
jjg@372 96 System.out.println("jar file successfully deleted");
jjg@372 97 } else {
jjg@372 98 throw new Error("Error deleting file \"" + tmpJar.getPath() + "\"");
jjg@372 99 }
jjg@372 100 }
jjg@372 101 }

mercurial