Skip to content

[generator] Add support for @explicitInterface metadata. #1006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,156 @@ public void InheritedInterfaceAsClass ()

Assert.Pass ("WriteType did not NRE");
}

[Test]
public void ExplicitInterfaceMetadata_InterfaceMethod ()
{
var xml = @"<api>
<package name='java.lang' jni-name='java/lang'>
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
</package>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<interface abstract='false' deprecated='not deprecated' extends='java.lang.Object' extends-generic-aware='java.lang.Object' jni-extends='Ljava/lang/Object;' final='false' name='MyInterface' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyInterface;'>
<method abstract='true' deprecated='not deprecated' final='false' name='countAffectedRows' jni-signature='()I' bridge='false' native='false' return='int' jni-return='I' static='false' synchronized='false' synthetic='false' visibility='public' explicitInterface='IRowCounter'></method>
</interface>
</package>
</api>";

var gens = ParseApiDefinition (xml);
var iface = gens.Single (g => g.Name == "IMyInterface");

generator.Context.ContextTypes.Push (iface);
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// Ensure explicit interface was written
Assert.True (writer.ToString ().Contains ("int IRowCounter.CountAffectedRows ("), $"was: `{writer}`");
}

[Test]
public void ExplicitInterfaceMetadata_InterfaceProperty ()
{
var xml = @"<api>
<package name='java.lang' jni-name='java/lang'>
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
</package>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<interface abstract='false' deprecated='not deprecated' extends='java.lang.Object' extends-generic-aware='java.lang.Object' jni-extends='Ljava/lang/Object;' final='false' name='MyInterface' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyInterface;'>
<method abstract='true' deprecated='not deprecated' final='false' name='getAge' jni-signature='()I' bridge='false' native='false' return='int' jni-return='I' static='false' synchronized='false' synthetic='false' visibility='public' explicitInterface='IHasAge'></method>
</interface>
</package>
</api>";

var gens = ParseApiDefinition (xml);
var iface = gens.Single (g => g.Name == "IMyInterface");

generator.Context.ContextTypes.Push (iface);
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// Ensure explicit interface was written
Assert.True (writer.ToString ().Contains ("int IHasAge.Age {"), $"was: `{writer}`");
}

[Test]
public void ExplicitInterfaceMetadata_ClassMethod ()
{
var xml = @"<api>
<package name='java.lang' jni-name='java/lang'>
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
</package>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<class abstract='false' deprecated='not deprecated' extends='java.lang.Object' extends-generic-aware='java.lang.Object' jni-extends='Ljava/lang/Object;' final='false' name='MyClass' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyClass;'>
<method abstract='false' deprecated='not deprecated' final='false' name='countAffectedRows' jni-signature='()I' bridge='false' native='false' return='int' jni-return='I' static='false' synchronized='false' synthetic='false' visibility='public' explicitInterface='IRowCounter'></method>
</class>
</package>
</api>";

var gens = ParseApiDefinition (xml);
var iface = gens.Single (g => g.Name == "MyClass");

generator.Context.ContextTypes.Push (iface);
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// Ensure explicit interface was written
Assert.True (writer.ToString ().Contains ("int IRowCounter.CountAffectedRows ("), $"was: `{writer}`");
}

[Test]
public void ExplicitInterfaceMetadata_ClassProperty ()
{
var xml = @"<api>
<package name='java.lang' jni-name='java/lang'>
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
</package>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<class abstract='false' deprecated='not deprecated' extends='java.lang.Object' extends-generic-aware='java.lang.Object' jni-extends='Ljava/lang/Object;' final='false' name='MyClass' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyClass;'>
<method abstract='false' deprecated='not deprecated' final='false' name='getAge' jni-signature='()I' bridge='false' native='false' return='int' jni-return='I' static='false' synchronized='false' synthetic='false' visibility='public' explicitInterface='IHasAge'></method>
</class>
</package>
</api>";

var gens = ParseApiDefinition (xml);
var iface = gens.Single (g => g.Name == "MyClass");

generator.Context.ContextTypes.Push (iface);
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// Ensure explicit interface was written
Assert.True (writer.ToString ().Contains ("int IHasAge.Age {"), $"was: `{writer}`");
}

[Test]
public void ExplicitInterfaceMetadata_AbstractClassMethod ()
{
var xml = @"<api>
<package name='java.lang' jni-name='java/lang'>
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
</package>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<class abstract='false' deprecated='not deprecated' extends='java.lang.Object' extends-generic-aware='java.lang.Object' jni-extends='Ljava/lang/Object;' final='false' name='MyClass' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyClass;'>
<method abstract='true' deprecated='not deprecated' final='false' name='countAffectedRows' jni-signature='()I' bridge='false' native='false' return='int' jni-return='I' static='false' synchronized='false' synthetic='false' visibility='public' explicitInterface='IRowCounter'></method>
</class>
</package>
</api>";

var gens = ParseApiDefinition (xml);
var iface = gens.Single (g => g.Name == "MyClass");

generator.Context.ContextTypes.Push (iface);
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// Ensure explicit interface was written
Assert.True (writer.ToString ().Contains ("abstract int IRowCounter.CountAffectedRows ("), $"was: `{writer}`");
}

