Skip to content

[java.interop] improve JavaCast() for interfaces - #1059

Merged
jonpryor merged 2 commits into
dotnet:masterfrom
jonathanpeppers:java-cast-invokers
Dec 14, 2017
Merged

jonpryor merged 2 commits into
dotnet:masterfrom
jonathanpeppers:java-cast-invokers

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Dec 1, 2017 •

Copy link
Copy Markdown
Member

Currently, interfaces generate Invoker classes such as:

// src/Mono.Android/obj/Debug/android-26/mcw/Java.Lang.IAppendable.cs
partial class IAppendableInvoker {
        public static IAppendable GetObject (IntPtr handle, JniHandleOwnership transfer)
        {
            return global::Java.Lang.Object.GetObject<IAppendable>(handle, transfer);
        }
}

JavaCast was previously looking up these Invoker types and calling
the GetObject method via reflection. We can simplify this by just calling
Java.Lang.Object.GetObject() directly—bypassing all the reflection.

One day we might be able to drop the generated Invoker method from
generator, but compiled bindings would break on older versions of
Xamarin.Android. It might be instead worthwhile look into allowing
the linker to strip the *Invoker.GetObject() methods that are now
not used anymore.

throw new ArgumentException ("Unable to get Invoker for interface '" + resultType.FullName + "'.", "resultType");
var getObject = invokerType.GetMethod ("GetObject", new[]{typeof (IntPtr), typeof (JniHandleOwnership)});
return (IJavaObject) getObject.Invoke (null, new object[]{instance.Handle, JniHandleOwnership.DoNotTransfer});
return Java.Lang.Object.GetObject (instance.Handle, JniHandleOwnership.DoNotTransfer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm less certain about this change: Java.Lang.Object.GetObject() will return any previously created wrapper for the given type handle, which need not be of type resultType.

This should probably be:

return Java.Lang.Object.GetObject (instance.Handle, JniHandleOwnership.DoNotTransfer, resultType);

@jonpryor

jonpryor commented Dec 1, 2017 •

Copy link
Copy Markdown
Contributor

It might be instead worthwhile look into allowing the linker to strip [the *Invoker.GetObject() method] that are now not used anymore.

@radekdoulik: How hard would it be to do that? I think such a change should "belong" to this PR, to validate the underlying change.

Currently, interfaces generate `Invoker` classes such as:
```
// src/Mono.Android/obj/Debug/android-26/mcw/Java.Lang.IAppendable.cs
partial class IAppendableInvoker {
        public static IAppendable GetObject (IntPtr handle, JniHandleOwnership transfer)
        {
            return global::Java.Lang.Object.GetObject<IAppendable>(handle, transfer);
        }
}
```

`JavaCast` was previously looking up these `Invoker` types and calling
the `GetObject` method via reflection. We can simplify this by just calling
`Java.Lang.Object.GetObject()` directly—bypassing all the reflection.

One day we might be able to drop the generated `Invoker` method from
generator, but compiled bindings would break on older versions of
Xamarin.Android. It might be *instead* worthwhile look into allowing
the linker to strip the `*Invoker.GetObject()` methods that are now
not used anymore.
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@radekdoulik

Copy link
Copy Markdown
Member

It is now preserved here: https://github.com/xamarin/xamarin-android/blob/master/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs#L64

I am not sure yet, whether adapter_type resolving to *Invoker is right here. So either we fix adapter_type lookup or just don't mark GetObject method here. Needs further investigation.

@radekdoulik

Copy link
Copy Markdown
Member

After discussion with @jonpryor I will try to remove the code which preserves the whole *Invoker type. We will see whether it will break things. Hopefully not and so we will get rid of *Invoker::GetObject methods as well.

@jonathanpeppers

Copy link
Copy Markdown
Member Author

@radekdoulik

Copy link
Copy Markdown
Member

Leave it for now. I am trying the modified linker and will see if we need to change more code.

@radekdoulik radekdoulik added the full-mono-integration-build For PRs; run a full build (~6-10h for mono bumps), not the faster PR subset (~2h for mono bumps) label Dec 4, 2017
@radekdoulik

Copy link
Copy Markdown
Member

OK, let see if the tests pass.

I checked it locally and the linked Mono.Android.dll doesn't preserve *Invoker::GetObject methods anymore.

@radekdoulik radekdoulik added the use-merge-commit Normally we squash-and-merge PRs. Use this label so we instead use a merge commit. label Dec 4, 2017
@radekdoulik

Copy link
Copy Markdown
Member

@monojenkins build macOS+xbuild PR Build

@radekdoulik

Copy link
Copy Markdown
Member

@monojenkins build macOS+xbuild PR Build

if (!m.TryGetRegisterAttribute (out registerAttr))
continue;

if (registerAttr.ConstructorArguments.Count != 3)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this use TryGetRegisterMember()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can :-) Thanks. Indeed, I first thought I will need to use another constructor parameter and then forgot about it.

