Mon, 07 Mar 2011 17:39:42 -0800
7021927: javac: regression in performance
Reviewed-by: jjg
ksrini@923 | 1 | /* |
ksrini@923 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
ksrini@923 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ksrini@923 | 4 | * |
ksrini@923 | 5 | * This code is free software; you can redistribute it and/or modify it |
ksrini@923 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ksrini@923 | 7 | * published by the Free Software Foundation. |
ksrini@923 | 8 | * |
ksrini@923 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ksrini@923 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ksrini@923 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ksrini@923 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ksrini@923 | 13 | * accompanied this code). |
ksrini@923 | 14 | * |
ksrini@923 | 15 | * You should have received a copy of the GNU General Public License version |
ksrini@923 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ksrini@923 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ksrini@923 | 18 | * |
ksrini@923 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ksrini@923 | 20 | * or visit www.oracle.com if you need additional information or have any |
ksrini@923 | 21 | * questions. |
ksrini@923 | 22 | */ |
ksrini@923 | 23 | |
ksrini@923 | 24 | import java.io.BufferedInputStream; |
ksrini@923 | 25 | import java.io.BufferedOutputStream; |
ksrini@923 | 26 | import java.io.Closeable; |
ksrini@923 | 27 | import java.io.File; |
ksrini@923 | 28 | import java.io.FileInputStream; |
ksrini@923 | 29 | import java.io.FileOutputStream; |
ksrini@923 | 30 | import java.io.IOException; |
ksrini@923 | 31 | import java.io.InputStream; |
ksrini@923 | 32 | import java.io.OutputStream; |
ksrini@923 | 33 | import java.io.PrintStream; |
ksrini@923 | 34 | |
ksrini@923 | 35 | public class Utils { |
ksrini@923 | 36 | |
ksrini@923 | 37 | static final sun.tools.jar.Main jarTool = |
ksrini@923 | 38 | new sun.tools.jar.Main(System.out, System.err, "jar-tool"); |
ksrini@923 | 39 | |
ksrini@923 | 40 | static final com.sun.tools.javac.Main javac = |
ksrini@923 | 41 | new com.sun.tools.javac.Main(); |
ksrini@923 | 42 | |
ksrini@923 | 43 | private Utils(){} |
ksrini@923 | 44 | |
ksrini@923 | 45 | public static boolean compile(String... args) { |
ksrini@923 | 46 | return javac.compile(args) == 0; |
ksrini@923 | 47 | } |
ksrini@923 | 48 | |
ksrini@923 | 49 | public static void createClassFile(File javaFile, File superClass, |
ksrini@923 | 50 | boolean delete) throws IOException { |
ksrini@923 | 51 | createJavaFile(javaFile, superClass); |
ksrini@923 | 52 | if (!compile(javaFile.getName())) { |
ksrini@923 | 53 | throw new RuntimeException("compile failed unexpectedly"); |
ksrini@923 | 54 | } |
ksrini@923 | 55 | if (delete) javaFile.delete(); |
ksrini@923 | 56 | } |
ksrini@923 | 57 | |
ksrini@923 | 58 | public static void createJavaFile(File outFile) throws IOException { |
ksrini@923 | 59 | createJavaFile(outFile, null); |
ksrini@923 | 60 | } |
ksrini@923 | 61 | |
ksrini@923 | 62 | public static void createJavaFile(File outFile, File superClass) throws IOException { |
ksrini@923 | 63 | PrintStream ps = null; |
ksrini@923 | 64 | String srcStr = "public class " + getSimpleName(outFile) + " "; |
ksrini@923 | 65 | if (superClass != null) { |
ksrini@923 | 66 | srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " "); |
ksrini@923 | 67 | } |
ksrini@923 | 68 | srcStr = srcStr.concat("{}"); |
ksrini@923 | 69 | try { |
ksrini@923 | 70 | FileOutputStream fos = new FileOutputStream(outFile); |
ksrini@923 | 71 | ps = new PrintStream(fos); |
ksrini@923 | 72 | ps.println(srcStr); |
ksrini@923 | 73 | } finally { |
ksrini@923 | 74 | close(ps); |
ksrini@923 | 75 | } |
ksrini@923 | 76 | } |
ksrini@923 | 77 | |
ksrini@923 | 78 | static String getClassFileName(File javaFile) { |
ksrini@923 | 79 | return javaFile.getName().endsWith(".java") |
ksrini@923 | 80 | ? javaFile.getName().replace(".java", ".class") |
ksrini@923 | 81 | : null; |
ksrini@923 | 82 | } |
ksrini@923 | 83 | |
ksrini@923 | 84 | static String getSimpleName(File inFile) { |
ksrini@923 | 85 | String fname = inFile.getName(); |
ksrini@923 | 86 | return fname.substring(0, fname.indexOf(".")); |
ksrini@923 | 87 | } |
ksrini@923 | 88 | |
ksrini@923 | 89 | public static void copyStream(InputStream in, OutputStream out) throws IOException { |
ksrini@923 | 90 | byte[] buf = new byte[8192]; |
ksrini@923 | 91 | int n = in.read(buf); |
ksrini@923 | 92 | while (n > 0) { |
ksrini@923 | 93 | out.write(buf, 0, n); |
ksrini@923 | 94 | n = in.read(buf); |
ksrini@923 | 95 | } |
ksrini@923 | 96 | } |
ksrini@923 | 97 | |
ksrini@923 | 98 | public static void close(Closeable c) { |
ksrini@923 | 99 | if (c != null) { |
ksrini@923 | 100 | try { |
ksrini@923 | 101 | c.close(); |
ksrini@923 | 102 | } catch (IOException ignore) {} |
ksrini@923 | 103 | } |
ksrini@923 | 104 | } |
ksrini@923 | 105 | |
ksrini@923 | 106 | public static void deleteFile(File f) { |
ksrini@923 | 107 | if (!f.delete()) { |
ksrini@923 | 108 | throw new RuntimeException("could not delete file: " + f.getAbsolutePath()); |
ksrini@923 | 109 | } |
ksrini@923 | 110 | } |
ksrini@923 | 111 | |
ksrini@923 | 112 | public static void cat(File output, File... files) throws IOException { |
ksrini@923 | 113 | BufferedInputStream bis = null; |
ksrini@923 | 114 | BufferedOutputStream bos = null; |
ksrini@923 | 115 | FileOutputStream fos = null; |
ksrini@923 | 116 | try { |
ksrini@923 | 117 | fos = new FileOutputStream(output); |
ksrini@923 | 118 | bos = new BufferedOutputStream(fos); |
ksrini@923 | 119 | for (File x : files) { |
ksrini@923 | 120 | FileInputStream fis = new FileInputStream(x); |
ksrini@923 | 121 | bis = new BufferedInputStream(fis); |
ksrini@923 | 122 | copyStream(bis, bos); |
ksrini@923 | 123 | Utils.close(bis); |
ksrini@923 | 124 | } |
ksrini@923 | 125 | } finally { |
ksrini@923 | 126 | Utils.close(bis); |
ksrini@923 | 127 | Utils.close(bos); |
ksrini@923 | 128 | Utils.close(fos); |
ksrini@923 | 129 | } |
ksrini@923 | 130 | } |
ksrini@923 | 131 | } |