Skip to content

Commit dc07800

Browse files
[Mono.Android] fix "exception unboxing" for ManagedValueManager (#10100)
Context: #10095 After enabling `Java.Interop-Tests.dll` for CoreCLR/NativeAOT, `Java.InteropTests.JavaExceptionTests.InnerExceptionIsNotAProxy()` was failing with: Expected: same as <System.InvalidOperationException: Managed Exception!> But was: <Java.Interop.JavaProxyThrowable: System.InvalidOperationException: Managed Exception! This is because `AndroidRuntime.cs` was doing: if (JNIEnvInit.ValueManager is AndroidValueManager vm) { return vm.UnboxException (value); } And in this case `ManagedValueManager` was being used, and so the exception would not be "unboxed". We can fix this by using `JNIEnvInit.ValueManager?.PeekValue()` instead and the test now passes. I also noticed that `AndroidValueManager` had code in `TryUnboxPeerObject()` for: var proxy = value as Android.Runtime.JavaProxyThrowable; if (proxy != null) { result = proxy.InnerException; return true; } return base.TryUnboxPeerObject (value, out result); While the base class handled `Java.Interop.JavaProxyThrowable`. This was missing for `ManagedValueManager`, so I added it.
1 parent 463e6da commit dc07800

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/Mono.Android/Android.Runtime/AndroidRuntime.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,13 @@ public override string GetCurrentManagedThreadStackTrace (int skipFrames, bool f
6666
return throwable;
6767
}
6868
JniObjectReference.Dispose (ref reference, options);
69-
var unwrapped = UnboxException (peeked!);
69+
var unwrapped = JNIEnvInit.ValueManager?.PeekValue (peeked!.PeerReference) as Exception;
7070
if (unwrapped != null) {
7171
return unwrapped;
7272
}
7373
return peekedExc;
7474
}
7575

76-
Exception? UnboxException (IJavaPeerable value)
77-
{
78-
if (JNIEnvInit.ValueManager is AndroidValueManager vm) {
79-
return vm.UnboxException (value);
80-
}
81-
return null;
82-
}
83-
8476
public override void RaisePendingException (Exception pendingException)
8577
{
8678
var je = pendingException as JavaException;
@@ -865,15 +857,6 @@ protected override bool TryUnboxPeerObject (IJavaPeerable value, [NotNullWhen (t
865857
return base.TryUnboxPeerObject (value, out result);
866858
}
867859

868-
internal Exception? UnboxException (IJavaPeerable value)
869-
{
870-
object? r;
871-
if (TryUnboxPeerObject (value, out r) && r is Exception e) {
872-
return e;
873-
}
874-
return null;
875-
}
876-
877860
public override void CollectPeers ()
878861
{
879862
GC.Collect ();

src/Mono.Android/Microsoft.Android.Runtime/ManagedValueManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,14 @@ protected override bool TryConstructPeer (
284284
}
285285
return base.TryConstructPeer (self, ref reference, options, type);
286286
}
287+
288+
protected override bool TryUnboxPeerObject (IJavaPeerable value, [NotNullWhen (true)]out object? result)
289+
{
290+
var proxy = value as JavaProxyThrowable;
291+
if (proxy != null) {
292+
result = proxy.InnerException;
293+
return true;
294+
}
295+
return base.TryUnboxPeerObject (value, out result);
296+
}
287297
}

0 commit comments

Comments
 (0)