-
Notifications
You must be signed in to change notification settings - Fork 58
[Java.Interop] CreatePeer() must satisfy targetType #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+76
−3
Conversation
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
Context: dotnet/android#9747 Context: https://discord.com/channels/732297728826277939/732297837953679412/1339638847864176640 Context: https://discord.com/channels/732297728826277939/732297837953679412/1340011105510101063 While trying to get a MAUI sample running atop NativeAOT, we're observing the following crash: E AndroidRuntime: net.dot.jni.internal.JavaProxyThrowable: System.InvalidCastException: Arg_InvalidCastException E AndroidRuntime: at Java.Lang.Object._GetObject[T](IntPtr, JniHandleOwnership) + 0x64 E AndroidRuntime: at Microsoft.Maui.WindowOverlay.Initialize() + 0x168 Further investigation shows that the crash is from accessing the `Activity.WindowManager` property: namespace Android.App; partial class Activity { public virtual unsafe Android.Views.IWindowManager? WindowManager { // Metadata.xml XPath method reference: path="/api/package[@name='android.app']/class[@name='Activity']/method[@name='getWindowManager' and count(parameter)=0]" [Register ("getWindowManager", "()Landroid/view/WindowManager;", "GetGetWindowManagerHandler")] get { const string __id = "getWindowManager.()Landroid/view/WindowManager;"; try { var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, null); return global::Java.Lang.Object.GetObject<Android.Views.IWindowManager> (__rm.Handle, JniHandleOwnership.TransferLocalRef); } finally { } } } } `Object.GetObject<T>()` is now a wrapper around `JniRuntime.JniValueManager.GetPeer()`, so the problem, rephrased, is that this: var peer = JniEnvironment.Runtime.ValueManager.GetPeer ( ref h, JniObjectReferenceOptions.CopyAndDispose, targetType:typeof (IWindowManager)); returns a value which *does not implement* `IWindowManager`. It was, in fact, returning a `Java.Lang.Object` instance (!). The cause of the bug is that `JniRuntime.JniValueManager.CreatePeer()` was not checking that the `Type` returned form `Runtime.TypeManager.GetType(JniTypeSignature)` was compatible with `targetType`; instead, it returned the first type in the inheritance chain that had an activation constructor. This was `Java.Lang.Object`. Later, when `_GetObject<T>()` tried to cast the return value of `JniRuntime.JniValueManager.GetPeer()` to `IWindowManager`, it failed. Fix this by updating `CreatePeer()` to check that the `Type` from `JniRuntime.JniTypeManager.GetType(JniTypeSignature)` is assignable to `targetType`. This ensures that we *don't* return a `Java.Lang.Object` instance, allowing the cast to succeed. Update `JniRuntimeJniValueManagerContract` to test these new semantics.
jonpryor
added a commit
to dotnet/android
that referenced
this pull request
Feb 14, 2025
Does It Build™?
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.
Files not reviewed (1)
- tests/Java.Interop-Tests/Java.Interop-Tests.csproj: Language not supported
jonathanpeppers
approved these changes
Feb 14, 2025
jonpryor
added a commit
to dotnet/android
that referenced
this pull request
Feb 15, 2025
Changes: dotnet/java-interop@d62008d...2a7183a * dotnet/java-interop@2a7183a1: [Java.Interop] CreatePeer() must satisfy targetType (dotnet/java-interop#1308) Context: #9747
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Context: dotnet/android#9747
Context: https://discord.com/channels/732297728826277939/732297837953679412/1339638847864176640
Context: https://discord.com/channels/732297728826277939/732297837953679412/1340011105510101063
While trying to get a MAUI sample running atop NativeAOT, we're observing the following crash:
Further investigation shows that the crash is from accessing the
Activity.WindowManager
property:Object.GetObject<T>()
is now a wrapper aroundJniRuntime.JniValueManager.GetPeer()
, so the problem, rephrased, is that this:returns a value which does not implement
IWindowManager
. It was, in fact, returning aJava.Lang.Object
instance (!).The cause of the bug is that
JniRuntime.JniValueManager.CreatePeer()
was not checking that theType
returned formRuntime.TypeManager.GetType(JniTypeSignature)
was compatible withtargetType
; instead, it returned the first type in the inheritance chain that had an activation constructor. This wasJava.Lang.Object
.Later, when
_GetObject<T>()
tried to cast the return value ofJniRuntime.JniValueManager.GetPeer()
toIWindowManager
, it failed.Fix this by updating
CreatePeer()
to check that theType
fromJniRuntime.JniTypeManager.GetType(JniTypeSignature)
is assignable totargetType
. This ensures that we don't return aJava.Lang.Object
instance, allowing the cast to succeed.Update
JniRuntimeJniValueManagerContract
to test these new semantics.