Skip to content

Commit e206cee

Browse files
Fix nullable reference type compilation errors
Co-authored-by: jonathanpeppers <[email protected]>
1 parent 5b6e0fa commit e206cee

File tree

5 files changed

+67
-42
lines changed

5 files changed

+67
-42
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ public override bool RunTask ()
8181
resource_fixup = MonoAndroidHelper.LoadMapFile (BuildEngine4, Path.GetFullPath (CaseMapFile), StringComparer.OrdinalIgnoreCase);
8282

8383
// Parse out the resources from the R.java file
84-
CodeTypeDeclaration resources;
84+
CodeTypeDeclaration? resources = null;
8585
if (UseManagedResourceGenerator) {
8686
var parser = new ManagedResourceParser () { Log = Log, JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile };
8787
resources = parser.Parse (ResourceDirectory, RTxtFile ?? string.Empty, AdditionalResourceDirectories?.Select (x => x.ItemSpec), IsApplication, resource_fixup);
8888
} else {
8989
var parser = new JavaResourceParser () { Log = Log };
90-
resources = parser.Parse (JavaResgenInputFile, IsApplication, resource_fixup);
90+
if (JavaResgenInputFile != null) {
91+
resources = parser.Parse (JavaResgenInputFile, IsApplication, resource_fixup);
92+
}
9193
}
9294

9395
var extension = Path.GetExtension (NetResgenOutputFile);
@@ -139,18 +141,24 @@ public override bool RunTask ()
139141
}
140142
Log.LogDebugMessage ("Scan assembly {0} for resource generator", fileName);
141143
}
142-
new ResourceDesignerImportGenerator (namespaceName, resources, Log)
143-
.CreateImportMethods (assemblies);
144+
if (resources != null) {
145+
new ResourceDesignerImportGenerator (namespaceName, resources, Log)
146+
.CreateImportMethods (assemblies);
147+
}
144148
}
145149

146-
AdjustConstructor (resources);
147-
foreach (var member in resources.Members)
148-
if (member is CodeTypeDeclaration)
149-
AdjustConstructor ((CodeTypeDeclaration) member);
150+
if (resources != null) {
151+
AdjustConstructor (resources);
152+
foreach (var member in resources.Members)
153+
if (member is CodeTypeDeclaration)
154+
AdjustConstructor ((CodeTypeDeclaration) member);
155+
}
150156

151157
// Write out our Resources.Designer.cs file
152158

153-
WriteFile (NetResgenOutputFile, resources, language, isCSharp, aliases, namespaceName);
159+
if (resources != null) {
160+
WriteFile (NetResgenOutputFile, resources, language, isCSharp, aliases, namespaceName);
161+
}
154162

155163
// During a regular build, write the designtime/Resource.designer.cs file as well
156164

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override bool RunTask ()
3434

3535
var javaPlatformDirectory = JavaPlatformJarPath.IsNullOrEmpty () ? "" : Path.GetDirectoryName (JavaPlatformJarPath);
3636
var parser = new FileResourceParser () { Log = Log, JavaPlatformDirectory = javaPlatformDirectory, ResourceFlagFile = ResourceFlagFile};
37-
var resources = parser.Parse (ResourceDirectory, AdditionalResourceDirectories, AarLibraries, resource_fixup);
37+
var resources = parser.Parse (ResourceDirectory, AdditionalResourceDirectories ?? [], AarLibraries ?? [], resource_fixup);
3838

3939
// only update if it changed.
4040
writer.Write (RTxtFile, resources);

src/Xamarin.Android.Build.Tasks/Utilities/BundleConfigSplitConfigsChecker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Json;
77

88
using Microsoft.Build.Utilities;
9+
using Xamarin.Android.Tasks;
910

1011
/// <para>
1112
/// When bundle configuration uses standard settings for split configs, the per-ABI library

