1 /* 2 * Copyright (c) 2020, 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 a hidden class inside of a hidden class. 27 * @library /test/lib 28 * @modules jdk.compiler 29 * @run main p.NestedHidden 30 */ 31 32 package p; 33 34 import java.lang.*; 35 36 import java.lang.invoke.MethodHandles; 37 import java.lang.invoke.MethodHandles.Lookup; 38 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; 39 import jdk.test.lib.compiler.InMemoryJavaCompiler; 40 41 42 // Test that a hidden class can define its own hidden class by calling 43 // lookup.defineHiddenClass(). 44 public class NestedHidden { 45 static byte klassbuf[] = InMemoryJavaCompiler.compile("p.TestClass", 46 "package p; " + 47 "public class TestClass { " + 48 " public static void concat(String one, String two) throws Throwable { " + 49 " System.out.println(one + two);" + 50 " } } "); 51 52 public static void main(String args[]) throws Exception { 53 // The hidden class calls lookup.defineHiddenClass(), creating a nested hidden class. 54 byte klassbuf2[] = InMemoryJavaCompiler.compile("p.TestClass2", 55 "package p; " + 56 "import java.lang.invoke.MethodHandles; " + 57 "import java.lang.invoke.MethodHandles.Lookup; " + 58 "import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; " + 59 "public class TestClass2 { " + 60 " public static void doit() throws Throwable { " + 61 " Lookup lookup = MethodHandles.lookup(); " + 62 " Class<?> klass2 = lookup.defineHiddenClass(p.NestedHidden.klassbuf, true, NESTMATE).lookupClass(); " + 63 " Class[] dArgs = new Class[2]; " + 64 " dArgs[0] = String.class; " + 65 " dArgs[1] = String.class; " + 66 " try { " + 67 " klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " + 68 " } catch (Throwable ex) { " + 69 " throw new RuntimeException(\"Exception: \" + ex.toString()); " + 70 " } " + 71 "} } "); 72 73 Lookup lookup = MethodHandles.lookup(); 74 Class<?> klass2 = lookup.defineHiddenClass(klassbuf2, true, NESTMATE).lookupClass(); 75 klass2.getMethod("doit").invoke(null); 76 } 77 }