Skip to content

Error with public class exposing inherited protected class methods with covariant return types #1116

Open
@jpobst

Description

@jpobst

Imagine we have the following Java:

public abstract class ClassA {
  protected abstract class ChildClass {
    public abstract ClassA createThing ();
  }
}

public class ClassB extends ClassA {
  public class ChildClass extends ClassA.ChildClass {
    @Override
    public ClassB createThing () { ... }
  }
}

There are 2 interesting things to "fix" when translating this to C#:

  1. public class ClassB.ChildClass inherits protected class ClassA.ChildClass, which C# doesn't allow
  2. ClassB.ChildClass.createThing () implements the abstract method ClassA.ChildClass.createThing (), but it has a covariant return type

The way we fix (1) is:

  • Make ClassB.ChildClass inherit from Object instead of ClassA.ChildClass
  • Copy members from ClassA.ChildClass to ClassB.ChildClass

If we didn't have the covariant return type (2), we would detect that ClassB.ChildClass.createThing () implements ClassA.ChildClass.createThing () and we would generate a virtual method.

However, because of the covariant return type, we cannot make the match and end up generating both a virtual and an abstract createThing ():

public abstract partial class ClassA : Java.Lang.Object {
  protected internal abstract partial class ChildClass : Java.Lang.Object {
    [Register ("createThing", "()Lexample/ClassA;", "GetCreateThingHandler")]
    public abstract ClassA CreateThing ();
  }
}

public partial class ClassB : ClassA {
  public new partial class ChildClass : Java.Lang.Object {
    [Register ("createThing", "()Lexample/ClassB;", "GetCreateThingHandler")]
    public virtual unsafe ClassB CreateThing () { ... }

    [Register ("createThing", "()Lexample/ClassA;", "GetCreateThingHandler")]
    public abstract ClassA CreateThing ();
  }
}

This fails with:

CS0513 'ClassB.ChildClass.CreateThing()' is abstract but it is contained in non-abstract type 

as well as several duplicate member errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugComponent does not function as intendedgeneratorIssues binding a Java library (generator, class-parse, etc.)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions