Skip to content

Conversation

@gugavaro
Copy link
Contributor

@gugavaro gugavaro commented Aug 15, 2019

Java allows public classes/interfaces to inherit/implements from "package-private" interfaces, in
which case the public & protected members of the "package-private"
interface are visible within the public class:

    // Java
    /* package */ interface BaseInterface {
     void foo();
      // ...
    }
    public interface TestInterface extends BaseInterface {
      // ...
   }
   public class TestClass implements TestInterface {
      // ...
    }

or

    public class TestClass implements BaseInterface {
      // ...
    }
 

The problem is that generator didn't properly support this construct,
and skips binding of "package-private" types, resulting in generated
C# code such as:

// C#
public partial interface TestInterface : BaseInterface {
  // CS0234: The type or namespace name `BaseInterface` does not exist in the namespace
}

Support this construct by updating generator to "copy" the members
from the "package-private" interface into the declaring interface:

// C#
public partial class TestClass : Java.Lang.Object {
  public void Foo() {
  // ...
}

This allows the generated code to compile without metadata fixup.

Specifics in implementing this:

  • Add a InterfaceGen.FixupAccessModifiers() to fix the private interfaces
  • Call FixupAccessModifiers() in the "validation" step
  • Add a property to expose the ImplementedInterfaces
  • In FixupAccessModifiers(), lookup the base interface of the current
    type and check if it is non-public.
  • Skip the package-private types, and replace them with that
    interface's base type
  • Look for each method on the base type, and copy it to the public
    type if it does not exist
  • Added tests for the follow scenarios
    • Private Interface -> Public Interface -> Public Class
    • Private Interface -> Public Class

@jonpryor jonpryor merged commit 2f2060f into master Aug 15, 2019
@jonpryor jonpryor deleted the gugavaro_private_interfaces branch August 15, 2019 02:26
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants