Path: blob/master/test/hotspot/jtreg/runtime/HiddenClasses/InstantiateHiddenClass.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 Test issues with instantiating hidden classes.26* @library /test/lib27* @modules jdk.compiler28* @run main InstantiateHiddenClass29*/3031import java.lang.invoke.MethodType;32import java.lang.invoke.MethodHandles;33import java.lang.invoke.MethodHandles.Lookup;34import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;35import jdk.test.lib.compiler.InMemoryJavaCompiler;3637public class InstantiateHiddenClass {3839static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",40"public class TestClass { " +41" public static void concat(String one, String two) throws Throwable { " +42" System.out.println(one + two);" +43" } } ");4445public static void main(String[] args) throws Throwable {4647// Test that a hidden class cannot be found through its name.48try {49Lookup lookup = MethodHandles.lookup();50Class<?> cl = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();51Class.forName(cl.getName()).newInstance();52throw new RuntimeException("Expected ClassNotFoundException not thrown");53} catch (ClassNotFoundException e ) {54// Test passed55}565758// Create two hidden classes and instantiate an object from each of them.59// Verify that the references to these objects are different and references60// to their classes are not equal either.61Lookup lookup = MethodHandles.lookup();62Class<?> c1 = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();63Class<?> c2 = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();64Object o1 = c1.newInstance();65Object o2 = c2.newInstance();66if (o1 == o2) {67throw new RuntimeException("Objects should not be equal");68}69if (o1.getClass() == o2.getClass()) {70throw new RuntimeException("Classes should not be equal");71}72}73}747576