Skip to content

[NativeAOT] Forward JCW Java annotations - #12549

Merged
simonrozsival merged 8 commits into
mainfrom
jonathanpeppers-fix-nativeaot-jcw-annotations
Aug 29, 2026
Merged

[NativeAOT] Forward JCW Java annotations#12549
simonrozsival merged 8 commits into
mainfrom
jonathanpeppers-fix-nativeaot-jcw-annotations

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Fixes#12542

Summary

  • forward Java annotations from managed custom attributes through the trimmable NativeAOT JCW pipeline
  • preserve annotations on types, methods, registered constructors, property overrides, and exported fields
  • cache annotation metadata and support cross-assembly attribute definitions such as Android.Webkit.JavascriptInterfaceAttribute
  • add regression coverage matching the app-to-Mono.Android assembly boundary

Testing

  • dotnet test tests\Microsoft.Android.Sdk.TrimmableTypeMap.Tests\Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csproj -v minimal (773 passed)

Restore annotation metadata forwarding in the trimmable JCW pipeline for types, methods, constructors, and exported fields.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
CopilotAI lite review requested due to automatic review settings August 27, 2026 20:49
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR restores/extends forwarding of Java annotations from managed custom attributes into generated Java Callable Wrappers (JCWs) for the trimmable NativeAOT pipeline, including support for cross-assembly attribute definitions and additional member kinds.

Changes:

  • Add JavaAnnotationParser and plumb parsed annotations through the scanner model (JavaPeerInfo, MarshalMethodInfo, etc.).
  • Emit parsed annotations in the JCW Java source generator for types, constructors, methods, and exported fields.
  • Add new fixture assemblies + regression tests covering cross-assembly annotation attributes (e.g., Android.Webkit.JavascriptInterfaceAttribute) and member scenarios (property override getter, constructor, export field).

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
FileDescription
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestFixtures/TestTypes.csAdds new managed fixture types/members exercising annotation forwarding scenarios (including JavascriptInterface).
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestFixtures/TestFixtures.csprojReferences the new attribute-fixture project so fixture compilation can use cross-assembly attribute types.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestFixtures/StubAttributes.csRemoves local stub attribute definitions now provided by the new shared attribute-fixture assembly.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestAttributeFixtures/TestAttributeFixtures.csprojNew project producing a dedicated assembly containing annotation/attribute type definitions for cross-assembly testing.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestAttributeFixtures/Attributes.csDefines AnnotationAttribute, RegisterAttribute, JavascriptInterfaceAttribute, and a custom annotation attribute with properties.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests.csprojEnsures the new fixture assembly is built and copied beside the test output for scanner input.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/JcwJavaSourceGeneratorTests.csAdds assertions that generated Java includes expected forwarded annotations across multiple member kinds.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/FixtureTestBase.csUpdates fixture scanning to include both fixture assemblies as scanner inputs.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.csIntegrates annotation parsing into scan results for types, methods/ctors, property overrides, and export fields.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerInfo.csExtends scan model with JavaAnnotationInfo and annotation collections for relevant entities.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaAnnotationParser.csNew parser that detects annotation-decorated attributes and extracts Java annotation names + property assignments.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/AssemblyIndex.csExtends export metadata with IsField to support correct annotation routing for export fields.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/JcwJavaSourceGenerator.csEmits forwarded annotations into generated .java source for types/members/fields.

Use Java string escapes and invariant numeric formatting when forwarding annotation properties.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f220fb47-4647-44f6-9b3f-47f17a46c846
@jonathanpeppersjonathanpeppers added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Aug 28, 2026
@simonrozsival
simonrozsival merged commit c88edf2 into mainAug 29, 2026
44 checks passed
@simonrozsival
simonrozsival deleted the jonathanpeppers-fix-nativeaot-jcw-annotations branch August 29, 2026 10:05
simonrozsival added a commit that referenced this pull request Sep 3, 2026
## Summary
- add a focused `MSBuildDeviceIntegration` app that loads deterministic local HTML in `WebView` and calls a managed bridge through `addJavascriptInterface`
- exercise the runtime behavior with `llvm-ir`/CoreCLR, trimmable/CoreCLR, and trimmable/NativeAOT
- assert the callback reaches the exact managed bridge instance once with the expected payload, with bounded timeout and device cleanup
Tracks the remaining annotation regression item in #12561 and adds end-to-end coverage for #12542 / #12549.
## Regression evidence
With annotation emission disabled, the NativeAOT runtime case timed out. With production annotation emission restored, all three runtime cases passed.
## Testing
```text
./dotnet-local.sh test bin/TestDebug/MSBuildDeviceIntegration/net10.0/MSBuildDeviceIntegration.dll \
--filter 'FullyQualifiedName~WebViewJavascriptInterfaceTests'
Passed: 3, Failed: 0, Skipped: 0
```
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-reviewThis PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java Callable Wrappers under Native AOT no longer forward C# attributes to Java annotations

3 participants

@jonathanpeppers@simonrozsival