1 /* 2 * Copyright (c) 2016, 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 * @bug 8058575 27 * @summary Creates an anonymous class inside of an anonymous class. 28 * @library /test/lib 29 * @modules java.base/jdk.internal.misc 30 * java.compiler 31 * java.management 32 * @run main p.NestedUnsafe2 33 */ 34 35 package p; 36 37 import java.security.ProtectionDomain; 38 import java.io.InputStream; 39 import java.lang.*; 40 import jdk.test.lib.*; 41 import jdk.internal.misc.Unsafe; 42 43 44 // Test that an anonymous class that gets put in its host's package cannot define 45 // an anonymous class in another package. 46 public class NestedUnsafe2 { 47 // The String concatenation should create the nested anonymous class. 48 public static byte klassbuf[] = InMemoryJavaCompiler.compile("q.TestClass", 49 "package q; " + 50 "public class TestClass { " + 51 " public static void concat(String one, String two) throws Throwable { " + 52 " System.out.println(one + two);" + 53 " } } "); 54 55 public static void main(String args[]) throws Exception { 56 Unsafe unsafe = Unsafe.getUnsafe(); 57 58 // The anonymous class calls defineAnonymousClass creating a nested anonymous class. 59 byte klassbuf2[] = InMemoryJavaCompiler.compile("TestClass2", 60 "import jdk.internal.misc.Unsafe; " + 61 "public class TestClass2 { " + 62 " public static void doit() throws Throwable { " + 63 " Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); " + 64 " Class klass2 = unsafe.defineAnonymousClass(TestClass2.class, p.NestedUnsafe2.klassbuf, new Object[0]); " + 65 " unsafe.ensureClassInitialized(klass2); " + 66 " Class[] dArgs = new Class[2]; " + 67 " dArgs[0] = String.class; " + 68 " dArgs[1] = String.class; " + 69 " try { " + 70 " klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " + 71 " } catch (Throwable ex) { " + 72 " throw new RuntimeException(\"Exception: \" + ex.toString()); " + 73 " } " + 74 "} } ", 75 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED"); 76 77 Class klass2 = unsafe.defineAnonymousClass(p.NestedUnsafe2.class, klassbuf2, new Object[0]); 78 try { 79 klass2.getMethod("doit").invoke(null); 80 throw new RuntimeException("Expected exception not thrown"); 81 } catch (Throwable ex) { 82 Throwable iae = ex.getCause(); 83 if (!iae.toString().contains( 84 "IllegalArgumentException: Host class p/NestedUnsafe2 and anonymous class q/TestClass")) { 85 throw new RuntimeException("Exception: " + iae.toString()); 86 } 87 } 88 } 89 }