Skip to content

Commit 3c5cf34

Browse files
jonpryorjpobst
andauthored
[generator] Extend skipInvokerMethods support to interfaces. (#1202) (#1203)
Context: 73ebad2 Context: dotnet/android-libraries#779 Commit 73ebad2 added support for the metadata attribute `//class[@skipInvokerMethods]`, which allowed us to suppress generation of invoker methods for a class. The AndroidX Media3 binding in dotnet/android-libraries#779 hits a similar issue wherein we generate incorrect generics on an invoker type. However, this is an invoker type for an *`interface`*, not a `class`. Extend our `skipInvokerMethods` support so that it can be used as `//interface[@skipInvokerMethods]` as well. Co-authored-by: Jonathan Pobst <jonathan.pobst@microsoft.com>
1 parent bec0326 commit 3c5cf34

File tree

8 files changed

+49
-14
lines changed

8 files changed

+49
-14
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,32 @@ public void SkipInvokerMethodsMetadata ()
352352
Assert.False (writer.ToString ().Contains ("public abstract Com.Xamarin.Android.MyBaseClass DoStuff ();"), $"was: `{writer}`");
353353
}
354354

355+
[Test]
356+
public void SkipInterfaceInvokerMethodsMetadata ()
357+
{
358+
var xml = @"<api>
359+
<package name='java.lang' jni-name='java/lang'>
360+
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
361+
</package>
362+
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
363+
<interface abstract='true' 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'>
364+
<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>
365+
</interface>
366+
</package>
367+
</api>";
368+
369+
var gens = ParseApiDefinition (xml);
370+
var iface = gens.Single (g => g.Name == "IMyInterface");
371+
372+
generator.Context.ContextTypes.Push (iface);
373+
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
374+
generator.Context.ContextTypes.Pop ();
375+
376+
// Ensure the invoker for 'countAffectedRows' isn't generated
377+
Assert.False (writer.ToString ().Contains ("static Delegate cb_countAffectedRows;"), $"was: `{writer}`");
378+
Assert.False (writer.ToString ().Contains ("InvokeAbstractInt32Method"), $"was: `{writer}`");
379+
}
380+
355381
[Test]
356382
public void CompatVirtualMethod_Class ()
357383
{

tools/generator/Java.Interop.Tools.Generator.Importers/XmlApiImporter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ public static ClassGen CreateClass (XElement pkg, XElement elem, CodeGenerationO
117117
!options.SupportNestedInterfaceTypes
118118
};
119119

120-
if (elem.Attribute ("skipInvokerMethods")?.Value is string skip)
121-
foreach (var m in skip.Split (new char [] { ',', ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
122-
klass.SkippedInvokerMethods.Add (m);
123-
124120
FillApiSince (klass, pkg, elem);
125121
SetLineInfo (klass, elem, options);
126122

@@ -264,6 +260,10 @@ public static GenBaseSupport CreateGenBaseSupport (XElement pkg, XElement elem,
264260
Visibility = elem.XGetAttribute ("visibility")
265261
};
266262

263+
if (elem.Attribute ("skipInvokerMethods")?.Value is string skip)
264+
foreach (var m in skip.Split (new char [] { ',', ' ', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
265+
support.SkippedInvokerMethods.Add (m);
266+
267267
if (support.IsDeprecated) {
268268
support.DeprecatedComment = elem.XGetAttribute ("deprecated");
269269

tools/generator/Java.Interop.Tools.Generator.ObjectModel/ClassGen.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace MonoDroid.Generation
1111
public class ClassGen : GenBase
1212
{
1313
bool fill_explicit_implementation_started;
14-
HashSet<string> skipped_invoker_methods;
1514

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

@@ -356,8 +355,6 @@ public override void ResetValidation ()
356355
base.ResetValidation ();
357356
}
358357

359-
public HashSet<string> SkippedInvokerMethods => skipped_invoker_methods ??= new HashSet<string> ();
360-
361358
public override string ToNative (CodeGenerationOptions opt, string varname, Dictionary<string, string> mappings = null)
362359
{
363360
if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1) {

tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ bool ReturnTypeMatches (Method m, Method mm)
833833

834834
public bool ShouldGenerateAnnotationAttribute => IsAnnotation;
835835

836+
public HashSet<string> SkippedInvokerMethods => support.SkippedInvokerMethods;
837+
836838
public void StripNonBindables (CodeGenerationOptions opt)
837839
{
838840
// Strip out default interface methods if not desired

tools/generator/Java.Interop.Tools.Generator.ObjectModel/GenBaseSupport.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace MonoDroid.Generation
45
{
56
public class GenBaseSupport
67
{
8+
HashSet<string> skipped_invoker_methods;
9+
710
public string AnnotatedVisibility { get; set; }
811
public bool IsAcw { get; set; }
912
public bool IsDeprecated { get; set; }
@@ -21,6 +24,8 @@ public class GenBaseSupport
2124
public string Visibility { get; set; }
2225
public GenericParameterDefinitionList TypeParameters { get; set; }
2326

27+
public HashSet<string> SkippedInvokerMethods => skipped_invoker_methods ??= new HashSet<string> ();
28+
2429
public virtual bool OnValidate (CodeGenerationOptions opt)
2530
{
2631
// See com.google.inject.internal.util package for this case.

tools/generator/Java.Interop.Tools.Generator.ObjectModel/Method.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public string GetMetadataXPathReference (GenBase declaringType) =>
193193

194194
public string GetSignature () => $"n_{JavaName}:{JniSignature}:{ConnectorName}";
195195

196+
public string GetSkipInvokerSignature () => $"{DeclaringType.RawJniName}.{JavaName}{JniSignature}";
197+
196198
public bool IsEventHandlerWithHandledProperty => RetVal.JavaName == "boolean" && EventName != "";
197199

198200
public override bool IsGeneric => base.IsGeneric || RetVal.IsGeneric;

tools/generator/SourceWriters/ClassInvokerClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void AddPropertyInvokers (ClassGen klass, IEnumerable<Property> properties, Hash
103103
void AddMethodInvokers (ClassGen klass, IEnumerable<Method> methods, HashSet<string> members, HashSet<string> skipInvokers, InterfaceGen gen, CodeGenerationOptions opt)
104104
{
105105
foreach (var m in methods) {
106-
if (skipInvokers.Contains ($"{m.DeclaringType.RawJniName}.{m.JavaName}{m.JniSignature}"))
106+
if (skipInvokers.Contains (m.GetSkipInvokerSignature ()))
107107
continue;
108108

109109
var sig = m.GetSignature ();

tools/generator/SourceWriters/InterfaceInvokerClass.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ public InterfaceInvokerClass (InterfaceGen iface, CodeGenerationOptions opt, Cod
5555

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

58-
AddMemberInvokers (iface, new HashSet<string> (), opt, context);
58+
AddMemberInvokers (iface, new HashSet<string> (), iface.SkippedInvokerMethods, opt, context);
5959
}
6060

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

6767
foreach (var i in iface.GetAllDerivedInterfaces ()) {
6868
AddPropertyInvokers (iface, i.Properties.Where (p => !p.Getter.IsStatic && !p.Getter.IsInterfaceDefaultMethod), members, opt, context);
69-
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);
69+
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);
7070
AddCharSequenceEnumerators (i);
7171
}
7272
}
@@ -90,10 +90,13 @@ void AddPropertyInvokers (InterfaceGen iface, IEnumerable<Property> properties,
9090
Properties.Add (new InterfaceInvokerProperty (iface, prop, opt, context));
9191
}
9292
}
93-
94-
void AddMethodInvokers (InterfaceGen iface, IEnumerable<Method> methods, HashSet<string> members, CodeGenerationOptions opt, CodeGeneratorContext context)
93+
94+
void AddMethodInvokers (InterfaceGen iface, IEnumerable<Method> methods, HashSet<string> members, HashSet<string> skipInvokers, CodeGenerationOptions opt, CodeGeneratorContext context)
9595
{
9696
foreach (var m in methods) {
97+
if (skipInvokers.Contains (m.GetSkipInvokerSignature ()))
98+
continue;
99+
97100
var sig = m.GetSignature ();
98101

99102
if (members.Contains (sig))

0 commit comments

Comments
 (0)