Skip to content

Commit ed267ff

Browse files
[Mono.Android] fix AndroidRuntime.UnboxException()
`Java.InteropTests.JavaExceptionTests.InnerExceptionIsNotAProxy()` fails with: Expected: same as <System.InvalidOperationException: Managed Exception!> But was: <Java.Interop.JavaProxyThrowable: System.InvalidOperationException: Managed Exception! Can be fixed by using `JNIEnvInit.ValueManager?.PeekValue()` instead.
1 parent e2f0960 commit ed267ff

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 System.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)