Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add support for $(AndroidEnableObsolete…
Browse files Browse the repository at this point in the history
…OverrideInheritance). (#8393)

Context: dotnet/java-interop@8e63cc8
Context: dotnet/java-interop@d0231c5

dotnet/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 dotnet/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 dotnet/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 dotnet/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
  • Loading branch information
jpobst authored and jonathanpeppers committed Oct 10, 2023
1 parent 2958ae6 commit 7b16e97
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Documentation/guides/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<AndroidBoundInterfacesContainStaticAndDefaultInterfaceMethods Condition=" '$(AndroidBoundInterfacesContainStaticAndDefaultInterfaceMethods)' == '' ">true</AndroidBoundInterfacesContainStaticAndDefaultInterfaceMethods>
<AndroidBoundInterfacesContainTypes Condition=" '$(AndroidBoundInterfacesContainTypes)' == '' ">true</AndroidBoundInterfacesContainTypes>
<AndroidBoundInterfacesContainConstants Condition=" '$(AndroidBoundInterfacesContainConstants)' == '' ">true</AndroidBoundInterfacesContainConstants>
<AndroidEnableObsoleteOverrideInheritance Condition=" '$(AndroidEnableObsoleteOverrideInheritance)' == '' ">true</AndroidEnableObsoleteOverrideInheritance>
<AndroidEnableRestrictToAttributes Condition=" '$(AndroidEnableRestrictToAttributes)' == '' ">obsolete</AndroidEnableRestrictToAttributes>

<!-- Mono components -->
Expand Down
4 changes: 4 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit 7b16e97

Please sign in to comment.