From 26d9d9172a791b34335929499d3a85c986ba8360 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 19 Jan 2022 20:52:18 +0100 Subject: [PATCH] [ScriptingBridge] Improve API and add nullability. (#13787) * Improve the API for both legacy Xamarin and .NET. * Obsolete the old API in legacy Xamarin and point to the new API. * Remove the old API in .NET. * Add nullability. --- src/ScriptingBridge/SBApplication.cs | 36 +++++++++++-------- src/scriptingbridge.cs | 2 +- .../ScriptingBridge/SBApplicationTest.cs | 20 +++++------ .../macOS-ScriptingBridge.ignore | 1 - 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/ScriptingBridge/SBApplication.cs b/src/ScriptingBridge/SBApplication.cs index 8a4801b8f9..adedb74199 100644 --- a/src/ScriptingBridge/SBApplication.cs +++ b/src/ScriptingBridge/SBApplication.cs @@ -1,3 +1,5 @@ +#nullable enable + using System; using System.Reflection; using AppKit; @@ -7,30 +9,36 @@ using ObjCRuntime; namespace ScriptingBridge { public partial class SBApplication { -#if XAMCORE_4_0 - public static SBApplication GetApplication (string ident) => Runtime.GetNSObject (_FromBundleIdentifier (ident)); + public static SBApplication? GetApplication (string ident) => Runtime.GetNSObject (_FromBundleIdentifier (ident)); - public static T GetApplication (string ident) where T : SBApplication => Runtime.GetINativeObject (_FromBundleIdentifier (ident), forced_type: true, owns: false); + public static T? GetApplication (string ident) where T : SBApplication => Runtime.GetINativeObject (_FromBundleIdentifier (ident), forced_type: true, owns: false); - public static SBApplication GetApplication (NSUrl url) => Runtime.GetNSObject (_FromURL (url) ; + public static SBApplication? GetApplication (NSUrl url) => Runtime.GetNSObject (_FromURL (url)); - public static T GetApplication (NSUrl url) where T : SBApplication => Runtime.GetINativeObject (_FromURL (url), forced_type: true, owns: false); + public static T? GetApplication (NSUrl url) where T : SBApplication => Runtime.GetINativeObject (_FromURL (url), forced_type: true, owns: false); - public static SBApplication GetApplication (int pid) => Runtime.GetNSObject (_FromProcessIdentifier (pid)); + public static SBApplication? GetApplication (int pid) => Runtime.GetNSObject (_FromProcessIdentifier (pid)); - public static T GetApplication (int pid) where T : SBApplication => Runtime.GetINativeObject (_FromProcessIdentifier (pid), forced_type: true, owns: false); -#else - public static SBApplication FromBundleIdentifier (string ident) => Runtime.GetNSObject (_FromBundleIdentifier (ident)); + public static T? GetApplication (int pid) where T : SBApplication => Runtime.GetINativeObject (_FromProcessIdentifier (pid), forced_type: true, owns: false); - public static T FromBundleIdentifier (string ident) where T : SBApplication => Runtime.GetINativeObject (_FromBundleIdentifier (ident), forced_type: true, owns: false); +#if !NET + [Obsolete ("Use 'GetApplication' instead.")] + public static SBApplication? FromBundleIdentifier (string ident) => Runtime.GetNSObject (_FromBundleIdentifier (ident)); - public static SBApplication FromURL (NSUrl url) => Runtime.GetNSObject (_FromURL (url)); + [Obsolete ("Use 'GetApplication' instead.")] + public static T? FromBundleIdentifier (string ident) where T : SBApplication => Runtime.GetINativeObject (_FromBundleIdentifier (ident), forced_type: true, owns: false); - public static T FromURL (NSUrl url) where T : SBApplication => Runtime.GetINativeObject (_FromURL (url), forced_type: true, owns: false); + [Obsolete ("Use 'GetApplication' instead.")] + public static SBApplication? FromURL (NSUrl url) => Runtime.GetNSObject (_FromURL (url)); - public static SBApplication FromProcessIdentifier (int pid) => Runtime.GetNSObject (_FromProcessIdentifier (pid)); + [Obsolete ("Use 'GetApplication' instead.")] + public static T? FromURL (NSUrl url) where T : SBApplication => Runtime.GetINativeObject (_FromURL (url), forced_type: true, owns: false); - public static T FromProcessIdentifier (int pid) where T : SBApplication => Runtime.GetINativeObject (_FromProcessIdentifier (pid), forced_type: true, owns: false); + [Obsolete ("Use 'GetApplication' instead.")] + public static SBApplication? FromProcessIdentifier (int pid) => Runtime.GetNSObject (_FromProcessIdentifier (pid)); + + [Obsolete ("Use 'GetApplication' instead.")] + public static T? FromProcessIdentifier (int pid) where T : SBApplication => Runtime.GetINativeObject (_FromProcessIdentifier (pid), forced_type: true, owns: false); #endif } } diff --git a/src/scriptingbridge.cs b/src/scriptingbridge.cs index 223db222cb..15b80c5ee1 100644 --- a/src/scriptingbridge.cs +++ b/src/scriptingbridge.cs @@ -161,7 +161,7 @@ namespace ScriptingBridge { [Model] [Protocol] interface SBApplicationDelegate { -#if !XAMCORE_4_0 +#if !NET [Abstract] [Export ("eventDidFail:withError:"), DelegateName ("SBApplicationError"), DefaultValue (null)] //NSObject EventDidFailwithError (const AppleEvent event, NSError error); diff --git a/tests/monotouch-test/ScriptingBridge/SBApplicationTest.cs b/tests/monotouch-test/ScriptingBridge/SBApplicationTest.cs index d2e784528e..bc7de7ac14 100644 --- a/tests/monotouch-test/ScriptingBridge/SBApplicationTest.cs +++ b/tests/monotouch-test/ScriptingBridge/SBApplicationTest.cs @@ -38,16 +38,16 @@ namespace Xamarin.Mac.Tests { { const string knownBundle = "com.apple.finder"; const string unknownBundle = "com.unknown.bundle"; -#if !XAMCORE_4_0 +#if !NET using (var app1 = SBApplication.FromBundleIdentifier (knownBundle)) using (var app2 = SBApplication.FromBundleIdentifier (knownBundle)) using (var app3 = SBApplication.FromBundleIdentifier (unknownBundle)) using (var app4 = SBApplication.FromBundleIdentifier (unknownBundle)) #else - var (app1 = SBApplication.GetApplication (knownBundle)) - var (app2 = SBApplication.GetApplication (knownBundle)) - var (app3 = SBApplication.GetApplication (unknownBundle)) - var (app4 = SBApplication.GetApplication (unknownBundle)) + using (var app1 = SBApplication.GetApplication (knownBundle)) + using (var app2 = SBApplication.GetApplication (knownBundle)) + using (var app3 = SBApplication.GetApplication (unknownBundle)) + using (var app4 = SBApplication.GetApplication (unknownBundle)) #endif { Assert.IsNotNull (app1, "SBApplication from known bundle is null"); @@ -61,12 +61,12 @@ namespace Xamarin.Mac.Tests { public void TestGetApplicationFromUrl () { using (NSUrl knownUrl = new NSUrl ("http://www.xamarin.com")) -#if !XAMCORE_4_0 +#if !NET using (var app1 = SBApplication.FromURL (knownUrl)) using (var app2 = SBApplication.FromURL (knownUrl)) #else using (var app1 = SBApplication.GetApplication (knownUrl)) - using (var app2 = SBApplication.GetApplication (knownUrl)) + using (var app2 = SBApplication.GetApplication (knownUrl)) #endif { Assert.IsNotNull (app1, "SBApplication from known URL is null"); @@ -79,16 +79,16 @@ namespace Xamarin.Mac.Tests { { int knownPid = System.Diagnostics.Process.GetCurrentProcess ().Id; int unknownPid = -1; // valid pid is > 0 -#if !XAMCORE_4_0 +#if !NET using (var app1 = SBApplication.FromProcessIdentifier (knownPid)) using (var app2 = SBApplication.FromProcessIdentifier (knownPid)) using (var app3 = SBApplication.FromProcessIdentifier (unknownPid)) using (var app4 = SBApplication.FromProcessIdentifier (unknownPid)) #else using (var app1 = SBApplication.GetApplication (knownPid)) - using (var app2 = SBApplication.GetApplication (knownPid)) + using (var app2 = SBApplication.GetApplication (knownPid)) using (var app3 = SBApplication.GetApplication (unknownPid)) - using (var app4 = SBApplication.GetApplication (unknownPid) + using (var app4 = SBApplication.GetApplication (unknownPid)) #endif { Assert.IsNotNull (app1, "SBApplication from known pid is null"); diff --git a/tests/xtro-sharpie/api-annotations-dotnet/macOS-ScriptingBridge.ignore b/tests/xtro-sharpie/api-annotations-dotnet/macOS-ScriptingBridge.ignore index d52ae0d7fc..a52d41b915 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/macOS-ScriptingBridge.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/macOS-ScriptingBridge.ignore @@ -1,7 +1,6 @@ # Initial result from new rule missing-null-allowed !missing-null-allowed! 'Foundation.NSError ScriptingBridge.SBObject::get_LastError()' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSObject ScriptingBridge.SBApplicationDelegate::EventDidFailwithError(System.IntPtr,Foundation.NSError)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSObject ScriptingBridge.SBObject::get_Get()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSObject[] ScriptingBridge.SBElementArray::Get()' is missing an [NullAllowed] on return type !missing-null-allowed! 'ObjCRuntime.Class ScriptingBridge.SBApplication::ClassForScripting(System.String)' is missing an [NullAllowed] on return type