Repository navigation
Metadata support for //method/@compatVirtualMethod #810
Description
Context: https://github.com/xamarin/xamarin-android/blob/dcd1cb78f67652bb53289e5dd67bc10350513931/Documentation/workflow/mono-android-api-compatibility.md
Context?! #509
One of the reasons we needed C#8 default interface method support was to support adding new members to interfaces without breaking API (source) compatibility. (ABI (binary) compatibility was preserved via a build-time link step in FixAbstractMethodsStep; see also dotnet/android@f96fcf93):
// API-1
public interface Cursor {
void close();
}
// API-11
public interface Cursor {
void close();
void getType(int columnIndex);
}Xamarin.Android handled this by (ab?)using $(TargetFrameworkVersion): each $(TargetFrameworkVersion) corresponded to a different API level binding. Build against v2.3 (API-10), and Cursor.getType(int) was not available. Build against v3.0 or later (API-11+), and Cursor.getType() was bound.
This meant that changing the $(TargetFrameworkVersion) could result in required source code changes, as "new" abstract members became visible.
Now that we have default interface member support (29f9707), we need to support maintaining source compatibility:
- When
api-mergeruns, it can determine when new abstract members are added to a type (class or interface). When this is detected, it should set new metadata,//method/@compatVirtualMethod=true. - When
generatorruns, it should change what it emits.
Imagine:
<type abstract="true" name="Example" …>
<method abstract="true" returns="void" name="m" … />
</type>where <type/> can be <class/> or <interface/>.
When //class/method[@abstract='true' and @compatVirtualMethod='true'] is encountered, instead of:
[Register(…)]
abstract partial class Example {
[Register(…)]
public abstract void M();
}generator should instead emit (as per mono-android-api-compatibility.md):
[Register(…)]
abstract partial class Example {
[Register(…)]
public virtual void M()
{
try {
const string __id = "m.()V";
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, null);
}
catch (Java.Lang.NoSuchMethodError) {
throw new Java.Lang.AbstractMethodError ();
}
finally {
GC.KeepAlive (this);
}
}
}When //interface/method[@abstract='true' and @compatVirtualMethod='true'] is encountered, instead of:
[Register(…)]
partial interface IExample {
[Register(…)]
void M();
}generator should instead emit:
[Register(…)]
partial interface IExample {
[Register(…)]
virtual unsafe void M()
{
try {
const string __id = "m.()V";
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, null);
}
catch (Java.Lang.NoSuchMethodError) {
throw new Java.Lang.AbstractMethodError ();
}
finally {
GC.KeepAlive (this);
}
}
}
Activity
[Mono.Android] Add Android API-S Developer Preview 1 binding (#5653)
[generator] Add support for 'compatVirtualMethod'. (#1088)
[generator] Add support for 'compatVirtualMethod'. (#1088)
[generator] Add support for 'compatVirtualMethod'. (#1088)
[generator] Add support for 'compatVirtualMethod'. (#1088)