Skip to content

Commit 209d846

Browse files
[xabt] Fix NRT annotations in Xamarin.Android.Build.Tasks, part 1 (#10327)
Context: #10326 This PR systematically updates Nullable Reference Types (NRT) annotations across the `src/Xamarin.Android.Build.Tasks/Tasks/` directory to follow the repository's coding guidelines as defined in `.github/copilot-instructions.md`. ## Changes Made This pull request primarily focuses on code modernization and cleanup for the `Xamarin.Android.Build.Tasks` project. The changes include replacing `Array.Empty<T>()` with `[]` for initializing arrays and switching from `string.Empty` to `""` for string initialization. Additionally, nullable annotations have been enabled in a utility file to improve code safety. These updates aim to simplify the codebase and align it with modern C# conventions. ### Code Modernization * Replaced `Array.Empty<T>()` with `[]` for array initialization across multiple files. This change simplifies array initialization syntax. Examples include `RuntimePackLibraryDirectories`, `ReferenceSatellitePaths`, and `InputAssemblies`. * Updated string initialization from `string.Empty` to `""` for consistency and brevity. Examples include `CodeGenerationTarget`, `OutputDirectory`, and `NuGetPackageId`. ### Nullable Annotations * Enabled nullable annotations in `DummyCustomAttributeProvider.cs` to improve code safety and nullability handling. ### Logical Enhancements * Added null checks in `GenerateResourceDesignerAssembly.cs` to skip processing resources with null identifiers or type names, ensuring robustness in resource handling. Co-authored-by: Jonathan Peppers <[email protected]>
1 parent 8c85cfb commit 209d846

31 files changed

+98
-84
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/Aapt2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected bool LogAapt2EventsFromOutput (string singleLine, MessageImportance me
130130
return true;
131131

132132
var match = AndroidRunToolTask.AndroidErrorRegex.Match (singleLine.Trim ());
133-
string file = string.Empty;
133+
string file = "";
134134

135135
if (match.Success) {
136136
file = match.Groups ["file"].Value;

src/Xamarin.Android.Build.Tasks/Tasks/Aapt2Link.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ string [] GenerateCommandLineCommands (string ManifestFile, string? currentAbi,
311311
}
312312

313313
if (hasAssetsErrors) {
314-
return Array.Empty<string> ();
314+
return [];
315315
}
316316

317317
if (!ProguardRuleOutput.IsNullOrEmpty ()) {

src/Xamarin.Android.Build.Tasks/Tasks/AssemblyModifierPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public override bool RunTask ()
8080
ReadSymbols = ReadSymbols,
8181
};
8282

83-
Dictionary<AndroidTargetArch, Dictionary<string, ITaskItem>> perArchAssemblies = MonoAndroidHelper.GetPerArchAssemblies (ResolvedAssemblies, Array.Empty<string> (), validate: false);
83+
Dictionary<AndroidTargetArch, Dictionary<string, ITaskItem>> perArchAssemblies = MonoAndroidHelper.GetPerArchAssemblies (ResolvedAssemblies, [], validate: false);
8484

8585
AssemblyPipeline? pipeline = null;
8686
var currentArch = AndroidTargetArch.None;

src/Xamarin.Android.Build.Tasks/Tasks/BuildAppBundle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ internal override CommandLineBuilder GetCommandLineBuilder ()
133133
cmd.AppendSwitchIfNotNull ("--modules ", string.Join (",", modules));
134134
cmd.AppendSwitchIfNotNull ("--output ", Output);
135135
cmd.AppendSwitchIfNotNull ("--config ", temp);
136-
foreach (var file in MetaDataFiles ?? Array.Empty<ITaskItem> ()) {
136+
foreach (var file in MetaDataFiles ?? []) {
137137
cmd.AppendSwitch ($"--metadata-file={file.ItemSpec}");
138138
}
139139
return cmd;

src/Xamarin.Android.Build.Tasks/Tasks/CheckDuplicateJavaLibraries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public override bool RunTask ()
2525
jarFiles = jarFiles.Concat (JavaLibraries);
2626
else if (JavaLibraries != null)
2727
jarFiles = JavaLibraries;
28-
var jarFilePaths = (LibraryProjectJars ?? Array.Empty<ITaskItem> ()).Concat (jarFiles ?? Array.Empty<ITaskItem> ()).Select (j => j.ItemSpec);
28+
var jarFilePaths = (LibraryProjectJars ?? []).Concat (jarFiles ?? []).Select (j => j.ItemSpec);
2929

3030
// Remove duplicate identical jars by name, size and content, and reject any jars that conflicts by name (i.e. different content).
3131
var jars = MonoAndroidHelper.DistinctFilesByContent (jarFilePaths).ToArray ();

src/Xamarin.Android.Build.Tasks/Tasks/CollectNativeFilesForArchive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class CollectNativeFilesForArchive : AndroidTask
5454
public string AndroidBinUtilsDirectory { get; set; } = "";
5555

5656
[Required]
57-
public ITaskItem[] RuntimePackLibraryDirectories { get; set; } = Array.Empty<ITaskItem> ();
57+
public ITaskItem[] RuntimePackLibraryDirectories { get; set; } = [];
5858

5959
[Required]
6060
public string IntermediateOutputPath { get; set; } = "";

src/Xamarin.Android.Build.Tasks/Tasks/CollectRuntimeConfigFilesForArchive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class CollectRuntimeConfigFilesForArchive : AndroidTask
1919
public string AndroidBinUtilsDirectory { get; set; } = "";
2020

2121
[Required]
22-
public ITaskItem[] RuntimePackLibraryDirectories { get; set; } = Array.Empty<ITaskItem> ();
22+
public ITaskItem[] RuntimePackLibraryDirectories { get; set; } = [];
2323

2424
[Required]
2525
public string IntermediateOutputPath { get; set; } = "";

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaCallableWrappers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public class GenerateJavaCallableWrappers : AndroidTask
2222
public override string TaskPrefix => "JCW";
2323

2424
[Required]
25-
public string CodeGenerationTarget { get; set; } = string.Empty;
25+
public string CodeGenerationTarget { get; set; } = "";
2626

2727
[Required]
28-
public string OutputDirectory { get; set; } = string.Empty;
28+
public string OutputDirectory { get; set; } = "";
2929

3030
[Required]
31-
public string PackageNamingPolicy { get; set; } = string.Empty;
31+
public string PackageNamingPolicy { get; set; } = "";
3232

3333
[Required]
3434
public ITaskItem [] ResolvedAssemblies { get; set; } = [];

src/Xamarin.Android.Build.Tasks/Tasks/GenerateLayoutBindings.CSharpBindingGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sealed class CSharpBindingGenerator : BindingGenerator
1414
{
1515
const string BindingPartialClassBackingFieldName = "__layout_binding";
1616

17-
string codebehindDir = string.Empty;
17+
string codebehindDir = "";
1818
protected override string LineCommentString => "//";
1919
protected override string DocCommentString => "///";
2020
public override string LanguageName => "C#";

src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceCaseMap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public override bool RunTask ()
4141
ResourceDirectory = Path.GetFullPath (ResourceDirectory);
4242

4343
// Create our capitalization maps so we can support mixed case resources
44-
foreach (var item in Resources ?? Array.Empty<ITaskItem>()) {
44+
foreach (var item in Resources ?? []) {
4545
var path = Path.GetFullPath (item.ItemSpec);
4646
if (!path.StartsWith (ResourceDirectory, StringComparison.OrdinalIgnoreCase)) {
4747
Log.LogDebugMessage ($"Skipping {item}. Path is not include the '{ResourceDirectory}'");
@@ -55,7 +55,7 @@ public override bool RunTask ()
5555

5656
AddRename (name.Replace ('/', Path.DirectorySeparatorChar), logical_name.Replace ('/', Path.DirectorySeparatorChar));
5757
}
58-
foreach (var additionalDir in AdditionalResourceDirectories ?? Array.Empty<ITaskItem>()) {
58+
foreach (var additionalDir in AdditionalResourceDirectories ?? []) {
5959
var dir = Path.Combine (ProjectDir, Path.GetDirectoryName (additionalDir.ItemSpec));
6060
var file = Path.Combine (dir, "__res_name_case_map.txt");
6161
if (!File.Exists (file)) {
@@ -72,7 +72,7 @@ public override bool RunTask ()
7272
}
7373
}
7474
var resmap = ".net/__res_name_case_map.txt";
75-
foreach (var aar in AarLibraries ?? Array.Empty<string>()) {
75+
foreach (var aar in AarLibraries ?? []) {
7676
Log.LogDebugMessage ($"Processing Aar file {aar}");
7777
if (!File.Exists (aar)) {
7878
Log.LogDebugMessage ($"Skipping non-existent aar: {aar}");

0 commit comments

Comments
 (0)