Path: blob/master/test/jdk/java/foreign/TestRestricted.java
41145 views
/*1* Copyright (c) 2021, 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 jdk.incubator.foreign.MemoryAddress;24import jdk.incubator.foreign.MemorySegment;25import jdk.incubator.foreign.ResourceScope;26import org.testng.annotations.Test;2728import java.lang.invoke.MethodHandles;29import java.lang.invoke.MethodType;30import java.lang.reflect.Method;31import java.lang.reflect.InvocationTargetException;3233/*34* @test35* @run testng TestRestricted36*/37public class TestRestricted {38@Test(expectedExceptions = InvocationTargetException.class)39public void testReflection() throws Throwable {40Method method = MemorySegment.class.getDeclaredMethod("globalNativeSegment");41method.invoke(null);42}4344@Test(expectedExceptions = IllegalCallerException.class)45public void testInvoke() throws Throwable {46var mh = MethodHandles.lookup().findStatic(MemorySegment.class,47"globalNativeSegment", MethodType.methodType(MemorySegment.class));48var seg = (MemorySegment)mh.invokeExact();49}5051@Test(expectedExceptions = IllegalCallerException.class)52public void testDirectAccess() throws Throwable {53MemorySegment.globalNativeSegment();54}5556@Test(expectedExceptions = InvocationTargetException.class)57public void testReflection2() throws Throwable {58Method method = MemoryAddress.class.getDeclaredMethod("asSegment", long.class, ResourceScope.class);59method.invoke(MemoryAddress.NULL, 4000L, ResourceScope.globalScope());60}6162@Test(expectedExceptions = IllegalCallerException.class)63public void testInvoke2() throws Throwable {64var mh = MethodHandles.lookup().findVirtual(MemoryAddress.class, "asSegment",65MethodType.methodType(MemorySegment.class, long.class, ResourceScope.class));66var seg = (MemorySegment)mh.invokeExact(MemoryAddress.NULL, 4000L, ResourceScope.globalScope());67}6869@Test(expectedExceptions = IllegalCallerException.class)70public void testDirectAccess2() throws Throwable {71MemoryAddress.NULL.asSegment(4000L, ResourceScope.globalScope());72}73}747576