Skip to content

Commit 7dfbab6

Browse files
authored
[generator] Add [SupportedOSPlatform] to bound constant fields (#1038)
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.
1 parent 1720628 commit 7dfbab6

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,24 @@ public void SupportedOSPlatform ()
736736

737737
StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
738738
}
739+
740+
[Test]
741+
public void SupportedOSPlatformConstFields ()
742+
{
743+
// Ensure we write [SupportedOSPlatform] for const fields
744+
var klass = new TestClass ("java.lang.Object", "com.mypackage.foo");
745+
var field = new TestField ("java.lang.String", "bar").SetConstant ("MY_VALUE");
746+
747+
field.ApiAvailableSince = 30;
748+
749+
klass.Fields.Add (field);
750+
751+
generator.Context.ContextTypes.Push (klass);
752+
generator.WriteType (klass, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
753+
generator.Context.ContextTypes.Pop ();
754+
755+
StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
756+
}
739757
}
740758

741759
[TestFixture]

tools/generator/SourceWriters/BoundField.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public BoundField (GenBase type, Field field, CodeGenerationOptions opt)
3131
if (field.IsEnumified)
3232
Attributes.Add (new GeneratedEnumAttr ());
3333

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

3637
if (field.Annotation.HasValue ())

0 commit comments

Comments
 (0)