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
1 /*
2 * Copyright 2007-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
24 /*
25 * @test
26 * @bug 6627362
27 * @summary javac generates code that uses array.clone,
28 * which is not available on JavaCard
29 */
31 import java.io.*;
32 import java.lang.reflect.*;
33 import java.net.*;
34 import java.util.*;
36 public class T6627362 {
37 static String testSrc = System.getProperty("test.src", ".");
39 public static void main(String... args) throws Exception {
40 new T6627362().run();
41 }
43 public void run() throws Exception {
44 testStandard();
45 testNoClone();
46 if (errors > 0)
47 throw new Error(errors + " test cases failed");
48 }
50 void testStandard() throws Exception {
51 // compile and disassemble E.java, check for reference to Object.clone()
52 File x = new File(testSrc, "x");
53 String[] jcArgs = { "-d", ".",
54 new File(x, "E.java").getPath() };
55 compile(jcArgs);
57 String[] jpArgs = { "-classpath", ".", "-c", "E" };
59 StringWriter sw = new StringWriter();
60 javap(new PrintWriter(sw, true), jpArgs);
61 check(sw.toString(), "Method \"[LE;\".clone:()Ljava/lang/Object;");
62 callValues();
63 }
65 void testNoClone() throws Exception {
66 // compile and disassemble E.java, using modified Object.java,
67 // check for reference to System.arraycopy
68 File x = new File(testSrc, "x");
69 String[] jcArgs = { "-d", ".",
70 new File(x, "E.java").getPath(),
71 new File(x, "Object.java").getPath()};
72 compile(jcArgs);
74 String[] jpArgs = { "-classpath", ".", "-c", "E" };
76 StringWriter sw = new StringWriter();
77 javap(new PrintWriter(sw, true), jpArgs);
78 check(sw.toString(), "//Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V");
79 callValues();
80 }
82 void compile(String... args) {
83 int rc = com.sun.tools.javac.Main.compile(args);
84 if (rc != 0)
85 throw new Error("javac failed: " + Arrays.asList(args) + ": " + rc);
86 }
88 void javap(PrintWriter out, String... args) throws Exception {
89 // for now, we have to exec javap
90 File javaHome = new File(System.getProperty("java.home"));
91 if (javaHome.getName().equals("jre"))
92 javaHome = javaHome.getParentFile();
93 File javap = new File(new File(javaHome, "bin"), "javap");
94 String[] cmd = new String[args.length + 1];
95 cmd[0] = javap.getPath();
96 System.arraycopy(args, 0, cmd, 1, args.length);
97 Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
98 p.getOutputStream().close();
99 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
100 String line;
101 while ((line = in.readLine()) != null)
102 out.println(line);
103 int rc = p.waitFor();
104 if (rc != 0)
105 throw new Error("javap failed: " + Arrays.asList(args) + ": " + rc);
106 }
108 void check(String s, String require) {
109 if (s.indexOf(require) == -1) {
110 System.err.println("Can't find " + require);
111 errors++;
112 }
113 }
115 void callValues() {
116 try {
117 File dot = new File(System.getProperty("user.dir"));
118 ClassLoader cl = new URLClassLoader(new URL[] { dot.toURL() });
119 Class<?> e_class = cl.loadClass("E");
120 Method m = e_class.getMethod("values", new Class[] { });
121 //System.err.println(m);
122 Object o = m.invoke(null, (Object[]) null);
123 List<Object> v = Arrays.asList((Object[]) o);
124 if (!v.toString().equals("[a, b, c]"))
125 throw new Error("unexpected result for E.values(): " + v);
126 } catch (Exception e) {
127 throw new Error(e);
128 }
129 }
131 int errors;
132 }