From 8936c7a9363783202ce4f63adbe5f247fb5fe5c5 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 3 May 2023 20:04:46 +0200 Subject: [PATCH] [src] Enable nullability in a few files. (#18185) --- src/Foundation/ConnectAttribute.cs | 6 +++-- src/Foundation/ExportAttribute.cs | 10 ++++---- src/ObjCRuntime/LinkWithAttribute.cs | 10 ++++---- src/ObjCRuntime/Registrar.core.cs | 16 +++++++++---- src/ObjCRuntime/RuntimeOptions.cs | 36 +++++++++++++++------------- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/src/Foundation/ConnectAttribute.cs b/src/Foundation/ConnectAttribute.cs index 2119d23cc51..a90a3519e56 100644 --- a/src/Foundation/ConnectAttribute.cs +++ b/src/Foundation/ConnectAttribute.cs @@ -22,11 +22,13 @@ // using System; +#nullable enable + namespace Foundation { [AttributeUsage (AttributeTargets.Property)] public sealed class ConnectAttribute : Attribute { - string name; + string? name; public ConnectAttribute () { } public ConnectAttribute (string name) @@ -34,7 +36,7 @@ public ConnectAttribute (string name) this.name = name; } - public string Name { + public string? Name { get { return this.name; } set { this.name = value; } } diff --git a/src/Foundation/ExportAttribute.cs b/src/Foundation/ExportAttribute.cs index 00e2d4657bd..f054ad1083f 100644 --- a/src/Foundation/ExportAttribute.cs +++ b/src/Foundation/ExportAttribute.cs @@ -34,28 +34,30 @@ using ObjCRuntime; using Registrar; +#nullable enable + namespace Foundation { [AttributeUsage (AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Property)] public class ExportAttribute : Attribute { - string selector; + string? selector; ArgumentSemantic semantic; protected ExportAttribute () { } - public ExportAttribute (string selector) + public ExportAttribute (string? selector) { this.selector = selector; this.semantic = ArgumentSemantic.None; } - public ExportAttribute (string selector, ArgumentSemantic semantic) + public ExportAttribute (string? selector, ArgumentSemantic semantic) { this.selector = selector; this.semantic = semantic; } - public string Selector { + public string? Selector { get { return this.selector; } set { this.selector = value; } } diff --git a/src/ObjCRuntime/LinkWithAttribute.cs b/src/ObjCRuntime/LinkWithAttribute.cs index f0679aad01b..4a3ca2b0aab 100644 --- a/src/ObjCRuntime/LinkWithAttribute.cs +++ b/src/ObjCRuntime/LinkWithAttribute.cs @@ -26,6 +26,8 @@ using System; using System.IO; +#nullable enable + namespace ObjCRuntime { [Flags] public enum LinkTarget : int { @@ -74,19 +76,19 @@ public bool ForceLoad { get; set; } - public string Frameworks { + public string? Frameworks { get; set; } - public string WeakFrameworks { + public string? WeakFrameworks { get; set; } - public string LibraryName { + public string? LibraryName { get; private set; } - public string LinkerFlags { + public string? LinkerFlags { get; set; } diff --git a/src/ObjCRuntime/Registrar.core.cs b/src/ObjCRuntime/Registrar.core.cs index bb9fdf655e8..ad5fe6817b3 100644 --- a/src/ObjCRuntime/Registrar.core.cs +++ b/src/ObjCRuntime/Registrar.core.cs @@ -1,10 +1,16 @@ using System.Text; +#nullable enable + namespace Registrar { abstract partial class Registrar { - internal static string CreateSetterSelector (string getterSelector) + internal static string? CreateSetterSelector (string? getterSelector) { +#if NET if (string.IsNullOrEmpty (getterSelector)) +#else + if (getterSelector is null || string.IsNullOrEmpty (getterSelector)) +#endif return getterSelector; var first = (int) getterSelector [0]; @@ -16,7 +22,7 @@ internal static string CreateSetterSelector (string getterSelector) public static string SanitizeObjectiveCName (string name) { - StringBuilder sb = null; + StringBuilder? sb = null; for (int i = 0; i < name.Length; i++) { var ch = name [i]; @@ -30,18 +36,18 @@ public static string SanitizeObjectiveCName (string name) case '>': case '$': case '-': - if (sb == null) + if (sb is null) sb = new StringBuilder (name, 0, i, name.Length); sb.Append ('_'); break; default: - if (sb != null) + if (sb is not null) sb.Append (ch); break; } } - if (sb != null) + if (sb is not null) return sb.ToString (); return name; diff --git a/src/ObjCRuntime/RuntimeOptions.cs b/src/ObjCRuntime/RuntimeOptions.cs index 211a89e3cf9..0b2ffc2d1a3 100644 --- a/src/ObjCRuntime/RuntimeOptions.cs +++ b/src/ObjCRuntime/RuntimeOptions.cs @@ -11,6 +11,8 @@ using ObjCRuntime; #endif +#nullable enable + #if MMP || MMP_TEST || MTOUCH || BUNDLER namespace Xamarin.Bundler { #else @@ -25,20 +27,20 @@ class RuntimeOptions { const string CFNetworkHandlerValue = "CFNetworkHandler"; const string NSUrlSessionHandlerValue = "NSUrlSessionHandler"; - string http_message_handler; + string? http_message_handler; #if MTOUCH || MMP || BUNDLER /* * This section is only used by the tools */ - internal static RuntimeOptions Create (Application app, string http_message_handler, string tls_provider) + internal static RuntimeOptions Create (Application app, string? http_message_handler, string? tls_provider) { var options = new RuntimeOptions (); options.http_message_handler = ParseHttpMessageHandler (app, http_message_handler); return options; } - static string ParseHttpMessageHandler (Application app, string value) + static string ParseHttpMessageHandler (Application app, string? value) { switch (value) { // default @@ -89,11 +91,11 @@ internal void Write (string app_dir) } // Called from CoreHttpMessageHandler - internal static TypeDefinition GetHttpMessageHandler (Application app, RuntimeOptions options, ModuleDefinition httpModule, ModuleDefinition platformModule = null) + internal static TypeDefinition GetHttpMessageHandler (Application app, RuntimeOptions options, ModuleDefinition httpModule, ModuleDefinition? platformModule = null) { - string handler; + string? handler; - if (options != null) { + if (options is not null) { handler = options.http_message_handler; } else if (app.Platform == Utils.ApplePlatform.WatchOS) { handler = NSUrlSessionHandlerValue; @@ -111,10 +113,10 @@ internal static TypeDefinition GetHttpMessageHandler (Application app, RuntimeOp type = httpModule.GetType ("System.Net.Http", "HttpClientHandler"); break; case CFNetworkHandlerValue: - type = platformModule.GetType ("System.Net.Http", "CFNetworkHandler"); + type = platformModule!.GetType ("System.Net.Http", "CFNetworkHandler"); break; case NSUrlSessionHandlerValue: - type = platformModule.GetType ("Foundation", "NSUrlSessionHandler"); + type = platformModule!.GetType ("Foundation", "NSUrlSessionHandler"); break; #else #if NET @@ -125,7 +127,7 @@ internal static TypeDefinition GetHttpMessageHandler (Application app, RuntimeOp case HttpClientHandlerValue: if (app.Platform == Utils.ApplePlatform.WatchOS) { ErrorHelper.Warning (2015, Errors.MT2015, handler); - type = platformModule.GetType ("System.Net.Http", "NSUrlSessionHandler"); + type = platformModule!.GetType ("System.Net.Http", "NSUrlSessionHandler"); } else { type = httpModule.GetType ("System.Net.Http", "HttpClientHandler"); } @@ -134,25 +136,25 @@ internal static TypeDefinition GetHttpMessageHandler (Application app, RuntimeOp case CFNetworkHandlerValue: if (app.Platform == Utils.ApplePlatform.WatchOS) { ErrorHelper.Warning (2015, Errors.MT2015, handler); - type = platformModule.GetType ("System.Net.Http", "NSUrlSessionHandler"); + type = platformModule!.GetType ("System.Net.Http", "NSUrlSessionHandler"); } else { - type = platformModule.GetType ("System.Net.Http", "CFNetworkHandler"); + type = platformModule!.GetType ("System.Net.Http", "CFNetworkHandler"); } break; case NSUrlSessionHandlerValue: - type = platformModule.GetType ("System.Net.Http", "NSUrlSessionHandler"); + type = platformModule!.GetType ("System.Net.Http", "NSUrlSessionHandler"); break; #endif default: throw new InvalidOperationException (string.Format ("Unknown HttpMessageHandler `{0}`.", handler)); } - if (type == null) + if (type is null) throw new InvalidOperationException (string.Format ("Cannot load HttpMessageHandler `{0}`.", handler)); return type; } #else - internal static RuntimeOptions Read () + internal static RuntimeOptions? Read () { // for iOS NSBundle.ResourcePath returns the path to the root of the app bundle // for macOS apps NSBundle.ResourcePath returns foo.app/Contents/Resources @@ -185,12 +187,12 @@ internal static HttpMessageHandler GetHttpMessageHandler () case CFNetworkHandlerValue: return new CFNetworkHandler (); default: - if (handler_name != null && handler_name != NSUrlSessionHandlerValue) + if (handler_name is not null && handler_name != NSUrlSessionHandlerValue) Runtime.NSLog ($"{handler_name} is not a valid HttpMessageHandler, defaulting to System.Net.Http.NSUrlSessionHandlerValue"); return new NSUrlSessionHandler (); } #elif __WATCHOS__ - if (handler_name != null && handler_name != NSUrlSessionHandlerValue) + if (handler_name is not null && handler_name != NSUrlSessionHandlerValue) Runtime.NSLog ($"{handler_name} is not a valid HttpMessageHandler, defaulting to NSUrlSessionHandler"); return new NSUrlSessionHandler (); #else @@ -200,7 +202,7 @@ internal static HttpMessageHandler GetHttpMessageHandler () case NSUrlSessionHandlerValue: return new NSUrlSessionHandler (); default: - if (handler_name != null && handler_name != HttpClientHandlerValue) + if (handler_name is not null && handler_name != HttpClientHandlerValue) Runtime.NSLog ($"{handler_name} is not a valid HttpMessageHandler, defaulting to System.Net.Http.HttpClientHandler"); return new HttpClientHandler (); }