75
76 static RestrictedMethod of(Class<?> owner, String name, Class<?> returnType, Class<?>... paramTypes) {
77 return new RestrictedMethod(owner, name, MethodType.methodType(returnType, paramTypes));
78 }
79 };
80
81 static final Set<RestrictedMethod> RESTRICTED_METHODS = Set.of(
82 RestrictedMethod.of(SymbolLookup.class, "libraryLookup", SymbolLookup.class, String.class, Arena.class),
83 RestrictedMethod.of(SymbolLookup.class, "libraryLookup", SymbolLookup.class, Path.class, Arena.class),
84 RestrictedMethod.of(Linker.class, "downcallHandle", MethodHandle.class, FunctionDescriptor.class, Linker.Option[].class),
85 RestrictedMethod.of(Linker.class, "downcallHandle", MethodHandle.class, MemorySegment.class, FunctionDescriptor.class, Linker.Option[].class),
86 RestrictedMethod.of(Linker.class, "upcallStub", MemorySegment.class, MethodHandle.class, FunctionDescriptor.class, Arena.class, Linker.Option[].class),
87 RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, long.class),
88 RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, Arena.class, Consumer.class),
89 RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, long.class, Arena.class, Consumer.class),
90 RestrictedMethod.of(AddressLayout.class, "withTargetLayout", AddressLayout.class, MemoryLayout.class),
91 RestrictedMethod.of(ModuleLayer.Controller.class, "enableNativeAccess", ModuleLayer.Controller.class, Module.class),
92 RestrictedMethod.of(System.class, "load", void.class, String.class),
93 RestrictedMethod.of(System.class, "loadLibrary", void.class, String.class),
94 RestrictedMethod.of(Runtime.class, "load", void.class, String.class),
95 RestrictedMethod.of(Runtime.class, "loadLibrary", void.class, String.class)
96 );
97
98 @Test
99 public void testRestricted() {
100 Set<RestrictedMethod> restrictedMethods = new HashSet<>(RESTRICTED_METHODS);
101 restrictedMethods(Object.class.getModule()).forEach(m -> checkRestrictedMethod(m, restrictedMethods));
102 if (!restrictedMethods.isEmpty()) {
103 fail("@Restricted annotation not found for methods: " + restrictedMethods);
104 }
105 }
106
107 void checkRestrictedMethod(Method meth, Set<RestrictedMethod> restrictedMethods) {
108 String sig = meth.getDeclaringClass().getName() + "::" + shortSig(meth);
109 boolean expectRestricted = restrictedMethods.remove(RestrictedMethod.from(meth));
110 assertTrue(expectRestricted, "unexpected @Restricted annotation found on method " + sig);
111 assertTrue(meth.isAnnotationPresent(CallerSensitive.class), "@CallerSensitive annotation not found on restricted method " + sig);
112 }
113
114 /**
115 * Returns a stream of all restricted methods on public classes in packages
|
75
76 static RestrictedMethod of(Class<?> owner, String name, Class<?> returnType, Class<?>... paramTypes) {
77 return new RestrictedMethod(owner, name, MethodType.methodType(returnType, paramTypes));
78 }
79 };
80
81 static final Set<RestrictedMethod> RESTRICTED_METHODS = Set.of(
82 RestrictedMethod.of(SymbolLookup.class, "libraryLookup", SymbolLookup.class, String.class, Arena.class),
83 RestrictedMethod.of(SymbolLookup.class, "libraryLookup", SymbolLookup.class, Path.class, Arena.class),
84 RestrictedMethod.of(Linker.class, "downcallHandle", MethodHandle.class, FunctionDescriptor.class, Linker.Option[].class),
85 RestrictedMethod.of(Linker.class, "downcallHandle", MethodHandle.class, MemorySegment.class, FunctionDescriptor.class, Linker.Option[].class),
86 RestrictedMethod.of(Linker.class, "upcallStub", MemorySegment.class, MethodHandle.class, FunctionDescriptor.class, Arena.class, Linker.Option[].class),
87 RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, long.class),
88 RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, Arena.class, Consumer.class),
89 RestrictedMethod.of(MemorySegment.class, "reinterpret", MemorySegment.class, long.class, Arena.class, Consumer.class),
90 RestrictedMethod.of(AddressLayout.class, "withTargetLayout", AddressLayout.class, MemoryLayout.class),
91 RestrictedMethod.of(ModuleLayer.Controller.class, "enableNativeAccess", ModuleLayer.Controller.class, Module.class),
92 RestrictedMethod.of(System.class, "load", void.class, String.class),
93 RestrictedMethod.of(System.class, "loadLibrary", void.class, String.class),
94 RestrictedMethod.of(Runtime.class, "load", void.class, String.class),
95 RestrictedMethod.of(Runtime.class, "loadLibrary", void.class, String.class),
96 // custom scheduler prototype 1
97 RestrictedMethod.of(Thread.Builder.OfVirtual.class, "unstarted", Thread.class, Runnable.class, Thread.class, Object.class),
98 // custom scheduler prototype 2
99 RestrictedMethod.of(Thread.Builder.OfVirtual.class, "scheduler", Thread.Builder.OfVirtual.class, Thread.VirtualThreadScheduler.class),
100 RestrictedMethod.of(Thread.VirtualThreadScheduler.class, "current", Thread.VirtualThreadScheduler.class)
101 );
102
103 @Test
104 public void testRestricted() {
105 Set<RestrictedMethod> restrictedMethods = new HashSet<>(RESTRICTED_METHODS);
106 restrictedMethods(Object.class.getModule()).forEach(m -> checkRestrictedMethod(m, restrictedMethods));
107 if (!restrictedMethods.isEmpty()) {
108 fail("@Restricted annotation not found for methods: " + restrictedMethods);
109 }
110 }
111
112 void checkRestrictedMethod(Method meth, Set<RestrictedMethod> restrictedMethods) {
113 String sig = meth.getDeclaringClass().getName() + "::" + shortSig(meth);
114 boolean expectRestricted = restrictedMethods.remove(RestrictedMethod.from(meth));
115 assertTrue(expectRestricted, "unexpected @Restricted annotation found on method " + sig);
116 assertTrue(meth.isAnnotationPresent(CallerSensitive.class), "@CallerSensitive annotation not found on restricted method " + sig);
117 }
118
119 /**
120 * Returns a stream of all restricted methods on public classes in packages
|