Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/generator-Tests/Unit-Tests/CodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,36 @@ public void SupportedOSPlatformConstFields ()
StringAssert.Contains ("[global::System.Runtime.Versioning.SupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain SupportedOSPlatform!");
}

[Test]
public void UnsupportedOSPlatform ()
{
var klass = SupportTypeBuilder.CreateClass ("java.code.MyClass", options);
klass.ApiRemovedSince = 30;

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

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

[Test]
public void UnsupportedOSPlatformConstFields ()
{
var klass = new TestClass ("java.lang.Object", "com.mypackage.foo");
var field = new TestField ("java.lang.String", "bar").SetConstant ("MY_VALUE");

field.ApiRemovedSince = 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.UnsupportedOSPlatformAttribute (\"android30.0\")]", builder.ToString (), "Should contain UnsupportedOSPlatform!");
}

[Test]
public void StringPropertyOverride ([Values ("true", "false")] string final)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/generator-Tests/Unit-Tests/XmlApiImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public void CreateClass_CorrectApiSinceOverridePackage ()
Assert.AreEqual (9, klass.ApiAvailableSince);
}

[Test]
public void CreateClass_CorrectApiRemoved ()
{
var xml = XDocument.Parse ("<package name='com.example.test' jni-name='com/example/test'><class name='myclass' removed-since='7' /></package>");
var klass = XmlApiImporter.CreateClass (xml.Root, xml.Root.Element ("class"), opt);

Assert.AreEqual (7, klass.ApiRemovedSince);
}

