Skip to content

Commit 549548c

Browse files
[xabt] Fix NRT annotations in Xamarin.Android.Build.Tasks, part 4 (#10364)
Context: #10326 This PR enables nullable reference types (NRT) for two files in the `src\Xamarin.Android.Build.Tasks\Utilities` directory, following the repository's NRT guidelines. ## Changes Made **JavaResourceParser.cs:** - Enabled `#nullable enable` - Made `CodeTypeDeclaration` non-nullable throughout for cleaner API design - Updated `Parse` method to always return a valid `CodeTypeDeclaration` object instead of null - Removed unnecessary null checks in lambda functions and used null-coalescing operators - Simplified calling code in `GenerateResourceDesigner.cs` by eliminating null handling - Maintained Log property validation for safety **MD2Managed.cs:** - Enabled `#nullable enable` - Fixed `Padding` method to throw `ArgumentException` for invalid input instead of returning null - Preserved existing cryptographic logic while ensuring type safety Co-authored-by: Jonathan Peppers <[email protected]>
1 parent dd00d48 commit 549548c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#nullable disable
2-
1+
#nullable enable
32
using System;
43
using System.CodeDom;
54
using System.Collections.Generic;
@@ -18,7 +17,10 @@ public CodeTypeDeclaration Parse (string file, bool isApp, Dictionary<string, st
1817
if (!File.Exists (file))
1918
throw new InvalidOperationException ("Specified Java resource file was not found: " + file);
2019

21-
CodeTypeDeclaration resources = null;
20+
if (Log == null)
21+
throw new InvalidOperationException ("Log property must be set before calling Parse");
22+
23+
CodeTypeDeclaration? resources = null;
2224

2325
using (var reader = File.OpenText (file)) {
2426
string line;
@@ -33,12 +35,12 @@ public CodeTypeDeclaration Parse (string file, bool isApp, Dictionary<string, st
3335
}
3436
}
3537

36-
return resources;
38+
return resources ?? new CodeTypeDeclaration ("Resource") { IsPartial = true };
3739
}
3840

39-
static KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>> Parse (string regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration> f)
41+
static KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration>> Parse (string regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration> f)
4042
{
41-
return new KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>> (new Regex (regex), f);
43+
return new KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration>> (new Regex (regex), f);
4244
}
4345

4446
// public finall class R {
@@ -49,11 +51,11 @@ static KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<str
4951
// }
5052
// }
5153
// }
52-
List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>>> Parser;
54+
List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration>>> Parser;
5355

5456
public JavaResourceParser ()
5557
{
56-
Parser = new List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration, Dictionary<string, string>, CodeTypeDeclaration>>> () {
58+
Parser = new List<KeyValuePair<Regex, Func<Match, bool, CodeTypeDeclaration?, Dictionary<string, string>, CodeTypeDeclaration>>> () {
5759
Parse ("^public final class R {",
5860
(m, app, _, map) => {
5961
var decl = new CodeTypeDeclaration ("Resource") {
@@ -73,6 +75,7 @@ public JavaResourceParser ()
7375
}),
7476
Parse ("^ public static final class ([^ ]+) {$",
7577
(m, app, g, map) => {
78+
g ??= new CodeTypeDeclaration ("Resource") { IsPartial = true };
7679
var t = new CodeTypeDeclaration (GetNestedTypeName (m.Groups [1].Value)) {
7780
IsPartial = true,
7881
TypeAttributes = TypeAttributes.Public,
@@ -85,8 +88,9 @@ public JavaResourceParser ()
8588
}),
8689
Parse (@"^ public static final int ([^ =]+)\s*=\s*([^;]+);$",
8790
(m, app, g, map) => {
91+
g ??= new CodeTypeDeclaration ("Resource") { IsPartial = true };
8892
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)) {
93+
var f = new CodeMemberField (typeof (int), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log!)) {
9094
Attributes = app ? MemberAttributes.Const | MemberAttributes.Public : MemberAttributes.Static | MemberAttributes.Public,
9195
InitExpression = new CodePrimitiveExpression (ToInt32 (m.Groups [2].Value, m.Groups [2].Value.IndexOf ("0x", StringComparison.Ordinal) == 0 ? 16 : 10)),
9296
Comments = {
@@ -98,8 +102,9 @@ public JavaResourceParser ()
98102
}),
99103
Parse (@"^ public static final int\[\] ([^ =]+) = {",
100104
(m, app, g, map) => {
105+
g ??= new CodeTypeDeclaration ("Resource") { IsPartial = true };
101106
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)) {
107+
var f = new CodeMemberField (typeof (int[]), ResourceIdentifier.GetResourceName (name, m.Groups[1].Value, map, Log!)) {
103108
// pity I can't make the member readonly...
104109
Attributes = MemberAttributes.Public | MemberAttributes.Static,
105110
};
@@ -108,6 +113,7 @@ public JavaResourceParser ()
108113
}),
109114
Parse (@"^ (0x[xa-fA-F0-9, ]+)$",
110115
(m, app, g, map) => {
116+
g ??= new CodeTypeDeclaration ("Resource") { IsPartial = true };
111117
var t = (CodeTypeDeclaration) g.Members [g.Members.Count-1];
112118
var f = (CodeMemberField) t.Members [t.Members.Count-1];
113119
string[] values = m.Groups [1].Value.Split (new[]{','}, StringSplitOptions.RemoveEmptyEntries);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2828
//
2929

30-
#nullable disable
30+
#nullable enable
3131

3232
using System;
3333

@@ -80,7 +80,7 @@ private byte[] Padding (int nLength)
8080
padding[i] = (byte) nLength;
8181
return padding;
8282
}
83-
return null;
83+
throw new ArgumentException ("Length must be greater than 0", nameof (nLength));
8484
}
8585

8686
//--- constructor -----------------------------------------------------------

0 commit comments

Comments
 (0)