test/tools/javac/T6558476.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 554
9d9f26857129
child 1669
d3648557391b
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

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

mercurial