[Test]
public void CreateCtor_EnsureValidName ()
{
Expand Down
1 change: 1 addition & 0 deletions tools/generator/ApiVersionsSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class ApiVersionsSupport
public interface IApiAvailability
{
int ApiAvailableSince { get; set; }
int ApiRemovedSince { get; set; }
}

static IEnumerable<GenBase> FlattenGens (IEnumerable<GenBase> gens)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public static Ctor CreateCtor (GenBase declaringType, XElement elem, CodeGenerat
var ctor = new Ctor (declaringType) {
AnnotatedVisibility = elem.XGetAttribute ("annotated-visibility"),
ApiAvailableSince = declaringType.ApiAvailableSince,
ApiRemovedSince = declaringType.ApiRemovedSince,
CustomAttributes = elem.XGetAttribute ("customAttributes"),
Deprecated = elem.Deprecated (),
DeprecatedSince = elem.XGetAttributeAsIntOrNull ("deprecated-since"),
Expand Down Expand Up @@ -211,6 +212,7 @@ public static Field CreateField (GenBase declaringType, XElement elem, CodeGener
var field = new Field {
AnnotatedVisibility = elem.XGetAttribute ("annotated-visibility"),
ApiAvailableSince = declaringType.ApiAvailableSince,
ApiRemovedSince = declaringType.ApiRemovedSince,
DeprecatedComment = elem.XGetAttribute ("deprecated"),
DeprecatedSince = elem.XGetAttributeAsIntOrNull ("deprecated-since"),
IsAcw = true,
Expand Down Expand Up @@ -369,6 +371,7 @@ public static Method CreateMethod (GenBase declaringType, XElement elem, CodeGen
var method = new Method (declaringType) {
AnnotatedVisibility = elem.XGetAttribute ("annotated-visibility"),
ApiAvailableSince = declaringType.ApiAvailableSince,
ApiRemovedSince = declaringType.ApiRemovedSince,
ArgsType = elem.Attribute ("argsType")?.Value,
CustomAttributes = elem.XGetAttribute ("customAttributes"),
Deprecated = elem.Deprecated (),
Expand Down Expand Up @@ -515,9 +518,12 @@ static XElement GetPreviousClass (XNode n, string nameValue)
// Elements need to be passed in the above order. (package, class, member)
static void FillApiSince (ApiVersionsSupport.IApiAvailability model, params XElement[] elems)
{
foreach (var elem in elems)
foreach (var elem in elems) {
if (int.TryParse (elem.XGetAttribute ("api-since"), out var result))
model.ApiAvailableSince = result;
if (int.TryParse (elem.XGetAttribute ("removed-since"), out var removed))
model.ApiRemovedSince = removed;
}
}

static bool IsObfuscatedName (int threshold, string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Field : ApiVersionsSupport.IApiAvailability, ISourceLineInfo
public string AnnotatedVisibility { get; set; }
public string Annotation { get; set; }
public int ApiAvailableSince { get; set; }
public int ApiRemovedSince { get; set; }
public string DeprecatedComment { get; set; }
public int? DeprecatedSince { get; set; }
public bool IsAcw { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ IEnumerable<GenBase> Ancestors ()

public int ApiAvailableSince { get; set; }

public int ApiRemovedSince { get; set; }

public virtual ClassGen BaseGen => null;

public GenBase BaseSymbol =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected MethodBase (GenBase declaringType)
public string AnnotatedVisibility { get; set; }
public string Annotation { get; internal set; }
public int ApiAvailableSince { get; set; }
public int ApiRemovedSince { get; set; }
public string AssemblyName { get; set; }
public GenBase DeclaringType { get; }
public string Deprecated { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.SourceWriter;

namespace generator.SourceWriters
{
public class UnsupportedOSPlatformAttr : AttributeWriter
{
public int Version { get; }

public UnsupportedOSPlatformAttr (int version) => Version = version;

public override void WriteAttribute (CodeWriter writer)
{
writer.WriteLine ($"[global::System.Runtime.Versioning.UnsupportedOSPlatformAttribute (\"android{Version}.0\")]");
}
}
}
18 changes: 15 additions & 3 deletions tools/generator/SourceWriters/Extensions/SourceWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace generator.SourceWriters
{
public static class SourceWriterExtensions
{
const int MINIMUM_API_LEVEL = 21;

public static void AddField (TypeWriter tw, GenBase type, Field field, CodeGenerationOptions opt)
{
if (field.NeedsProperty)
Expand Down Expand Up @@ -306,16 +308,26 @@ public static void AddParameterListCallArgs (List<string> body, ParameterList pa
}

public static void AddSupportedOSPlatform (List<AttributeWriter> attributes, ApiVersionsSupport.IApiAvailability member, CodeGenerationOptions opt)
=> AddSupportedOSPlatform (attributes, member.ApiAvailableSince, opt);
{
AddSupportedOSPlatform (attributes, member.ApiAvailableSince, opt);
AddUnsupportedOSPlatform (attributes, member.ApiRemovedSince, opt);
}

public static void AddSupportedOSPlatform (List<AttributeWriter> attributes, int since, CodeGenerationOptions opt)
{
// There's no sense in writing say 'android15' because we do not support older APIs,
// so those APIs will be available in all of our versions.
if (since > 21 && opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1)
if (since > MINIMUM_API_LEVEL && opt.CodeGenerationTarget == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1)
attributes.Add (new SupportedOSPlatformAttr (since));
}

public static void AddUnsupportedOSPlatform (List<AttributeWriter> attributes, int since, CodeGenerationOptions opt)
{
// Here it makes sense to still write 'android15' because it will be missing in later versions like `android35`.
if (since > 0 && opt.CodeGenerationTarget == CodeGenerationTarget.XAJavaInterop1)
attributes.Add (new UnsupportedOSPlatformAttr (since));
}

public static void AddObsolete (List<AttributeWriter> attributes, string message, CodeGenerationOptions opt, bool forceDeprecate = false, bool isError = false, int? deprecatedSince = null)
{
// Bail if we're not obsolete
Expand All @@ -336,7 +348,7 @@ static bool AddObsoletedOSPlatformAttribute (List<AttributeWriter> attributes, s
return false;

// If it was obsoleted in a version earlier than we support (like 15), use a regular [Obsolete] instead
if (!deprecatedSince.HasValue || deprecatedSince <= 21)
if (!deprecatedSince.HasValue || deprecatedSince <= MINIMUM_API_LEVEL)
return false;

// This is the default Android message, but it isn't useful so remove it
Expand Down