Skip to content

Commit

Permalink
[generator] Add [SupportedOSPlatform] to bound constant fields (#1038)
Browse files Browse the repository at this point in the history
Context: #1037

Currently we do not add `[SupportedOSPlatform]` attributes to bound
`const` fields, which can lead to customers using them on platforms
where they are not supported:

	namespace Android.Telecom {
	    public partial class TelecomManager : Java.Lang.Object {
	        [Register("ACTION_POST_CALL", ApiSince=30)]
	        public const string ActionPostCall = "android.telecom.action.POST_CALL";
	    }
	}

Update `generator` so that `[SupportedOSPlatform]` is emitted for
such `const` fields:

	namespace Android.Telecom {
	    public partial class TelecomManager : Java.Lang.Object {
	        [Register("ACTION_POST_CALL", ApiSince=30)]
	        [global::System.Runtime.Versioning.SupportedOSPlatformAttribute ("android30.0")]
	        public const string ActionPostCall = "android.telecom.action.POST_CALL";
	    }
	}

Note this does not address the `enum` part of #1037, which will
require a much more involved fix.
  • Loading branch information
jpobst authored Oct 20, 2022
1 parent 1720628 commit 7dfbab6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,24 @@ public void SupportedOSPlatform ()

StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
}

[Test]
public void SupportedOSPlatformConstFields ()
{
// Ensure we write [SupportedOSPlatform] for const fields
var klass = new TestClass ("java.lang.Object", "com.mypackage.foo");
var field = new TestField ("java.lang.String", "bar").SetConstant ("MY_VALUE");

field.ApiAvailableSince = 30;

klass.Fields.Add (field);

generator.Context.ContextTypes.Push (klass);
generator.WriteType (klass, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();

StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
}
}

[TestFixture]
Expand Down
1 change: 1 addition & 0 deletions tools/generator/SourceWriters/BoundField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public BoundField (GenBase type, Field field, CodeGenerationOptions opt)
if (field.IsEnumified)
Attributes.Add (new GeneratedEnumAttr ());

SourceWriterExtensions.AddSupportedOSPlatform (Attributes, field, opt);
SourceWriterExtensions.AddObsolete (Attributes, field.DeprecatedComment, opt, field.IsDeprecated, isError: field.IsDeprecatedError, deprecatedSince: field.DeprecatedSince);

if (field.Annotation.HasValue ())
Expand Down

0 comments on commit 7dfbab6

Please sign in to comment.