if (!methodAndType.Contains (":"))
continue;

var values = methodAndType.Split (':');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use string.Split(char[], int), e.g.

var values = methodAndType.Split (new[]{':'}, 2);

That way if the type or assembly name somehow contains :, we don't screw up.

The `adapter_type` was resolved to the `*Invoker` type.

One of the methods we wanted preserve, was GetObject method, which was
reached by reflection. It is not the case anymore.

We still need to preserve interface methods in the Invoker types.
@radekdoulik

Copy link
Copy Markdown
Member

build

@dotnet dotnet deleted a comment from dnfclas Dec 12, 2017
@radekdoulik

Copy link
Copy Markdown
Member

build

@jonpryor
jonpryor merged commit 5b945ab into dotnet:master Dec 14, 2017
@radekdoulik

Copy link
Copy Markdown
Member

Looks like the use-merge-commit label went unnoticed and my commit got squished.

@jonpryor

Copy link
Copy Markdown
Contributor

Sorry. :-(

jonpryor added a commit that referenced this pull request May 23, 2022
Context: 5b945ab
Context: https://gist.github.com/jpobst/be05c8115ba0cd06b646f65c5068cfa2

Commit 55a2dd3 would cause apps to crash on startup:

    android.runtime.JavaProxyThrowable: System.ArgumentException: Couldn't bind to method 'GetOnActivityCreated_Landroid_app_Activity_Landroid_os_Bundle_Handler'.
      at System.Delegate.GetCandidateMethod (System.Type type, System.Type target, System.String method, System.Reflection.BindingFlags bflags, System.Boolean ignoreCase, System.Boolean throwOnBindFailure)
      at System.Delegate.CreateDelegate (System.Type type, System.Type target, System.String method, System.Boolean ignoreCase, System.Boolean throwOnBindFailure)
      at System.Delegate.CreateDelegate (System.Type type, System.Type target, System.String method)
      at Android.Runtime.AndroidTypeManager.RegisterNativeMembers (Java.Interop.JniType nativeClass, System.Type type, System.ReadOnlySpan`1[T] methods)
    --- End of stack trace from previous location where exception was thrown ---

The conjecture for this crash is that `CheckInvokerType()` from
5b945ab / [PR #1059][0] was responsible for the crash, because when
commit 55a2dd3 is present, invoker names would be *partially*
qualified, not fully qualified.

Pre-55a2dd3d:

    partial interface IComparable {
        [Register ("compareTo", "(Ljava/lang/Object;)I", "GetCompareTo_Ljava_lang_Object_Handler:Java.Lang.IComparableInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")]
        int CompareTo (Java.Lang.Object o);
    }

With 55a2dd3:

    partial interface IComparable {
        [Register ("compareTo", "(Ljava/lang/Object;)I", "GetCompareTo_Ljava_lang_Object_Handler:Java.Lang.IComparableInvoker, Mono.Android")]
        int CompareTo (Java.Lang.Object o);
    }

`CheckInvokerType()` will trim off `, PublicKeyToken` while leaving
the rest of the assembly qualified name intact.  Meaning it would be
comparing:

    Java.Lang.IComparableInvoker, Mono.Android, Version=0.0.0.0, Culture=neutral,

to:

    Java.Lang.IComparableInvoker, Mono.Android

which will never work.

Improve `CheckInvokerType()` and `TypeNameWithoutKey()` so that it
parses the assembly qualified name and generates *only* a partial-
assembly qualified name for comparison.

Hopefully this will work more sanely.

[0]: 4213a55
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

full-mono-integration-build For PRs; run a full build (~6-10h for mono bumps), not the faster PR subset (~2h for mono bumps) use-merge-commit Normally we squash-and-merge PRs. Use this label so we instead use a merge commit.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants