Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

Commit 120d8a7

Browse files
authored
[generator] Add more [ObsoleteOSPlatform] to prevent CA1422 (#1078)
Context: dotnet/android#7590 Context: dotnet/android@7004467 While building `Mono.Android.dll`, we had 1462 `CA1422` warnings of the following caused by an interface being marked as `[ObsoleteOSPlatform]` but the interface invoker class is not similarly annotated: …\xamarin-android\src\Mono.Android\obj\Debug\net8.0\android-33\mcw\Org.Apache.Commons.Logging.ILog.cs(128,11): warning CA1422: This call site is reachable on: 'Android' 21.0 and later. 'ILog' is obsoleted on: 'Android' 22.0 and later (This class is obsoleted in this android platform). There were also 42 `CA1422` warnings caused by interface async extension classes not having `[ObsoleteOSPlatform]` attributes: …\xamarin-android\src\Mono.Android\obj\Debug\net8.0\android-33\mcw\Org.Apache.Http.IO.ISessionOutputBuffer.cs(52,58): warning CA1422: This call site is reachable on: 'Android' 21.0 and later. 'ISessionOutputBuffer.Write(byte[]?, int, int)' is obsoleted on: 'Android' 22.0 and later (This class is obsoleted in this android platform). These warnings were disabled in dotnet/android@70044670. Fix these warnings by adding the `[ObsoleteOSPlatform]` attribute to these types if the source interface is deprecated.
1 parent 8a1ae57 commit 120d8a7

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,36 @@ public void ObsoletedOSPlatformAttributeUnneededSupport ()
611611
Assert.True(writer.ToString().Contains("[global::System.Obsolete (@\"This method has an invalid deprecated-since!\")]"),writer.ToString());
612612
}
613613

614+
[Test]
615+
publicvoidObsoletedOSPlatformAttributeInterfaceInfrastructureSupport()
616+
{
617+
varxml=@"<api>
618+
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
619+
<interface abstract='false' deprecated='This interface was deprecated in API-25' final='false' name='MyType' static='false' visibility='public' jni-signature='Lcom/xamarin/android/MyType;' deprecated-since='25' />
620+
</package>
621+
</api>";
622+
623+
options.UseObsoletedOSPlatformAttributes=true;
624+
625+
vargens=ParseApiDefinition(xml);
626+
variface=gens.OfType<InterfaceGen>().Single(g =>g.Name=="IMyType");
627+
628+
generator.Context.ContextTypes.Push(iface);
629+
varinvoker=newInterfaceInvokerClass(iface,options,generator.Context);
630+
varextensions=newInterfaceExtensionsClass(iface,iface.Name,options);
631+
generator.Context.ContextTypes.Pop();
632+
633+
// Ensure attribute was added to invoker class
634+
varinvoker_attribute=invoker.Attributes.OfType<ObsoletedOSPlatformAttr>().Single();
635+
Assert.AreEqual("This interface was deprecated in API-25",invoker_attribute.Message);
636+
Assert.AreEqual(25,invoker_attribute.Version);
637+
638+
// Ensure attribute was added to extensions class
639+
varextensions_attribute=invoker.Attributes.OfType<ObsoletedOSPlatformAttr>().Single();
640+
Assert.AreEqual("This interface was deprecated in API-25",extensions_attribute.Message);
641+
Assert.AreEqual(25,extensions_attribute.Version);
642+
}
643+
614644
[Test]
615645
publicvoidObsoleteGetterOnlyProperty()
616646
{

‎tools/generator/SourceWriters/InterfaceExtensionsClass.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public InterfaceExtensionsClass (InterfaceGen iface, string declaringTypeName, C
1818
IsStatic=true;
1919
IsPartial=true;
2020

21+
SourceWriterExtensions.AddObsolete(Attributes,iface.DeprecatedComment,opt,iface.IsDeprecated,deprecatedSince:iface.DeprecatedSince);
22+
2123
foreach(varmethodiniface.Methods.Where(m =>!m.IsStatic)){
2224
if(method.CanHaveStringOverload){
2325
// TODO: Don't allow obsolete here to match old generator.

‎tools/generator/SourceWriters/InterfaceInvokerClass.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public InterfaceInvokerClass (InterfaceGen iface, CodeGenerationOptions opt, Cod
3232
MemberType=(!ji)?null:(MemberTypes?)MemberTypes.TypeInfo,
3333
});
3434

35+
SourceWriterExtensions.AddObsolete(Attributes,iface.DeprecatedComment,opt,iface.IsDeprecated,deprecatedSince:iface.DeprecatedSince);
36+
3537
Fields.Add(newPeerMembersField(opt,iface.RawJniName,$"{iface.Name}Invoker",false));
3638

3739
if(!ji){

0 commit comments

Comments
 (0)