Skip to content

[TrimmableTypeMap] Fix [Export] JNI signature mapping for non-primitive parameter types#11051

Open
Copilot wants to merge 2 commits intomainfrom
copilot/fix-9141961-55075302-dd591e09-e008-489a-a247-c8cbff8f3695
Open

[TrimmableTypeMap] Fix [Export] JNI signature mapping for non-primitive parameter types#11051
Copilot wants to merge 2 commits intomainfrom
copilot/fix-9141961-55075302-dd591e09-e008-489a-a247-c8cbff8f3695

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 30, 2026

Problem

The scanner's ManagedTypeToJniDescriptor() method maps managed types to JNI descriptors for [Export] method signatures. It correctly handles primitives (boolZ, intI, etc.) and stringLjava/lang/String;, but falls back to Ljava/lang/Object; for all other types. This means [Export] methods with parameters like Android.Graphics.Bitmap, Android.Views.View, or any custom Java-bound type get incorrect JNI signatures in the generated JCW, causing NoSuchMethodError at runtime.

Fix

ManagedTypeToJniDescriptor() now resolves Java-bound types by looking up their [Register] attribute via the existing TryResolveJniObjectDescriptor() infrastructure (already used by the constructor signature path), falling back to Ljava/lang/Object; only for types that truly cannot be resolved.

Changes Made

  • ManagedTypeToJniDescriptor — Added TryResolveJniObjectDescriptor call before the Ljava/lang/Object; fallback; converted from static to instance to access the assembly cache.
  • BuildJniSignatureFromManaged, ParseExportAttribute, ParseExportFieldAsMethod, TryGetMethodRegisterInfo, CollectExportField — Converted from static to instance (transitively need the assembly cache for type resolution).
  • ExportWithJavaBoundParams test type — Added with three [Export] methods taking Android.Views.View parameters.
  • Scan_ExportMethod_ResolvesJavaBoundParameterTypes test theory — Verifies correct JNI signatures for [Export] methods with non-primitive Java-bound parameter types.
  • ExportFieldTests — Updated assertion to expect the correctly resolved type name (my.app.ExportFieldExample) instead of the previous incorrect java.lang.Object fallback.

Testing

  • All 303 TrimmableTypeMap unit tests pass, including 3 new test cases.

@jonathanpeppers
Copy link
Copy Markdown
Member

@copilot can you continue what you were doing?

ManagedTypeToJniDescriptor now resolves Java-bound types via their
[Register] attribute using TryResolveJniObjectDescriptor, falling back
to Ljava/lang/Object; only for types that cannot be resolved.

Methods that call it are converted from static to instance to access
the assembly cache for type resolution.

Agent-Logs-Url: https://github.com/dotnet/android/sessions/78609547-4f51-479a-aa5a-03a5bb54a0b8

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 30, 2026

@copilot can you continue what you were doing?

Done in 768a003. The fix converts ManagedTypeToJniDescriptor from static to instance and adds a TryResolveJniObjectDescriptor call before the Ljava/lang/Object; fallback — the same resolution already used by ManagedTypeToJniDescriptorOrNull for constructor signatures. Methods in the call chain (BuildJniSignatureFromManaged, ParseExportAttribute, ParseExportFieldAsMethod, TryGetMethodRegisterInfo, CollectExportField) are also converted from static to instance to access the assembly cache. All 303 tests pass, including 3 new test cases for [Export] methods with Android.Views.View parameters.

Copilot AI changed the title [WIP] Copilot Request [TrimmableTypeMap] Fix [Export] JNI signature mapping for non-primitive parameter types Mar 30, 2026
Copilot AI requested a review from jonathanpeppers March 30, 2026 21:21
@simonrozsival simonrozsival marked this pull request as ready for review March 31, 2026 07:28
Copilot AI review requested due to automatic review settings March 31, 2026 07:28
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect JNI signature generation for [Export] (and related [ExportField]) methods when parameters/return types are non-primitive Java-bound managed types, by resolving descriptors via [Register] metadata instead of always falling back to Ljava/lang/Object;. This aligns [Export] signature computation with the existing constructor-signature resolution path.

Changes:

  • Update JavaPeerScanner.ManagedTypeToJniDescriptor() to resolve Java-bound types via TryResolveJniObjectDescriptor() before falling back to Ljava/lang/Object;.
  • Convert several helper methods from static to instance methods to access the scanner’s assembly cache for type resolution.
  • Add/adjust unit tests to validate correct JNI signatures for [Export] methods with Java-bound parameter types and for [ExportField] Java type names.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/TestFixtures/TestTypes.cs Adds a new fixture type with [Export] methods taking Android.Views.View parameters to exercise Java-bound parameter signature resolution.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Scanner/JavaPeerScannerTests.Behavior.cs Adds a theory verifying resolved JNI signatures for the new [Export] fixture methods.
tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/ExportFieldTests.cs Updates expectation so [ExportField] return type resolves to the actual Java type instead of java.lang.Object.
src/Microsoft.Android.Sdk.TrimmableTypeMap/Scanner/JavaPeerScanner.cs Implements Java-bound type resolution in [Export] signature mapping and refactors helpers to instance methods to use the assembly cache.

@simonrozsival simonrozsival added copilot `copilot-cli` or other AIs were used to author this trimmable-type-map labels Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot `copilot-cli` or other AIs were used to author this trimmable-type-map

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TrimmableTypeMap] Fix [Export] JNI signature mapping for non-primitive parameter types

4 participants