From 7b16e978da5421d009de072817b5ac5c8ccd2123 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Mon, 9 Oct 2023 14:09:42 -1000 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Add support for $(AndroidEnableObsoleteOverrideInheritance). (#8393) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: https://github.com/xamarin/java.interop/commit/8e63cc8da1882cadc7db329bfa6cafb6f914769f Context: https://github.com/xamarin/java.interop/commit/d0231c5c7eab4b4e51dda43dd523d6649b8f1593 xamarin/java.interop@d0231c5c updated generator output so that `[Obsolete]` would automagically "flow" to derived types and method overrides, so that [warning CS0672][0] would not be emitted. Before xamarin/java.interop@d0231c5c, we would emit: public class Context { [Obsolete] public virtual void SetWallpaper () { … } } public class ContextWrapper : Context { [ObsoletedOSPlatform ("android23.0")] public override void SetWallpaper () { … } } `ContextWrapper.SetWallpaper()` would elicit a CS0672 warning: warning CS0672: Member 'ContextWrapper.SetWallpaper()' overrides obsolete member 'Context.SetWallpaper()'. Add the Obsolete attribute to 'ContextWrapper.SetWallpaper()' With xamarin/java.interop@d0231c5c, we now emit the following, which no longer emits CS0672: public class Context { [Obsolete] public virtual void SetWallpaper () { … } } public class ContextWrapper : Context { [Obsolete] public override void SetWallpaper () { … } } While we feel that this is a nice improvement, this broke some customers who didn't want their bindings to automatically gain `[Obsolete]` "just because" they were overriding `[Obsolete]` types or members. In xamarin/java.interop@8e63cc8d, we added the ability to globally *opt-out* of this new behavior by using `generator --lang-features=do-not-fix-obsolete-overrides`. Add a new `$(AndroidEnableObsoleteOverrideInheritance)` MSBuild property. When False, this adds `generator --lang-features=do-not-fix-obsolete-overrides`, preventing the automatic CS0672 fix behavior. By default `$(AndroidEnableObsoleteOverrideInheritance)` is True. [0]: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0672 --- Documentation/guides/building-apps/build-properties.md | 9 +++++++++ .../Android/Xamarin.Android.Bindings.Core.targets | 1 + .../Microsoft.Android.Sdk.DefaultProperties.targets | 1 + src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/Documentation/guides/building-apps/build-properties.md b/Documentation/guides/building-apps/build-properties.md index 55ce6c6f304..be7378f3c1a 100644 --- a/Documentation/guides/building-apps/build-properties.md +++ b/Documentation/guides/building-apps/build-properties.md @@ -469,6 +469,15 @@ Support for this property was added in Xamarin.Android 5.1. This property is `False` by default. +## AndroidEnableObsoleteOverrideInheritance + +A boolean property that determines if bound methods automatically inherit `[Obsolete]` +attributes from methods they override. + +Support for this property was added in .NET 8. + +This property is `True` by default. + ## AndroidEnablePreloadAssemblies A boolean property that controls diff --git a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Core.targets b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Core.targets index fd0674e9946..0344a49f591 100644 --- a/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Core.targets +++ b/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Bindings.Core.targets @@ -99,6 +99,7 @@ It is shared between "legacy" binding projects and .NET 5 projects. EnableBindingNestedInterfaceTypes="$(AndroidBoundInterfacesContainTypes)" EnableBindingInterfaceConstants="$(AndroidBoundInterfacesContainConstants)" EnableRestrictToAttributes="$(AndroidEnableRestrictToAttributes)" + EnableObsoleteOverrideInheritance="$(AndroidEnableObsoleteOverrideInheritance)" Nullable="$(Nullable)" UseJavaLegacyResolver="$(_AndroidUseJavaLegacyResolver)" NamespaceTransforms="@(AndroidNamespaceReplacement)" diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets index 2ec7baa7784..46a750a585b 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.DefaultProperties.targets @@ -39,6 +39,7 @@ true true true + true obsolete diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs index 33a9cad54cc..9744f864c4c 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs @@ -52,6 +52,7 @@ public class BindingsGenerator : AndroidDotnetToolTask public bool EnableBindingNestedInterfaceTypes { get; set; } public bool EnableBindingInterfaceConstants { get; set; } public string EnableRestrictToAttributes { get; set; } + public bool EnableObsoleteOverrideInheritance { get; set; } public string Nullable { get; set; } public ITaskItem[] TransformFiles { get; set; } @@ -217,6 +218,9 @@ protected override string GenerateCommandLineCommands () if (EnableBindingStaticAndDefaultInterfaceMethods) features.Add ("default-interface-methods"); + if (!EnableObsoleteOverrideInheritance) + features.Add ("do-not-fix-obsolete-overrides"); + if (string.Equals (EnableRestrictToAttributes, "obsolete", StringComparison.OrdinalIgnoreCase)) features.Add ("restrict-to-attributes");