ForeignAccess
public interface ForeignAccessThis interface is used to register classes, methods, and fields for foreign access at run time. An instance of this interface is acquired via Feature.AfterRegistrationAccess.getForeignAccess().
All methods in ForeignAccess require a AccessCondition as their first parameter. Registration for foreign access will happen only if the specified condition is satisfied.
How to use
ForeignAccess should only be used during Feature.afterRegistration. Any attempt to register metadata in any other phase will result in an error.
Example:
@Override
public void afterRegistration(AfterRegistrationAccess access) {
ForeignAccess foreignAccess = access.getForeignAccess();
AccessCondition condition = AccessCondition.typeReached(ConditionType.class);
foreignAccess.registerForDowncall(condition, java.lang.foreign.ValueLayout.JAVA_INT);
}Methods3
void registerForDowncall(AccessCondition, Object, Object...)
registerForDowncall
since 25.0.1void registerForDowncall(AccessCondition condition, Object desc, Object... options)Registers the provided function descriptor and options pair at image build time for downcalls into foreign code, if the condition is satisfied. Required to get a downcall method handle using Linker.downcallHandle for the same descriptor and options at run time.
Even though this method is weakly typed for compatibility reasons, run-time checks will be performed to ensure that the arguments have the expected type. It will be deprecated in favor of strongly typed variant as soon as possible.
Parameters
conditionAccessCondition- represents the condition that needs to be satisfied in order to access target resources.
descObject- A
FunctionDescriptorto register for downcalls. optionsObject...- An array of
Linker.Optionused for the downcalls.
void registerForUpcall(AccessCondition, Object, Object...)
registerForUpcall
since 25.0.1void registerForUpcall(AccessCondition condition, Object desc, Object... options)Registers the provided function descriptor and options pair at image build time for upcalls from foreign code, if the condition is satisfied. Required to get an upcall stub function pointer using Linker.upcallStub for the same descriptor and options at run time.
Even though this method is weakly typed for compatibility reasons, run-time checks will be performed to ensure that the arguments have the expected type. It will be deprecated in favor of strongly typed variant as soon as possible.
Parameters
conditionAccessCondition- represents the condition that needs to be satisfied in order to access target resources.
descObject- A
FunctionDescriptorto register for upcalls. optionsObject...- An array of
Linker.Optionused for the upcalls.
void registerForDirectUpcall(AccessCondition, MethodHandle, Object, Object...)
registerForDirectUpcall
since 25.0.1void registerForDirectUpcall(
AccessCondition condition,
MethodHandle target,
Object desc,
Object... options
)Registers a specific static method (denoted by a method handle) as a fast upcall target, if the condition is satisfied. This will create a specialized upcall stub that will invoke only the specified method, which is much faster than using registerForUpcall(AccessCondition, Object, Object...)).
The provided method handle must be a direct method handle. Those are most commonly created using MethodHandles.Lookup.findStatic(Class, String, MethodType). However, a strict requirement is that it must be possible to create a non-empty descriptor for the method handle using MethodHandle.describeConstable(). The denoted static method will also be registered for reflective access since run-time code will also create a method handle to denoted static method.
Even though this method is weakly typed for compatibility reasons, run-time checks will be performed to ensure that the arguments have the expected type. It will be deprecated in favor of strongly typed variant as soon as possible.
Parameters
conditionAccessCondition- represents the condition that needs to be satisfied in order to access target resources.
targetMethodHandle- A direct method handle denoting a static method.
descObject- A
FunctionDescriptorto register for upcalls. optionsObject...- An array of
Linker.Optionused for the upcalls.