Path: blob/master/test/langtools/tools/javac/6627362/T6627362.java
41149 views
/*1* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 662736226* @summary javac generates code that uses array.clone,27* which is not available on JavaCard28* @modules jdk.compiler29* jdk.jdeps/com.sun.tools.javap30*/3132import java.io.*;33import java.lang.reflect.*;34import java.net.*;35import java.util.*;3637public class T6627362 {38static String testSrc = System.getProperty("test.src", ".");3940public static void main(String... args) throws Exception {41new T6627362().run();42}4344public void run() throws Exception {45testStandard();46testNoClone();47if (errors > 0)48throw new Error(errors + " test cases failed");49}5051void testStandard() throws Exception {52// compile and disassemble E.java, check for reference to Object.clone()53File x = new File(testSrc, "x");54String[] jcArgs = { "-d", ".",55new File(x, "E.java").getPath() };56compile(jcArgs);5758String[] jpArgs = { "-classpath", ".", "-c", "E" };5960StringWriter sw = new StringWriter();61javap(new PrintWriter(sw, true), jpArgs);62check(sw.toString(), "Method \"[LE;\".clone:()Ljava/lang/Object;");63callValues();64}6566void testNoClone() throws Exception {67// compile and disassemble E.java, using modified Object.java,68// check for reference to System.arraycopy69File x = new File(testSrc, "x");70String[] jcArgs = { "-d", ".", "--patch-module", "java.base=" + x.getAbsolutePath(),71new File(x, "E.java").getPath(),72new File(new File(new File(x, "java"), "lang"), "Object.java").getPath()};73compile(jcArgs);7475String[] jpArgs = { "-classpath", ".", "-c", "E" };7677StringWriter sw = new StringWriter();78javap(new PrintWriter(sw, true), jpArgs);79check(sw.toString(), "// Method java/lang/System.arraycopy:(Ljava/lang/Object;ILjava/lang/Object;II)V");80callValues();81}8283void compile(String... args) {84int rc = com.sun.tools.javac.Main.compile(args);85if (rc != 0)86throw new Error("javac failed: " + Arrays.asList(args) + ": " + rc);87}8889void javap(PrintWriter out, String... args) throws Exception {90int rc = com.sun.tools.javap.Main.run(args, out);91if (rc != 0)92throw new Error("javap failed: " + Arrays.asList(args) + ": " + rc);93}9495void check(String s, String require) {96System.out.println("Checking:\n" + s);97if (s.indexOf(require) == -1) {98System.err.println("Can't find " + require);99errors++;100}101}102103void callValues() {104try {105File dot = new File(System.getProperty("user.dir"));106ClassLoader cl = new URLClassLoader(new URL[] { dot.toURL() });107Class<?> e_class = cl.loadClass("E");108Method m = e_class.getMethod("values", new Class[] { });109//System.err.println(m);110Object o = m.invoke(null, (Object[]) null);111List<Object> v = Arrays.asList((Object[]) o);112if (!v.toString().equals("[a, b, c]"))113throw new Error("unexpected result for E.values(): " + v);114} catch (Exception e) {115throw new Error(e);116}117}118119int errors;120}121122123124