Skip to content

Commit

Permalink
[generator] Extend skipInvokerMethods support to interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Mar 5, 2024
1 parent 14a9470 commit 3b5f684
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
26 changes: 26 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,32 @@ public void SkipInvokerMethodsMetadata ()
Assert.False (writer.ToString ().Contains ("public abstract Com.Xamarin.Android.MyBaseClass DoStuff ();"), $"was: `{writer}`");
}

[Test]
public void SkipInterfaceInvokerMethodsMetadata ()
{
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;' skipInvokerMethods='com/xamarin/android/MyInterface.countAffectedRows()I'>
<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'></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 the invoker for 'countAffectedRows' isn't generated
Assert.False (writer.ToString ().Contains ("static Delegate cb_countAffectedRows;"), $"was: `{writer}`");
Assert.False (writer.ToString ().Contains ("InvokeAbstractInt32Method"), $"was: `{writer}`");
}

[Test]
public void CompatVirtualMethod_Class ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ public static InterfaceGen CreateInterface (XElement pkg, XElement elem, CodeGen
!options.SupportNestedInterfaceTypes
};

if (elem.Attribute ("skipInvokerMethods")?.Value is string skip)
foreach (var m in skip.Split (new char [] { ',', ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
iface.SkippedInvokerMethods.Add (m);

FillApiSince (iface, pkg, elem);
SetLineInfo (iface, elem, options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace MonoDroid.Generation
public class ClassGen : GenBase
{
bool fill_explicit_implementation_started;
HashSet<string> skipped_invoker_methods;

public List<Ctor> Ctors { get; private set; } = new List<Ctor> ();

Expand Down Expand Up @@ -356,8 +355,6 @@ public override void ResetValidation ()
base.ResetValidation ();
}

public HashSet<string> SkippedInvokerMethods => skipped_invoker_methods ??= new HashSet<string> ();

public override string ToNative (CodeGenerationOptions opt, string varname, Dictionary<string, string> mappings = null)
{
if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public abstract class GenBase : IGeneratable, ApiVersionsSupport.IApiAvailabilit
protected bool iface_validation_failed;
protected GenBaseSupport support;
protected bool validated = false;
HashSet<string> skipped_invoker_methods;

readonly List<string> implemented_interfaces = new List<string> ();
readonly Dictionary<string, Method> jni_sig_hash = new Dictionary<string, Method> ();
Expand Down Expand Up @@ -854,6 +855,8 @@ bool ReturnTypeMatches (Method m, Method mm)

public bool ShouldGenerateAnnotationAttribute => IsAnnotation;

public HashSet<string> SkippedInvokerMethods => skipped_invoker_methods ??= new HashSet<string> ();

public void StripNonBindables (CodeGenerationOptions opt)
{
// Strip out default interface methods if not desired
Expand Down
15 changes: 9 additions & 6 deletions tools/generator/SourceWriters/InterfaceInvokerClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public InterfaceInvokerClass (InterfaceGen iface, CodeGenerationOptions opt, Cod

Constructors.Add (new InterfaceInvokerConstructor (opt, iface, context));

AddMemberInvokers (iface, new HashSet<string> (), opt, context);
AddMemberInvokers (iface, new HashSet<string> (), iface.SkippedInvokerMethods, opt, context);
}

static HashSet<InterfaceGen> GetCompleteImplementedInterfaces (HashSet<InterfaceGen> ifaces, InterfaceGen toplevel)
Expand All @@ -81,15 +81,15 @@ static HashSet<InterfaceGen> GetCompleteImplementedInterfaces (HashSet<Interface
return ifaces;
}

void AddMemberInvokers (InterfaceGen iface, HashSet<string> members, CodeGenerationOptions opt, CodeGeneratorContext context)
void AddMemberInvokers (InterfaceGen iface, HashSet<string> members, HashSet<string> skipInvokers, CodeGenerationOptions opt, CodeGeneratorContext context)
{
AddPropertyInvokers (iface, iface.Properties.Where (p => !p.Getter.IsStatic && !p.Getter.IsInterfaceDefaultMethod), members, opt, context);
AddMethodInvokers (iface, iface.Methods.Where (m => !m.IsStatic && !m.IsInterfaceDefaultMethod), members, opt, context);
AddMethodInvokers (iface, iface.Methods.Where (m => !m.IsStatic && !m.IsInterfaceDefaultMethod), members, skipInvokers, opt, context);
AddCharSequenceEnumerators (iface);

foreach (var i in iface.GetAllDerivedInterfaces ()) {
AddPropertyInvokers (iface, i.Properties.Where (p => !p.Getter.IsStatic && !p.Getter.IsInterfaceDefaultMethod), members, opt, context);
AddMethodInvokers (iface, i.Methods.Where (m => !m.IsStatic && !m.IsInterfaceDefaultMethod && !iface.IsCovariantMethod (m) && !(i.FullName.StartsWith ("Java.Lang.ICharSequence", StringComparison.Ordinal) && m.Name.EndsWith ("Formatted", StringComparison.Ordinal))), members, opt, context);
AddMethodInvokers (iface, i.Methods.Where (m => !m.IsStatic && !m.IsInterfaceDefaultMethod && !iface.IsCovariantMethod (m) && !(i.FullName.StartsWith ("Java.Lang.ICharSequence", StringComparison.Ordinal) && m.Name.EndsWith ("Formatted", StringComparison.Ordinal))), members, skipInvokers, opt, context);
AddCharSequenceEnumerators (i);
}
}
Expand All @@ -113,10 +113,13 @@ void AddPropertyInvokers (InterfaceGen iface, IEnumerable<Property> properties,
Properties.Add (new InterfaceInvokerProperty (iface, prop, opt, context));
}
}

void AddMethodInvokers (InterfaceGen iface, IEnumerable<Method> methods, HashSet<string> members, CodeGenerationOptions opt, CodeGeneratorContext context)
void AddMethodInvokers (InterfaceGen iface, IEnumerable<Method> methods, HashSet<string> members, HashSet<string> skipInvokers, CodeGenerationOptions opt, CodeGeneratorContext context)
{
foreach (var m in methods) {
if (skipInvokers.Contains ($"{m.DeclaringType.RawJniName}.{m.JavaName}{m.JniSignature}"))
continue;

var sig = m.GetSignature ();

if (members.Contains (sig))
Expand Down

0 comments on commit 3b5f684

Please sign in to comment.