Skip to content

Commit

Permalink
[generator] Add more [ObsoleteOSPlatform] to prevent CA1422 (dotnet#1078
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
jpobst authored Jan 26, 2023
1 parent 8a1ae57 commit 120d8a7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,36 @@ public void ObsoletedOSPlatformAttributeUnneededSupport ()
Assert.True (writer.ToString ().Contains ("[global::System.Obsolete (@\"This method has an invalid deprecated-since!\")]"), writer.ToString ());
}

[Test]
public void ObsoletedOSPlatformAttributeInterfaceInfrastructureSupport ()
{
var xml = @"<api>
<package name='com.xamarin.android' jni-name='com/xamarin/android'>
<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' />
</package>
</api>";

options.UseObsoletedOSPlatformAttributes = true;

var gens = ParseApiDefinition (xml);
var iface = gens.OfType<InterfaceGen> ().Single (g => g.Name == "IMyType");

generator.Context.ContextTypes.Push (iface);
var invoker = new InterfaceInvokerClass (iface, options, generator.Context);
var extensions = new InterfaceExtensionsClass (iface, iface.Name, options);
generator.Context.ContextTypes.Pop ();

// Ensure attribute was added to invoker class
var invoker_attribute = invoker.Attributes.OfType<ObsoletedOSPlatformAttr> ().Single ();
Assert.AreEqual ("This interface was deprecated in API-25", invoker_attribute.Message);
Assert.AreEqual (25, invoker_attribute.Version);

// Ensure attribute was added to extensions class
var extensions_attribute = invoker.Attributes.OfType<ObsoletedOSPlatformAttr> ().Single ();
Assert.AreEqual ("This interface was deprecated in API-25", extensions_attribute.Message);
Assert.AreEqual (25, extensions_attribute.Version);
}

[Test]
public void ObsoleteGetterOnlyProperty ()
{
Expand Down
2 changes: 2 additions & 0 deletions tools/generator/SourceWriters/InterfaceExtensionsClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public InterfaceExtensionsClass (InterfaceGen iface, string declaringTypeName, C
IsStatic = true;
IsPartial = true;

SourceWriterExtensions.AddObsolete (Attributes, iface.DeprecatedComment, opt, iface.IsDeprecated, deprecatedSince: iface.DeprecatedSince);

foreach (var method in iface.Methods.Where (m => !m.IsStatic)) {
if (method.CanHaveStringOverload) {
// TODO: Don't allow obsolete here to match old generator.
Expand Down
2 changes: 2 additions & 0 deletions tools/generator/SourceWriters/InterfaceInvokerClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public InterfaceInvokerClass (InterfaceGen iface, CodeGenerationOptions opt, Cod
MemberType = (!ji) ? null : (MemberTypes?) MemberTypes.TypeInfo,
});

SourceWriterExtensions.AddObsolete (Attributes, iface.DeprecatedComment, opt, iface.IsDeprecated, deprecatedSince: iface.DeprecatedSince);

Fields.Add (new PeerMembersField (opt, iface.RawJniName, $"{iface.Name}Invoker", false));

if (!ji) {
Expand Down

0 comments on commit 120d8a7

Please sign in to comment.