Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[src] Enable nullability in a few files. #18185

Merged
merged 1 commit into from
May 3, 2023
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
6 changes: 4 additions & 2 deletions src/Foundation/ConnectAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@
//
using System;

#nullable enable

namespace Foundation {

[AttributeUsage (AttributeTargets.Property)]
public sealed class ConnectAttribute : Attribute {
string name;
string? name;

public ConnectAttribute () { }
public ConnectAttribute (string name)
{
this.name = name;
}

public string Name {
public string? Name {
get { return this.name; }
set { this.name = value; }
}
Expand Down
10 changes: 6 additions & 4 deletions src/Foundation/ExportAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
10 changes: 6 additions & 4 deletions src/ObjCRuntime/LinkWithAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using System;
using System.IO;

#nullable enable

namespace ObjCRuntime {
[Flags]
public enum LinkTarget : int {
Expand Down Expand Up @@ -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;
}

Expand Down
16 changes: 11 additions & 5 deletions src/ObjCRuntime/Registrar.core.cs
Original file line number Diff line number Diff line change
@@ -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))
tj-devel709 marked this conversation as resolved.
Show resolved Hide resolved
#endif
return getterSelector;

var first = (int) getterSelector [0];
Expand All @@ -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];
Expand All @@ -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;
Expand Down
36 changes: 19 additions & 17 deletions src/ObjCRuntime/RuntimeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using ObjCRuntime;
#endif

#nullable enable

#if MMP || MMP_TEST || MTOUCH || BUNDLER
namespace Xamarin.Bundler {
#else
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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");
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 ();
}
Expand Down