Path: blob/master/test/jdk/java/lang/invoke/MethodHandles/ensureInitialized/Main.java
41154 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*/2223import java.lang.invoke.MethodHandles;24import java.lang.reflect.Method;2526import org.testng.annotations.BeforeClass;27import org.testng.annotations.Test;28import static org.testng.Assert.*;2930/**31* @test32* @bug 823552133* @summary Tests for Lookup::ensureClassInitialized34* @build java.base/* m1/* m2/* Main35* @run testng/othervm --add-modules m1 Main36*/3738public class Main {39// Test access to public java.lang class40@Test41public void testPublic() throws Exception {42assertFalse(Helper.isInitialized(PublicInit.class));43MethodHandles.lookup().ensureInitialized(PublicInit.class);44assertTrue(Helper.isInitialized(PublicInit.class));45// no-op if already initialized46MethodHandles.lookup().ensureInitialized(PublicInit.class);47}4849// access denied to package-private java.lang class50@Test(expectedExceptions = { IllegalAccessException.class })51public void testPackagePrivate() throws Exception {52Class<?> c = Class.forName("java.lang.DefaultInit", false, null);53assertFalse(Helper.isInitialized(c));54// access denied55MethodHandles.lookup().ensureInitialized(c);56}5758// access denied to public class in a non-exported package59@Test(expectedExceptions = { IllegalAccessException.class })60public void testNonExportedPackage() throws Exception {61Class<?> c = Class.forName("jdk.internal.misc.VM", false, null);62// access denied63MethodHandles.lookup().ensureInitialized(c);64}6566// invoke p1.Test::test to test module boundary access67@Test68public void testModuleAccess() throws Exception {69Class<?> c = Class.forName("p1.Test");70Method m = c.getMethod("test");71m.invoke(null);72}7374@Test(expectedExceptions = { IllegalArgumentException.class })75public void testArrayType() throws Exception {76Class<?> arrayType = PublicInit.class.arrayType();77MethodHandles.lookup().ensureInitialized(arrayType);78}79}808182