Skip to content
This repository was archived by the owner on Nov 9, 2026. It is now read-only.
This repository was archived by the owner on Nov 9, 2026. It is now read-only.

Metadata support for //method/@compatVirtualMethod #810

Closed

Description

@jonpryor

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:

  1. When api-merge runs, 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.
  2. When generator runs, 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

  1. added this to the Under consideration milestone on Mar 1, 2021
  2. removed their assignment
    on Mar 11, 2021
  3. added
    enhancement
    generator
    on May 5, 2021
  4. locked and limited conversation to collaborators on Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgenerator

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions