Thu, 30 Jul 2009 10:29:53 +0100
6827648: Extremely slow compilation time for visitor pattern code + generics
Summary: Javac unnecessarily recomputates type-substitutions multiple times
Reviewed-by: jjg
jjg@86 | 1 | /* |
xdono@117 | 2 | * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@86 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@86 | 4 | * |
jjg@86 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@86 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@86 | 7 | * published by the Free Software Foundation. |
jjg@86 | 8 | * |
jjg@86 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@86 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@86 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@86 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@86 | 13 | * accompanied this code). |
jjg@86 | 14 | * |
jjg@86 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@86 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@86 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@86 | 18 | * |
jjg@86 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@86 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@86 | 21 | * have any questions. |
jjg@86 | 22 | */ |
jjg@86 | 23 | |
jjg@86 | 24 | /* |
jjg@86 | 25 | * @test |
jjg@86 | 26 | * @bug 6627362 |
jjg@86 | 27 | * @summary javac generates code that uses array.clone, |
jjg@86 | 28 | * which is not available on JavaCard |
jjg@86 | 29 | */ |
jjg@86 | 30 | |
jjg@86 | 31 | import java.io.*; |
jjg@86 | 32 | import java.lang.reflect.*; |
jjg@86 | 33 | import java.net.*; |
jjg@86 | 34 | import java.util.*; |
jjg@86 | 35 | |
jjg@86 | 36 | public class T6627362 { |
jjg@86 | 37 | static String testSrc = System.getProperty("test.src", "."); |
jjg@86 | 38 | |
jjg@86 | 39 | public static void main(String... args) throws Exception { |
jjg@86 | 40 | new T6627362().run(); |
jjg@86 | 41 | } |
jjg@86 | 42 | |
jjg@86 | 43 | public void run() throws Exception { |
jjg@86 | 44 | testStandard(); |
jjg@86 | 45 | testNoClone(); |
jjg@86 | 46 | if (errors > 0) |
jjg@86 | 47 | throw new Error(errors + " test cases failed"); |
jjg@86 | 48 | } |
jjg@86 | 49 | |
jjg@86 | 50 | void testStandard() throws Exception { |
jjg@86 | 51 | // compile and disassemble E.java, check for reference to Object.clone() |
jjg@86 | 52 | File x = new File(testSrc, "x"); |
jjg@86 | 53 | String[] jcArgs = { "-d", ".", |
jjg@86 | 54 | new File(x, "E.java").getPath() }; |
jjg@86 | 55 | compile(jcArgs); |
jjg@86 | 56 | |
jjg@86 | 57 | String[] jpArgs = { "-classpath", ".", "-c", "E" }; |
jjg@86 | 58 | |
jjg@86 | 59 | StringWriter sw = new StringWriter(); |
jjg@86 | 60 | javap(new PrintWriter(sw, true), jpArgs); |
jjg@86 | 61 | check(sw.toString(), "Method \"[LE;\".clone:()Ljava/lang/Object;"); |
jjg@86 | 62 | callValues(); |
jjg@86 | 63 | } |
jjg@86 | 64 | |
jjg@86 | 65 | void testNoClone() throws Exception { |
jjg@86 | 66 | // compile and disassemble E.java, using modified Object.java, |
jjg@86 | 67 | // check for reference to System.arraycopy |
jjg@86 | 68 | File x = new File(testSrc, "x"); |
jjg@86 | 69 | String[] jcArgs = { "-d", ".", |
jjg@86 | 70 | new File(x, "E.java").getPath(), |
jjg@86 | 71 | new File(x, "Object.java").getPath()}; |
jjg@86 | 72 | compile(jcArgs); |
jjg@86 | 73 | |
jjg@86 | 74 | String[] jpArgs = { "-classpath", ".", "-c", "E" }; |
jjg@86 | 75 | |
jjg@86 | 76 | StringWriter sw = new StringWriter(); |
jjg@86 | 77 | javap(new PrintWriter(sw, true), jpArgs); |
jjg@86 | 78 | check(sw.toString(), "//Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V"); |
jjg@86 | 79 | callValues(); |
jjg@86 | 80 | } |
jjg@86 | 81 | |
jjg@86 | 82 | void compile(String... args) { |
jjg@86 | 83 | int rc = com.sun.tools.javac.Main.compile(args); |
jjg@86 | 84 | if (rc != 0) |
jjg@86 | 85 | throw new Error("javac failed: " + Arrays.asList(args) + ": " + rc); |
jjg@86 | 86 | } |
jjg@86 | 87 | |
jjg@86 | 88 | void javap(PrintWriter out, String... args) throws Exception { |
jjg@86 | 89 | // for now, we have to exec javap |
jjg@86 | 90 | File javaHome = new File(System.getProperty("java.home")); |
jjg@86 | 91 | if (javaHome.getName().equals("jre")) |
jjg@86 | 92 | javaHome = javaHome.getParentFile(); |
jjg@86 | 93 | File javap = new File(new File(javaHome, "bin"), "javap"); |
jjg@86 | 94 | String[] cmd = new String[args.length + 1]; |
jjg@86 | 95 | cmd[0] = javap.getPath(); |
jjg@86 | 96 | System.arraycopy(args, 0, cmd, 1, args.length); |
jjg@86 | 97 | Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start(); |
jjg@86 | 98 | p.getOutputStream().close(); |
jjg@86 | 99 | BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
jjg@86 | 100 | String line; |
jjg@86 | 101 | while ((line = in.readLine()) != null) |
jjg@86 | 102 | out.println(line); |
jjg@86 | 103 | int rc = p.waitFor(); |
jjg@86 | 104 | if (rc != 0) |
jjg@86 | 105 | throw new Error("javap failed: " + Arrays.asList(args) + ": " + rc); |
jjg@86 | 106 | } |
jjg@86 | 107 | |
jjg@86 | 108 | void check(String s, String require) { |
jjg@86 | 109 | if (s.indexOf(require) == -1) { |
jjg@86 | 110 | System.err.println("Can't find " + require); |
jjg@86 | 111 | errors++; |
jjg@86 | 112 | } |
jjg@86 | 113 | } |
jjg@86 | 114 | |
jjg@86 | 115 | void callValues() { |
jjg@86 | 116 | try { |
jjg@86 | 117 | File dot = new File(System.getProperty("user.dir")); |
jjg@86 | 118 | ClassLoader cl = new URLClassLoader(new URL[] { dot.toURL() }); |
jjg@86 | 119 | Class<?> e_class = cl.loadClass("E"); |
jjg@86 | 120 | Method m = e_class.getMethod("values", new Class[] { }); |
jjg@86 | 121 | //System.err.println(m); |
jjg@86 | 122 | Object o = m.invoke(null, (Object[]) null); |
jjg@86 | 123 | List<Object> v = Arrays.asList((Object[]) o); |
jjg@86 | 124 | if (!v.toString().equals("[a, b, c]")) |
jjg@86 | 125 | throw new Error("unexpected result for E.values(): " + v); |
jjg@86 | 126 | } catch (Exception e) { |
jjg@86 | 127 | throw new Error(e); |
jjg@86 | 128 | } |
jjg@86 | 129 | } |
jjg@86 | 130 | |
jjg@86 | 131 | int errors; |
jjg@86 | 132 | } |
jjg@86 | 133 |