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