T6627362.java revision 3294:9adfb22ff08f
190075Sobrien/*
290075Sobrien * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
390075Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490075Sobrien *
590075Sobrien * This code is free software; you can redistribute it and/or modify it
690075Sobrien * under the terms of the GNU General Public License version 2 only, as
790075Sobrien * published by the Free Software Foundation.
890075Sobrien *
990075Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1090075Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190075Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1290075Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1390075Sobrien * accompanied this code).
1490075Sobrien *
1590075Sobrien * You should have received a copy of the GNU General Public License version
1690075Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1790075Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1890075Sobrien *
1990075Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2090075Sobrien * or visit www.oracle.com if you need additional information or have any
2190075Sobrien * questions.
2290075Sobrien */
2390075Sobrien
2490075Sobrien/*
2590075Sobrien * @test
2690075Sobrien * @bug 6627362
2790075Sobrien * @summary javac generates code that uses array.clone,
2890075Sobrien *          which is not available on JavaCard
2990075Sobrien * @modules jdk.compiler
3090075Sobrien *          jdk.jdeps/com.sun.tools.javap
3190075Sobrien */
3290075Sobrien
3390075Sobrienimport java.io.*;
3490075Sobrienimport java.lang.reflect.*;
3590075Sobrienimport java.net.*;
3690075Sobrienimport java.util.*;
3790075Sobrien
3890075Sobrienpublic class T6627362 {
3990075Sobrien    static String testSrc = System.getProperty("test.src", ".");
4090075Sobrien
4190075Sobrien    public static void main(String... args) throws Exception {
4290075Sobrien        new T6627362().run();
4390075Sobrien    }
4490075Sobrien
4590075Sobrien    public void run() throws Exception {
4690075Sobrien        testStandard();
4790075Sobrien        testNoClone();
4890075Sobrien        if (errors > 0)
4990075Sobrien            throw new Error(errors + " test cases failed");
5090075Sobrien    }
5190075Sobrien
5290075Sobrien    void testStandard() throws Exception {
5390075Sobrien        // compile and disassemble E.java, check for reference to Object.clone()
5490075Sobrien        File x = new File(testSrc, "x");
5590075Sobrien        String[] jcArgs = { "-d", ".",
5690075Sobrien                            new File(x, "E.java").getPath() };
5790075Sobrien        compile(jcArgs);
5890075Sobrien
5990075Sobrien        String[] jpArgs = { "-classpath", ".", "-c", "E" };
6090075Sobrien
6190075Sobrien        StringWriter sw = new StringWriter();
6290075Sobrien        javap(new PrintWriter(sw, true), jpArgs);
6390075Sobrien        check(sw.toString(), "Method \"[LE;\".clone:()Ljava/lang/Object;");
6490075Sobrien        callValues();
6590075Sobrien    }
6690075Sobrien
6790075Sobrien    void testNoClone() throws Exception {
6890075Sobrien        // compile and disassemble E.java, using modified Object.java,
6990075Sobrien        // check for reference to System.arraycopy
7090075Sobrien        File x = new File(testSrc, "x");
7190075Sobrien        String[] jcArgs = { "-d", ".", "-Xmodule:java.base",
7290075Sobrien                            new File(x, "E.java").getPath(),
7390075Sobrien                            new File(x, "Object.java").getPath()};
7490075Sobrien        compile(jcArgs);
7590075Sobrien
7690075Sobrien        String[] jpArgs = { "-classpath", ".", "-c", "E" };
7790075Sobrien
7890075Sobrien        StringWriter sw = new StringWriter();
7990075Sobrien        javap(new PrintWriter(sw, true), jpArgs);
8090075Sobrien        check(sw.toString(), "// Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V");
8190075Sobrien        callValues();
8290075Sobrien    }
8390075Sobrien
8490075Sobrien    void compile(String... args) {
8590075Sobrien        int rc = com.sun.tools.javac.Main.compile(args);
8690075Sobrien        if (rc != 0)
8790075Sobrien            throw new Error("javac failed: " + Arrays.asList(args) + ": " + rc);
8890075Sobrien    }
8990075Sobrien
9090075Sobrien    void javap(PrintWriter out, String... args) throws Exception {
9190075Sobrien        int rc = com.sun.tools.javap.Main.run(args, out);
9290075Sobrien        if (rc != 0)
9390075Sobrien            throw new Error("javap failed: " + Arrays.asList(args) + ": " + rc);
9490075Sobrien    }
9590075Sobrien
9690075Sobrien    void check(String s, String require) {
9790075Sobrien        System.out.println("Checking:\n" + s);
9890075Sobrien        if (s.indexOf(require) == -1) {
9990075Sobrien            System.err.println("Can't find " + require);
10090075Sobrien            errors++;
10190075Sobrien        }
10290075Sobrien    }
10390075Sobrien
10490075Sobrien    void callValues() {
10590075Sobrien        try {
10690075Sobrien            File dot = new File(System.getProperty("user.dir"));
10790075Sobrien            ClassLoader cl = new URLClassLoader(new URL[] { dot.toURL() });
10890075Sobrien            Class<?> e_class = cl.loadClass("E");
10990075Sobrien            Method m = e_class.getMethod("values", new Class[] { });
11090075Sobrien            //System.err.println(m);
11190075Sobrien            Object o = m.invoke(null, (Object[]) null);
11290075Sobrien            List<Object> v = Arrays.asList((Object[]) o);
11390075Sobrien            if (!v.toString().equals("[a, b, c]"))
11490075Sobrien                throw new Error("unexpected result for E.values(): " + v);
11590075Sobrien        } catch (Exception e) {
11690075Sobrien            throw new Error(e);
11790075Sobrien        }
11890075Sobrien    }
11990075Sobrien
12090075Sobrien    int errors;
12190075Sobrien}
12290075Sobrien
12390075Sobrien