Path: blob/master/test/hotspot/jtreg/runtime/HiddenClasses/NestedHidden.java
40942 views
/*1* Copyright (c) 2020, 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* @summary Creates a hidden class inside of a hidden class.26* @library /test/lib27* @modules jdk.compiler28* @run main p.NestedHidden29*/3031package p;3233import java.lang.*;3435import java.lang.invoke.MethodHandles;36import java.lang.invoke.MethodHandles.Lookup;37import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;38import jdk.test.lib.compiler.InMemoryJavaCompiler;394041// Test that a hidden class can define its own hidden class by calling42// lookup.defineHiddenClass().43public class NestedHidden {44static byte klassbuf[] = InMemoryJavaCompiler.compile("p.TestClass",45"package p; " +46"public class TestClass { " +47" public static void concat(String one, String two) throws Throwable { " +48" System.out.println(one + two);" +49" } } ");5051public static void main(String args[]) throws Exception {52// The hidden class calls lookup.defineHiddenClass(), creating a nested hidden class.53byte klassbuf2[] = InMemoryJavaCompiler.compile("p.TestClass2",54"package p; " +55"import java.lang.invoke.MethodHandles; " +56"import java.lang.invoke.MethodHandles.Lookup; " +57"import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*; " +58"public class TestClass2 { " +59" public static void doit() throws Throwable { " +60" Lookup lookup = MethodHandles.lookup(); " +61" Class<?> klass2 = lookup.defineHiddenClass(p.NestedHidden.klassbuf, true, NESTMATE).lookupClass(); " +62" Class[] dArgs = new Class[2]; " +63" dArgs[0] = String.class; " +64" dArgs[1] = String.class; " +65" try { " +66" klass2.getMethod(\"concat\", dArgs).invoke(null, \"CC\", \"DD\"); " +67" } catch (Throwable ex) { " +68" throw new RuntimeException(\"Exception: \" + ex.toString()); " +69" } " +70"} } ");7172Lookup lookup = MethodHandles.lookup();73Class<?> klass2 = lookup.defineHiddenClass(klassbuf2, true, NESTMATE).lookupClass();74klass2.getMethod("doit").invoke(null);75}76}777879