[Test]
public void ExplicitInterfaceMetadata_AbstractClassProperty ()
{
var xml = @"<api>
<package name='java.lang' jni-name='java/lang'>
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
</package>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<class abstract='false' deprecated='not deprecated' extends='java.lang.Object' extends-generic-aware='java.lang.Object' jni-extends='Ljava/lang/Object;' final='false' name='MyClass' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyClass;'>
<method abstract='true' deprecated='not deprecated' final='false' name='getAge' jni-signature='()I' bridge='false' native='false' return='int' jni-return='I' static='false' synchronized='false' synthetic='false' visibility='public' explicitInterface='IHasAge'></method>
</class>
</package>
</api>";

var gens = ParseApiDefinition (xml);
var iface = gens.Single (g => g.Name == "MyClass");

generator.Context.ContextTypes.Push (iface);
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

// Ensure explicit interface was written
Assert.True (writer.ToString ().Contains ("abstract int IHasAge.Age {"), $"was: `{writer}`");
}
}

[TestFixture]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ public static Method CreateMethod (GenBase declaringType, XElement elem, CodeGen
ArgsType = elem.Attribute ("argsType")?.Value,
CustomAttributes = elem.XGetAttribute ("customAttributes"),
Deprecated = elem.Deprecated (),
ExplicitInterface = elem.XGetAttribute ("explicitInterface"),
EventName = elem.Attribute ("eventName")?.Value,
GenerateAsyncWrapper = elem.Attribute ("generateAsyncWrapper") != null,
GenerateDispatchingSetter = elem.Attribute ("generateDispatchingSetter") != null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Method (GenBase declaringType) : base (declaringType)
public bool ReturnNotNull { get; set; }
public ReturnValue RetVal { get; set; }
public int SourceApiLevel { get; set; }
public string ExplicitInterface { get; set; }

// it used to be private though...
internal string AdjustedName => IsReturnCharSequence ? Name + "Formatted" : Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ public void AutoDetectEnumifiedOverrideProperties (AncestorDescendantCache cache
}
}
}

public string ExplicitInterface => Getter?.ExplicitInterface ?? Setter?.ExplicitInterface;
}
}
1 change: 1 addition & 0 deletions tools/generator/SourceWriters/BoundAbstractProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class BoundAbstractProperty : PropertyWriter
public BoundAbstractProperty (GenBase gen, Property property, CodeGenerationOptions opt)
{
Name = property.AdjustedName;
ExplicitInterfaceImplementation = property.ExplicitInterface;
PropertyType = new TypeReferenceWriter (opt.GetTypeReferenceName (property.Getter.RetVal));

SetVisibility (property.Getter.RetVal.IsGeneric ? "protected" : property.Getter.Visibility);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public BoundInterfaceMethodDeclaration (Method method, string adapter, CodeGener
this.opt = opt;

Name = method.AdjustedName;
ExplicitInterfaceImplementation = method.ExplicitInterface;

ReturnType = new TypeReferenceWriter (opt.GetTypeReferenceName (method.RetVal));
IsDeclaration = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class BoundInterfacePropertyDeclaration : PropertyWriter
public BoundInterfacePropertyDeclaration (GenBase gen, Property property, string adapter, CodeGenerationOptions opt)
{
Name = property.AdjustedName;
ExplicitInterfaceImplementation = property.ExplicitInterface;

PropertyType = new TypeReferenceWriter (opt.GetTypeReferenceName (property));
IsAutoProperty = true;
Expand Down
4 changes: 4 additions & 0 deletions tools/generator/SourceWriters/BoundMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public BoundMethod (GenBase type, Method method, CodeGenerationOptions opt, bool
if (is_explicit)
ExplicitInterfaceImplementation = GetDeclaringTypeOfExplicitInterfaceMethod (method.OverriddenInterfaceMethod);

// Allow user to override our explicit interface logic
if (method.ExplicitInterface.HasValue ())
ExplicitInterfaceImplementation = method.ExplicitInterface;

if ((IsVirtual || !IsOverride) && type.RequiresNew (method.AdjustedName, method))
IsShadow = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public BoundMethodAbstractDeclaration (GenBase gen, Method method, CodeGeneratio
}

Name = method.AdjustedName;
ExplicitInterfaceImplementation = method.ExplicitInterface;

IsAbstract = true;
IsShadow = impl.RequiresNew (method.Name, method);
Expand Down
1 change: 1 addition & 0 deletions tools/generator/SourceWriters/BoundProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class BoundProperty : PropertyWriter
public BoundProperty (GenBase gen, Property property, CodeGenerationOptions opt, bool withCallbacks = true, bool forceOverride = false)
{
Name = property.AdjustedName;
ExplicitInterfaceImplementation = property.ExplicitInterface;
PropertyType = new TypeReferenceWriter (opt.GetTypeReferenceName (property.Getter.RetVal));

SetVisibility (gen is InterfaceGen ? string.Empty : property.Getter.IsAbstract && property.Getter.RetVal.IsGeneric ? "protected" : (property.Setter ?? property.Getter).Visibility);
Expand Down