Skip to content

Commit 8a6f16e

Browse files
committed
[linker] improve method detection in FixAbstractMethodsStep
The forms team run into an issue with debugger in VS [1]. During investigation it turned out that our FixAbstractMethodsStep injects 2 methods, which were not needed, as the methods were already implemented. The bug itself looks like an issue, where original mdb file was used in the apk, instead of the one written by our linker. This change improves the method detection, where explicit interface methods name starts with `global::` [2]. In this particular case it were these methods: global::Android.Views.View.IOnTouchListener.OnTouch global::Android.Views.View.IOnClickListener.OnClick This is possibly compiler specific, so we rather look in MethodDefinition's Overrides to find the interface method and compare it to the interface method we are looking for. [1] https://bugzilla.xamarin.com/show_bug.cgi?id=59293 [2] excerpt from ikdasm output: .method private hidebysig newslot virtual final instance void 'global::Android.Views.View.IOnClickListener.OnClick'(class [Mono.Android]Android.Views.View v) cil managed { .override [Mono.Android]Android.Views.View/IOnClickListener::OnClick
1 parent 15f8793 commit 8a6f16e

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,23 @@ static bool CompareTypes (TypeReference iType, TypeReference tType)
9595
return true;
9696
}
9797

98+
bool IsInOverrides (MethodDefinition iMethod, MethodDefinition tMethod)
99+
{
100+
if (!tMethod.HasOverrides)
101+
return false;
102+
103+
foreach (var o in tMethod.Overrides)
104+
if (o != null && iMethod == o.Resolve ())
105+
return true;
106+
107+
return false;
108+
}
109+
98110
bool HaveSameSignature (TypeReference iface, MethodDefinition iMethod, MethodDefinition tMethod)
99111
{
112+
if (IsInOverrides (iMethod, tMethod))
113+
return true;
114+
100115
if (iMethod.Name != tMethod.Name && (iMethod.DeclaringType == null || (iMethod.DeclaringType.DeclaringType == null ? (string.Format ("{0}.{1}", iface.FullName, iMethod.Name) != tMethod.Name) : (string.Format ("{0}.{1}.{2}", iMethod.DeclaringType.DeclaringType, iface.Name, iMethod.Name) != tMethod.Name))))
101116
return false;
102117

tests/CodeGen-Binding/Xamarin.Android.FixJavaAbstractMethod-APIv1Binding/java/test/bindings/Cursor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public interface Cursor {
44
void method();
55
int methodWithRT ();
6+
int methodWithCursor (Cursor cursor);
67
int methodWithParams (int number, String text);
78
int methodWithParams (int number, String text, float real);
89
}

tests/CodeGen-Binding/Xamarin.Android.FixJavaAbstractMethod-APIv2Binding/java/test/bindings/Cursor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public interface Cursor {
55
void newMethod();
66
int methodWithRT ();
77
int newMethodWithRT ();
8+
int methodWithCursor (Cursor cursor);
89
int methodWithParams (int number, String text);
910
int newMethodWithParams (int number, String text);
1011
int methodWithParams (int number, String text, float real);

tests/CodeGen-Binding/Xamarin.Android.FixJavaAbstractMethod-Library/MyClrCursor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Library
55
{
6-
public class MyClrCursor : Java.Lang.Object, ICursor
6+
public class MyClrCursor : Java.Lang.Object, global::Test.Bindings.ICursor
77
{
88
public void Method ()
99
{
@@ -23,6 +23,11 @@ public int MethodWithRT ()
2323
{
2424
return 3;
2525
}
26+
27+
int global::Test.Bindings.ICursor.MethodWithCursor (global::Test.Bindings.ICursor cursor)
28+
{
29+
return 4;
30+
}
2631
}
2732
}
2833

tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/BindingTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public void JavaAbstractMethodTest ()
179179
if (e.GetType ().ToString () != "Java.Lang.AbstractMethodError")
180180
throw e;
181181
}
182+
183+
var mi = ic.GetType ().GetMethod ("MethodWithCursor");
184+
var mi2 = ic.GetType ().GetMethod ("global::Test.Bindings.ICursor.MethodWithCursor");
185+
if (!(mi == mi2))
186+
throw new Exception ("FixAbstractMethodStep broken, MethodWithRT added, while it should not be");
182187
}
183188

184189
// Context https://bugzilla.xamarin.com/show_bug.cgi?id=36036

0 commit comments

Comments
 (0)