Add RegisterNatives overload for raw function pointers#1391
Merged
jonathanpeppers merged 4 commits intomainfrom Mar 19, 2026
Merged
Add RegisterNatives overload for raw function pointers#1391jonathanpeppers merged 4 commits intomainfrom
jonathanpeppers merged 4 commits intomainfrom
Conversation
Add JniNativeMethod struct matching JNI's JNINativeMethod layout (byte* Name, byte* Signature, IntPtr FunctionPointer) and a new RegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>) overload. Calls the JNI RegisterNatives function pointer directly via the function table, bypassing delegate marshaling entirely. Zero allocations in the entire path. Add end-to-end test with a dedicated Java class (RegisterNativesTestType) and an [UnmanagedCallersOnly] native callback that is registered via the new API, then called from Java to verify the result. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
jonathanpeppers
left a comment
There was a problem hiding this comment.
##[error]src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): Error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?)
D:\a\1\s\src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): Error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): Error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\PublicAPI.Unshipped.txt(16,1): Error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\PublicAPI.Unshipped.txt(16,1): error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
- Fix CS1061: Use JNIEnv function table directly via (*((JNIEnv**)env)) instead of info.Invoker which doesn't exist in the function pointers compilation path - Fix RS0016: Add implicit parameterless constructor to PublicAPI - Fix RS0016/RS0017: Use nint instead of System.IntPtr in PublicAPI (matches .NET 10 representation) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The Java class was not listed in the JavaInteropTestJar item group, causing ClassNotFoundException at runtime. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jonathanpeppers
approved these changes
Mar 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add JniNativeMethod struct matching JNI's JNINativeMethod layout
(byte* Name, byte* Signature, IntPtr FunctionPointer)and a newRegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>)overload.Calls the JNI RegisterNatives function pointer directly via the function table, bypassing delegate marshaling entirely. Zero allocations in the entire path.
Add end-to-end test with a dedicated Java class (RegisterNativesTestType) and an
[UnmanagedCallersOnly]native callback that is registered via the new API, then called from Java to verify the result.