Skip to content

Missing exception-free JavaCast overload #9038

Closed
dotnet/java-interop
#1234

Description

@mattleibow

Android framework version

net8.0-android

Affected platform version

.NET 8

Description

When writing some code that does not have a specific .NET type, or if the linker removes the .NET type because it is not used, there is no way to check to see if the type is implementing an interface or has a specific base type.

For example, I am using Glide to load a Drawable and then I want to see if it implements the IAnimatable interface. In debug builds this works because the AnimationDrawable type happens to be in the .NET type system. When the linker runs through the code, that type is never referenced so it is removed. This causes the runtime behavior to be wrong for an if block like this:

if (myDrawableFromJava is IAnimatable)

Similarly, if I have a slim binding (or chose not to bind everything) then the .NET type will never exist at all.

The app still runs because it uses the DrawableInvoker type to do things. This invoker does not have the interface to the .NET type check fails.

To get around this issue, I want to use the JavaCast<T>() extension method, but that now throws an exception if the type is not what I am casting.

Is there an overload or alternative that will allow be to check the types on the Java side to see if an interface or base type is valid and then return the instance that is of that type?

Steps to Reproduce

This is the core issue: dotnet/maui#22874

Did you find any workaround?

public static TResult? TryJavaCast<[DynamicallyAccessedMembers (Constructors)] TResult>(this IJavaObject? instance)
	where TResult : class, IJavaObject
{
	try
	{
		return instance.JavaCast<TResult>();
	}
	catch
	{
		return null;
	}
}

Relevant log output

No response

Activity

  1. added
    Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).
    and removed
    needs-triageIssues that need to be assigned.
    on Jun 18, 2024
  2. jonpryor commented on Jun 27, 2024

    @jonpryor
    Contributor

    I like this idea, and we should do it in dotnet/java-interop as an extension method on IJavaPeerable. :-)

  3. jpobst commented on Jun 27, 2024

    @jpobst
    Contributor

    This probably merits some discussion on what the API should look like. To fit the standard "Try" pattern, the API would be:

    public bool TryJavaCast<T> (this IJavaPeerable? instance, out T? result) where T : class, IJavaPeerable

    If we want to simply return T?, perhaps we should call it AsJavaCast to by symmetric with a C# "as" cast?

    public T? AsJavaCast<T> (this IJavaPeerable? instance) where T : class, IJavaPeerable
  4. jonpryor commented on Jun 28, 2024

    @jonpryor
    Contributor

    @jpobst: I agree with your thoughts on the "Try" pattern; TryJavaCast<T>() having a non-bool return type feels wrong.

    We could alternatively do .JavaAs<T>() for "symmetry" with the as operator.

    Usage-wise, I think bool-returning TryJavaCast() might be better?

    if (a.TryJavaCast<B>(out var b)) {
        // use `b`
    }
    
    // vs
    var b = a.JavaAs<B>();
    if (b != null) {
        // use `b`
    }

    If you're going to need an if anyway, you might as well go with the bool TryJavaParse<T>() idea. However, if you want to use the null conditional operator or pattern matching, .JavaAs<T>() might be better:

    a.JavaAs<B>()?.SomeMethodSpecificToB();
    
    if (a.JavaAs<B>() is B b) { // … but why?!
    }

    Maybe both Try and As variants?

    We can also debate .AsJavaCast<T>() vs. .JavaAs<T>(). .JavaAs<T>() came to mind earlier todays, before I had a chance to elaborate. However, .JavaAs<T>() might be "too cute".

  5. locked and limited conversation to collaborators on Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: Mono.AndroidIssues with the Android API binding (Mono.Android.dll).

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions