Skip to content

Commit 412e974

Browse files
authored
Revert "[generator] Disable [SupportedOSPlatform] until .NET 5/6. (#781)" (#841)
Context: dotnet/android#5338 This reverts commit 00862ad. Now that we are properly building with `$(TargetFramework)`=net6.0 (4d0cba6), we can re-enable the `[SupportedOSPlatform]` support, as originally introduced in da12df4. Emit the [`System.Runtime.Versioning.SupportedOSPlatformAttribute`][0] custom attribute which is used to specify on which platforms and platform versions an API is available. This is used to build analyzers to give users warnings if they are trying to use an API when it will not be available on their target platform. [0]: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.versioning.supportedosplatformattribute?view=net-5.0
1 parent 23baf0b commit 412e974

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

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

+10-12
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,18 @@ class XAJavaInteropCodeGeneratorTests : CodeGeneratorTests
276276
{
277277
protected override CodeGenerationTarget Target => CodeGenerationTarget.XAJavaInterop1;
278278

279+
[Test]
280+
public void SupportedOSPlatform ()
281+
{
282+
var klass = SupportTypeBuilder.CreateClass ("java.code.MyClass", options);
283+
klass.ApiAvailableSince = 30;
279284

280-
// Disabled until we can properly build .NET 5/6 assemblies in our XA tree.
281-
//[Test]
282-
//public void SupportedOSPlatform ()
283-
//{
284-
// var klass = SupportTypeBuilder.CreateClass ("java.code.MyClass", options);
285-
// klass.ApiAvailableSince = 30;
286-
287-
// generator.Context.ContextTypes.Push (klass);
288-
// generator.WriteType (klass, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
289-
// generator.Context.ContextTypes.Pop ();
285+
generator.Context.ContextTypes.Push (klass);
286+
generator.WriteType (klass, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
287+
generator.Context.ContextTypes.Pop ();
290288

291-
// StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
292-
//}
289+
StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
290+
}
293291
}
294292

295293
[TestFixture]

tools/generator/Java.Interop.Tools.Generator.ObjectModel/NamespaceMapping.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,20 @@ public void Generate (CodeGenerationOptions opt, GenerationInfo gen_info)
3232
foreach (var jni in opt.GetJniMarshalDelegates ())
3333
sw.WriteLine ($"delegate {FromJniType (jni[jni.Length - 1])} {jni} (IntPtr jnienv, IntPtr klass{GetDelegateParameters (jni)});");
3434

35-
// Disabled until we can properly build .NET 5/6 assemblies in our XA tree.
3635
// [SupportedOSPlatform] only exists in .NET 5.0+, so we need to generate a
3736
// dummy one so earlier frameworks can compile.
38-
//if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1) {
39-
// sw.WriteLine ("#if !NET");
40-
// sw.WriteLine ("namespace System.Runtime.Versioning {");
41-
// sw.WriteLine (" [System.Diagnostics.Conditional(\"NEVER\")]");
42-
// sw.WriteLine (" [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");
43-
// sw.WriteLine (" internal sealed class SupportedOSPlatformAttribute : Attribute {");
44-
// sw.WriteLine (" public SupportedOSPlatformAttribute (string platformName) { }");
45-
// sw.WriteLine (" }");
46-
// sw.WriteLine ("}");
47-
// sw.WriteLine ("#endif");
48-
// sw.WriteLine ("");
49-
//}
37+
if (opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1) {
38+
sw.WriteLine ("#if !NET");
39+
sw.WriteLine ("namespace System.Runtime.Versioning {");
40+
sw.WriteLine (" [System.Diagnostics.Conditional(\"NEVER\")]");
41+
sw.WriteLine (" [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]");
42+
sw.WriteLine (" internal sealed class SupportedOSPlatformAttribute : Attribute {");
43+
sw.WriteLine (" public SupportedOSPlatformAttribute (string platformName) { }");
44+
sw.WriteLine (" }");
45+
sw.WriteLine ("}");
46+
sw.WriteLine ("#endif");
47+
sw.WriteLine ("");
48+
}
5049
}
5150
}
5251

tools/generator/SourceWriters/Attributes/SupportedOSPlatformAttr.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public class SupportedOSPlatformAttr : AttributeWriter
1515

1616
public override void WriteAttribute (CodeWriter writer)
1717
{
18-
// Disabled until we can properly build .NET 5/6 assemblies in our XA tree.
19-
//writer.WriteLine ($"[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android{Version}.0\")]");
18+
writer.WriteLine ($"[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android{Version}.0\")]");
2019
}
2120
}
2221
}

0 commit comments

Comments
 (0)