1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @summary Creates an anonymous class inside of an anonymous class. 27 * @library /testlibrary 28 * @modules java.base/jdk.internal.misc 29 * java.compiler 30 * java.management 31 * @run main NestedUnsafe 32 */ 33 34 import java.security.ProtectionDomain; 35 import java.io.InputStream; 36 import java.lang.*; 37 import jdk.test.lib.*; 38 import jdk.internal.misc.Unsafe; 39 import static jdk.test.lib.Asserts.*; 40 41 // package p; 42 43 public class NestedUnsafe { 44 // The String concatenation should create the nested anonymous class. 45 static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", 46 "public class TestClass { " + 47 " public static void concat(String one, String two) throws Throwable { " + 48 " System.out.println(one + two);" + 49 " } } "); 50 51 public static void main(String args[]) throws Exception { 52 Unsafe unsafe = Utils.getUnsafe(); 53 54 Class klass = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf, new Object[0]); 55 unsafe.ensureClassInitialized(klass); 56 Class[] cArgs = new Class[2]; 57 cArgs[0] = String.class; 58 cArgs[1] = String.class; 59 try { 60 klass.getMethod("concat", cArgs).invoke(null, "AA", "BB"); 61 } catch (Throwable ex) { 62 throw new RuntimeException("Exception: " + ex.toString()); 63 } 64 65 // The anonymous class calls defineAnonymousClass creating a nested anonymous class. 66 byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2", 67 "import jdk.internal.misc.Unsafe; " + 68 "public class TestClass2 { " + 69 " public static void doit() throws Throwable { " + 70 " Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " + 71 " Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, NestedUnsafe.klassbuf, new Object[0]); " + 72 " unsafe.ensureClassInitialized(klass2); " + 73 " Class[] dArgs = new Class[2]; " + 74 " dArgs[0] = String.class; " + 75 " dArgs[1] = String.class; " + 76 " try { " + 77 " klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " + 78 " } catch (Throwable ex) { " + 79 " throw new RuntimeException(\"Exception: \" + ex.toString()); " + 80 " } " + 81 "} } ", 82 "-XaddExports:java.base/jdk.internal.misc=ALL-UNNAMED"); 83 Class klass2 = unsafe.defineAnonymousClass(NestedUnsafe.class, klassbuf2, new Object[0]); 84 try { 85 klass2.getMethod("doit").invoke(null); 86 } catch (Throwable ex) { 87 throw new RuntimeException("Exception: " + ex.toString()); 88 } 89 } 90 }