Skip to content

Conversation

@jonathanpeppers
Copy link
Member

Context: #10095

After enabling Java.Interop-Tests.dll for CoreCLR/NativeAOT, we started seeing Java.InteropTests.JavaExceptionTests.InnerExceptionIsNotAProxy() fail 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.

Context: #10095

After enabling `Java.Interop-Tests.dll` for CoreCLR/NativeAOT, we started seeing
`Java.InteropTests.JavaExceptionTests.InnerExceptionIsNotAProxy()` fail 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.
@jonathanpeppers jonathanpeppers requested a review from jonpryor as a code owner May 2, 2025 13:26
@jonpryor jonpryor merged commit dc07800 into main May 2, 2025
59 checks passed
@jonpryor jonpryor deleted the dev/peppers/unbox-exceptions branch May 2, 2025 18:30
@github-actions github-actions bot locked and limited conversation to collaborators Jun 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants