test/tools/javac/6627362/T6627362.java

changeset 86
3437676858e3
child 117
24a47c3062fe
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/6627362/T6627362.java	Fri Aug 01 15:23:18 2008 -0700
     1.3 @@ -0,0 +1,133 @@
     1.4 +/*
     1.5 + * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6627362
    1.30 + * @summary javac generates code that uses array.clone,
    1.31 + *          which is not available on JavaCard
    1.32 + */
    1.33 +
    1.34 +import java.io.*;
    1.35 +import java.lang.reflect.*;
    1.36 +import java.net.*;
    1.37 +import java.util.*;
    1.38 +
    1.39 +public class T6627362 {
    1.40 +    static String testSrc = System.getProperty("test.src", ".");
    1.41 +
    1.42 +    public static void main(String... args) throws Exception {
    1.43 +        new T6627362().run();
    1.44 +    }
    1.45 +
    1.46 +    public void run() throws Exception {
    1.47 +        testStandard();
    1.48 +        testNoClone();
    1.49 +        if (errors > 0)
    1.50 +            throw new Error(errors + " test cases failed");
    1.51 +    }
    1.52 +
    1.53 +    void testStandard() throws Exception {
    1.54 +        // compile and disassemble E.java, check for reference to Object.clone()
    1.55 +        File x = new File(testSrc, "x");
    1.56 +        String[] jcArgs = { "-d", ".",
    1.57 +                            new File(x, "E.java").getPath() };
    1.58 +        compile(jcArgs);
    1.59 +
    1.60 +        String[] jpArgs = { "-classpath", ".", "-c", "E" };
    1.61 +
    1.62 +        StringWriter sw = new StringWriter();
    1.63 +        javap(new PrintWriter(sw, true), jpArgs);
    1.64 +        check(sw.toString(), "Method \"[LE;\".clone:()Ljava/lang/Object;");
    1.65 +        callValues();
    1.66 +    }
    1.67 +
    1.68 +    void testNoClone() throws Exception {
    1.69 +        // compile and disassemble E.java, using modified Object.java,
    1.70 +        // check for reference to System.arraycopy
    1.71 +        File x = new File(testSrc, "x");
    1.72 +        String[] jcArgs = { "-d", ".",
    1.73 +                            new File(x, "E.java").getPath(),
    1.74 +                            new File(x, "Object.java").getPath()};
    1.75 +        compile(jcArgs);
    1.76 +
    1.77 +        String[] jpArgs = { "-classpath", ".", "-c", "E" };
    1.78 +
    1.79 +        StringWriter sw = new StringWriter();
    1.80 +        javap(new PrintWriter(sw, true), jpArgs);
    1.81 +        check(sw.toString(), "//Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V");
    1.82 +        callValues();
    1.83 +    }
    1.84 +
    1.85 +    void compile(String... args) {
    1.86 +        int rc = com.sun.tools.javac.Main.compile(args);
    1.87 +        if (rc != 0)
    1.88 +            throw new Error("javac failed: " + Arrays.asList(args) + ": " + rc);
    1.89 +    }
    1.90 +
    1.91 +    void javap(PrintWriter out, String... args) throws Exception {
    1.92 +        // for now, we have to exec javap
    1.93 +        File javaHome = new File(System.getProperty("java.home"));
    1.94 +        if (javaHome.getName().equals("jre"))
    1.95 +            javaHome = javaHome.getParentFile();
    1.96 +        File javap = new File(new File(javaHome, "bin"), "javap");
    1.97 +        String[] cmd = new String[args.length + 1];
    1.98 +        cmd[0] = javap.getPath();
    1.99 +        System.arraycopy(args, 0, cmd, 1, args.length);
   1.100 +        Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
   1.101 +        p.getOutputStream().close();
   1.102 +        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   1.103 +        String line;
   1.104 +        while ((line = in.readLine()) != null)
   1.105 +            out.println(line);
   1.106 +        int rc = p.waitFor();
   1.107 +        if (rc != 0)
   1.108 +            throw new Error("javap failed: " + Arrays.asList(args) + ": " + rc);
   1.109 +    }
   1.110 +
   1.111 +    void check(String s, String require) {
   1.112 +        if (s.indexOf(require) == -1) {
   1.113 +            System.err.println("Can't find " + require);
   1.114 +            errors++;
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    void callValues() {
   1.119 +        try {
   1.120 +            File dot = new File(System.getProperty("user.dir"));
   1.121 +            ClassLoader cl = new URLClassLoader(new URL[] { dot.toURL() });
   1.122 +            Class<?> e_class = cl.loadClass("E");
   1.123 +            Method m = e_class.getMethod("values", new Class[] { });
   1.124 +            //System.err.println(m);
   1.125 +            Object o = m.invoke(null, (Object[]) null);
   1.126 +            List<Object> v = Arrays.asList((Object[]) o);
   1.127 +            if (!v.toString().equals("[a, b, c]"))
   1.128 +                throw new Error("unexpected result for E.values(): " + v);
   1.129 +        } catch (Exception e) {
   1.130 +            throw new Error(e);
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    int errors;
   1.135 +}
   1.136 +

mercurial