Repository navigation
[java.interop] improve JavaCast() for interfaces - #1059
Conversation
| 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); |
There was a problem hiding this comment.
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);
@radekdoulik: How hard would it be to do that? I think such a change should "belong" to this PR, to validate the underlying change. |
539eb8b to
440b0ae
Compare
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.
440b0ae to
a42eadc
Compare
|
It's weird I would think there would be similar code for |
|
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. |
|
After discussion with @jonpryor I will try to remove the code which preserves the whole |
|
@radekdoulik do you think I should change this PR to not keep |
|
Leave it for now. I am trying the modified linker and will see if we need to change more code. |
|
OK, let see if the tests pass. I checked it locally and the linked Mono.Android.dll doesn't preserve |
b937b7f to
5a68209
Compare
|
@monojenkins build macOS+xbuild PR Build |
5a68209 to
0ec11d4
Compare
|
@monojenkins build macOS+xbuild PR Build |
| if (!m.TryGetRegisterAttribute (out registerAttr)) | ||
| continue; | ||
|
|
||
| if (registerAttr.ConstructorArguments.Count != 3) |
There was a problem hiding this comment.
Couldn't this use TryGetRegisterMember()?
There was a problem hiding this comment.
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 (':'); |
There was a problem hiding this comment.
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.
0ec11d4 to
4213a55
Compare
|
build |
|
build |
|
Looks like the |
|
Sorry. :-( |
Changes: https://github.com/xamarin/monodroid/compare/2efbdd3e...0f6725ed Fixes: https://work.azdo.io/1041849 Fixes: https://developercommunity.visualstudio.com/content/problem/858509/adb0030-monoandroidtoolsrequiresuninstallexception-2.html * xamarin/monodroid@0f6725edf: [tools/msbuild] prevent InstallPackageAssemblies from reporting two errors (#1058) * xamarin/monodroid@16fd3b845: Bump to xamarin/androidtools/master@d221090 (#1059)
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
Currently, interfaces generate
Invokerclasses such as:JavaCastwas previously looking up theseInvokertypes and callingthe
GetObjectmethod via reflection. We can simplify this by just callingJava.Lang.Object.GetObject()directly—bypassing all the reflection.One day we might be able to drop the generated
Invokermethod fromgenerator, 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 nownot used anymore.