T6627362.java revision 393:45301370bc5a
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 */
23
24/*
25 * @test
26 * @bug 6627362
27 * @summary javac generates code that uses array.clone,
28 *          which is not available on JavaCard
29 */
30
31import java.io.*;
32import java.lang.reflect.*;
33import java.net.*;
34import java.util.*;
35
36public class T6627362 {
37    static String testSrc = System.getProperty("test.src", ".");
38
39    public static void main(String... args) throws Exception {
40        new T6627362().run();
41    }
42
43    public void run() throws Exception {
44        testStandard();
45        testNoClone();
46        if (errors > 0)
47            throw new Error(errors + " test cases failed");
48    }
49
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);
56
57        String[] jpArgs = { "-classpath", ".", "-c", "E" };
58
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    }
64
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);
73
74        String[] jpArgs = { "-classpath", ".", "-c", "E" };
75
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    }
81
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    }
87
88    void javap(PrintWriter out, String... args) throws Exception {
89        int rc = com.sun.tools.javap.Main.run(args, out);
90        if (rc != 0)
91            throw new Error("javap failed: " + Arrays.asList(args) + ": " + rc);
92    }
93
94    void check(String s, String require) {
95        System.out.println("Checking:\n" + s);
96        if (s.indexOf(require) == -1) {
97            System.err.println("Can't find " + require);
98            errors++;
99        }
100    }
101
102    void callValues() {
103        try {
104            File dot = new File(System.getProperty("user.dir"));
105            ClassLoader cl = new URLClassLoader(new URL[] { dot.toURL() });
106            Class<?> e_class = cl.loadClass("E");
107            Method m = e_class.getMethod("values", new Class[] { });
108            //System.err.println(m);
109            Object o = m.invoke(null, (Object[]) null);
110            List<Object> v = Arrays.asList((Object[]) o);
111            if (!v.toString().equals("[a, b, c]"))
112                throw new Error("unexpected result for E.values(): " + v);
113        } catch (Exception e) {
114            throw new Error(e);
115        }
116    }
117
118    int errors;
119}
120
121