src/Xamarin.Android.Build.Tasks/Utilities/FileResourceParser.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public IList<R> Parse (string resourceDirectory, IEnumerable<string> additionalR
162162
itemid++;
163163
r.UpdateId (id);
164164
} else {
165-
if (foofoo.ContainsKey (r.Identifier)) {
165+
if (r.Identifier != null && foofoo.ContainsKey (r.Identifier)) {
166166
var items = foofoo[r.Identifier];
167167
if (r.Ids != null) {
168168
// do something special cos its an array we need to replace *some* its.
@@ -274,7 +274,7 @@ void CreateResourceField (string root, string id, Dictionary<string, ICollection
274274
void ProcessStyleable (XmlReader reader, Dictionary<string, ICollection<R>> resources)
275275
{
276276
Log.LogDebugMessage ($"{nameof(ProcessStyleable)}");
277-
string topName = null;
277+
string? topName = null;
278278
List<R> fields = new List<R> ();
279279
List<string> attribs = new List<string> ();
280280
if (reader.HasAttributes) {
@@ -286,7 +286,7 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, ICollection<R>> reso
286286
while (reader.Read ()) {
287287
if (reader.NodeType == XmlNodeType.Whitespace || reader.NodeType == XmlNodeType.Comment)
288288
continue;
289-
string name = null;
289+
string? name = null;
290290
if (topName.IsNullOrEmpty ()) {
291291
if (reader.HasAttributes) {
292292
while (reader.MoveToNextAttribute ()) {
@@ -305,7 +305,9 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, ICollection<R>> reso
305305
}
306306
reader.MoveToElement ();
307307
if (reader.LocalName == "attr") {
308-
attribs.Add (name);
308+
if (name != null) {
309+
attribs.Add (name);
310+
}
309311
}
310312
}
311313
var field = new R () {
@@ -314,7 +316,9 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, ICollection<R>> reso
314316
Type = RType.Array,
315317
};
316318
if (!arrayMapping.ContainsKey (field)) {
317-
foofoo.Add (field.Identifier, new List<string> ());
319+
if (field.Identifier != null) {
320+
foofoo.Add (field.Identifier, new List<string> ());
321+
}
318322
attribs.Sort (StringComparer.OrdinalIgnoreCase);
319323
for (int i = 0; i < attribs.Count; i++) {
320324
string name = attribs [i];
@@ -348,7 +352,9 @@ void ProcessStyleable (XmlReader reader, Dictionary<string, ICollection<R>> reso
348352
resources [field.ResourceTypeName].Add (field);
349353
int id = 0;
350354
foreach (string r in attribs) {
351-
foofoo[field.Identifier].Add (r.Replace (":", "_"));
355+
if (field.Identifier != null) {
356+
foofoo[field.Identifier].Add (r.Replace (":", "_"));
357+
}
352358
resources [field.ResourceTypeName].Add (new R () {
353359
ResourceTypeName = field.ResourceTypeName,
354360
Identifier = $"{field.Identifier}_{r.Replace (":", "_")}",
@@ -383,15 +389,15 @@ void ProcessXmlFile (XmlReader reader, Dictionary<string, ICollection<R>> resour
383389
try {
384390
ProcessStyleable (reader.ReadSubtree (), resources);
385391
} catch (Exception ex) {
386-
Log.LogErrorFromException (ex);
392+
Log?.LogErrorFromException (ex);
387393
}
388394
continue;
389395
}
390396
if (reader.HasAttributes) {
391-
string name = null;
392-
string type = null;
393-
string id = null;
394-
string custom_id = null;
397+
string? name = null;
398+
string? type = null;
399+
string? id = null;
400+
string? custom_id = null;
395401
while (reader.MoveToNextAttribute ()) {
396402
if (reader.LocalName == "name")
397403
name = reader.Value;

src/Xamarin.Android.Build.Tasks/Utilities/JavaResourceParser.cs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class JavaResourceParser : ResourceParser
3636
return resources;
3737
}
3838

39-
static KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>> Parse (string regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration> f)
39+
static KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration?>> Parse (string regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration?> f)
4040
{
41-
return new KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>> (new Regex (regex), f);
41+
return new KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration?>> (new Regex (regex), f);
4242
}
4343

4444
// public finall class R {
@@ -49,11 +49,11 @@ static KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<str
4949
// }
5050
// }
5151
// }
52-
List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>>> Parser;
52+
List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration?>>> Parser;
5353

5454
public JavaResourceParser ()
5555
{
56-
Parser = new List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>>> () {
56+
Parser = new List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration?>>> () {
5757
Parse ("^public final class R {",
5858
(m, app, _, map) => {
5959
var decl = new CodeTypeDeclaration ("Resource") {
@@ -80,43 +80,53 @@ public JavaResourceParser ()
8080
t.Members.Add (new CodeConstructor () {
8181
Attributes = MemberAttributes.Private,
8282
});
83-
g.Members.Add (t);
83+
g?.Members.Add (t);
8484
return g;
8585
}),
8686
Parse (@"^ public static final int ([^ =]+)\s*=\s*([^;]+);$",
8787
(m, app, g, map) => {
88-
var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name;
89-
var f = new CodeMemberField (typeof (int), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log)) {
88+
if (g != null) {
89+
var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name;
90+
if (Log != null) {
91+
var f = new CodeMemberField (typeof (int), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log)) {
9092
Attributes = app ? MemberAttributes.Const | MemberAttributes.Public : MemberAttributes.Static | MemberAttributes.Public,
9193
InitExpression = new CodePrimitiveExpression (ToInt32 (m.Groups [2].Value, m.Groups [2].Value.IndexOf ("0x", StringComparison.Ordinal) == 0 ? 16 : 10)),
9294
Comments = {
9395
new CodeCommentStatement ("aapt resource value: " + m.Groups [2].Value),
9496
},
95-
};
96-
((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Members.Add (f);
97+
};
98+
((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Members.Add (f);
99+
}
100+
}
97101
return g;
98102
}),
99103
Parse (@"^ public static final int\[\] ([^ =]+) = {",
100104
(m, app, g, map) => {
101-
var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name;
102-
var f = new CodeMemberField (typeof (int[]), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log)) {
105+
if (g != null) {
106+
var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name;
107+
if (Log != null) {
108+
var f = new CodeMemberField (typeof (int[]), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log)) {
103109
// pity I can't make the member readonly...
104110
Attributes = MemberAttributes.Public | MemberAttributes.Static,
105-
};
106-
((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Members.Add (f);
111+
};
112+
((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Members.Add (f);
113+
}
114+
}
107115
return g;
108116
}),
109117
Parse (@"^ (0x[xa-fA-F0-9, ]+)$",
110118
(m, app, g, map) => {
111-
var t = (CodeTypeDeclaration) g.Members [g.Members.Count-1];
112-
var f = (CodeMemberField) t.Members [t.Members.Count-1];
113-
string[] values = m.Groups [1].Value.Split (new[]{','}, StringSplitOptions.RemoveEmptyEntries);
114-
CodeArrayCreateExpression c = (CodeArrayCreateExpression) f.InitExpression;
115-
if (c == null) {
116-
f.InitExpression = c = new CodeArrayCreateExpression (typeof (int[]));
119+
if (g != null) {
120+
var t = (CodeTypeDeclaration) g.Members [g.Members.Count-1];
121+
var f = (CodeMemberField) t.Members [t.Members.Count-1];
122+
string[] values = m.Groups [1].Value.Split (new[]{','}, StringSplitOptions.RemoveEmptyEntries);
123+
CodeArrayCreateExpression c = (CodeArrayCreateExpression) f.InitExpression;
124+
if (c == null) {
125+
f.InitExpression = c = new CodeArrayCreateExpression (typeof (int[]));
126+
}
127+
foreach (string value in values)
128+
c.Initializers.Add (new CodePrimitiveExpression (ToInt32 (value.Trim (), 16)));
117129
}
118-
foreach (string value in values)
119-
c.Initializers.Add (new CodePrimitiveExpression (ToInt32 (value.Trim (), 16)));
120130
return g;
121131
}),
122132
};

0 commit comments

Comments
 (0)