Skip to content

Commit fe60483

Browse files
authored
[generator] Mark abstract methods as [Obsolete] if needed (#1011)
Fixes: #969 If you have a Java `abstract` method which is deprecated: // Java public abstract class Example { @deprecated public abstract void m(); } then the C# binding is *not* `[Obsolete]`, *and* the `*Invoker` override *is* `[Obsolete]`: // Binding public abstract partial class Example : Java.Lang.Object { // Note: *not* [Obsolete] [Register (…)] public abstract void M(); } internal partial class ExampleInvoker : Example { [Obsolete] public override void M() => … } This state of affairs results in a [CS0809 warning][0]: warning CS0809: Obsolete member 'ExampleInvoker.M()' overrides non-obsolete member 'Example.M()' Commit 37cff25 added a fix for this for `JavaInterop1` output, asking: > TODO: should this change be done for *all* codegen targets? The answer: Yes. Expand the fix from 37cff25 to apply to `XAJavaInterop1` codegen, and add unit tests for this scenario. [0]: https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0809
1 parent 275fa75 commit fe60483

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,31 @@ public void ExplicitInterfaceMetadata_AbstractClassProperty ()
502502
// Ensure explicit interface was written
503503
Assert.True (writer.ToString ().Contains ("abstract int IHasAge.Age {"), $"was: `{writer}`");
504504
}
505+
506+
[Test]
507+
public void ObsoleteBoundMethodAbstractDeclaration ()
508+
{
509+
var xml = @"<api>
510+
<package name='java.lang' jni-name='java/lang'>
511+
<class abstract='false' deprecated='not deprecated' final='false' name='Object' static='false' visibility='public' jni-signature='Ljava/lang/Object;' />
512+
</package>
513+
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
514+
<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;'>
515+
<method abstract='true' deprecated='This is so old!' 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>
516+
</class>
517+
</package>
518+
</api>";
519+
520+
var gens = ParseApiDefinition (xml);
521+
var iface = gens.Single (g => g.Name == "MyClass");
522+
523+
generator.Context.ContextTypes.Push (iface);
524+
generator.WriteType (iface, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
525+
generator.Context.ContextTypes.Pop ();
526+
527+
// Ensure [Obsolete] was written
528+
Assert.True (writer.ToString ().Contains ("[Obsolete (@\"This is so old!\")]"), writer.ToString ());
529+
}
505530
}
506531

507532
[TestFixture]

tools/generator/SourceWriters/BoundMethodAbstractDeclaration.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ public BoundMethodAbstractDeclaration (GenBase gen, Method method, CodeGeneratio
5353
if (method.DeclaringType.IsGeneratable)
5454
Comments.Add ($"// Metadata.xml XPath method reference: path=\"{method.GetMetadataXPathReference (method.DeclaringType)}\"");
5555

56-
// TODO: shouldn't `[Obsolete]` be added for *all* CodeGenerationTargets?
57-
if (opt.CodeGenerationTarget == CodeGenerationTarget.JavaInterop1 && method.Deprecated.HasValue ()) {
56+
if (method.Deprecated.HasValue ())
5857
Attributes.Add (new ObsoleteAttr (method.Deprecated.Replace ("\"", "\"\"")));
59-
}
6058

6159
SourceWriterExtensions.AddSupportedOSPlatform (Attributes, method, opt);
6260

0 commit comments

Comments
 (0)