Skip to content

Commit 8daa026

Browse files
authored
[Xamarin.Android.Tools.Bytecode] Fix Nullability warnings (#922)
Context: 77c9c5f Commit 77c9c5f inadvertently introduced nullability warnings: …\Xamarin.Android.Tools.Bytecode\ClassPath.cs(308,89): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\ClassPath.cs(308,54): warning CS8604: Possible null reference argument for parameter 'jniType' in 'JavaMethodParameterTypeInfo.JavaMethodParameterTypeInfo(string jniType, string javaType)'. …\Xamarin.Android.Tools.Bytecode\ClassPath.cs(308,89): warning CS8604: Possible null reference argument for parameter 'javaType' in 'JavaMethodParameterTypeInfo.JavaMethodParameterTypeInfo(string jniType, string javaType)'. …\Xamarin.Android.Tools.Bytecode\ClassPath.cs(318,8): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(403,19): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(405,19): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(413,46): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(419,58): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(439,19): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(438,11): warning CS8619: Nullability of reference types in value of type 'string?[]' doesn't match target type 'string[]'. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(444,17): warning CS8600: Converting null literal or possible null value to non-nullable type. …\Xamarin.Android.Tools.Bytecode\JavaDocumentScraper.cs(445,10): warning CS8602: Dereference of a possibly null reference. Fix these nullability warnings.
1 parent 77c9c5f commit 8daa026

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Xamarin.Android.Tools.Bytecode/ClassPath.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ void FixupParametersFromDocs (XElement api, string path)
305305
continue;
306306

307307
var parameterTypes = parameterElements
308-
.Select (p => new JavaMethodParameterTypeInfo ((string) p.Attribute ("jni-type"), (string) p.Attribute ("type")))
308+
.Select (p => new JavaMethodParameterTypeInfo (
309+
jniType: (string?) p.Attribute ("jni-type") ?? "",
310+
javaType: (string?) p.Attribute ("type") ?? ""))
309311
.ToArray ();
310312

311313
if (!parameterTypes.Any ())
@@ -315,7 +317,7 @@ void FixupParametersFromDocs (XElement api, string path)
315317
currentpackage,
316318
className,
317319
currentMethod,
318-
(string) method.Attribute ("jni-signature"),
320+
(string?) method.Attribute ("jni-signature"),
319321
parameterTypes,
320322
isVarArgs: false
321323
);

src/Xamarin.Android.Tools.Bytecode/JavaDocumentScraper.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,23 @@ public ApiXmlDocScraper (string apiXmlFile)
400400
var xtype = xdoc
401401
.Elements ("api")
402402
.Elements ("package")
403-
.Where (p => ((string) p.Attribute ("name")) == info.PackageName)
403+
.Where (p => ((string?) p.Attribute ("name")) == info.PackageName)
404404
.Elements ()
405-
.Where (t => ((string) t.Attribute ("name")) == info.TypeName)
405+
.Where (t => ((string?) t.Attribute ("name")) == info.TypeName)
406406
.FirstOrDefault ();
407407
if (xtype == null) {
408408
return null;
409409
}
410410

411411
var members = info.MethodName == "constructor"
412412
? xtype.Elements ("constructor")
413-
: xtype.Elements ("method").Where (m => ((string) m.Attribute ("name")) == info.MethodName);
413+
: xtype.Elements ("method").Where (m => ((string?) m.Attribute ("name")) == info.MethodName);
414414
var pcount = info.ParameterTypes.Length;
415415
members = members
416416
.Where (m => m.Elements ("parameter").Count () == pcount);
417417

418418
XElement? member =
419-
members.FirstOrDefault (m => info.MethodSignature == (string) m.Attribute ("jni-signature"));
419+
members.FirstOrDefault (m => info.MethodSignature == (string?) m.Attribute ("jni-signature"));
420420
if (member == null) {
421421
foreach (var m in members) {
422422
var found = true;
@@ -436,12 +436,15 @@ public ApiXmlDocScraper (string apiXmlFile)
436436
if (member == null)
437437
return null;
438438
return member.Elements ("parameter")
439-
.Select (p => (string) p.Attribute ("name"))
439+
.Select (p => ((string?) p.Attribute ("name")) ?? "")
440440
.ToArray ();
441441

442442
bool ParameterTypesMatch (XElement parameter, string ptype)
443443
{
444-
var jtype = (string) parameter.Attribute ("type");
444+
var jtype = (string?) parameter.Attribute ("type");
445+
if (jtype == null) {
446+
return false;
447+
}
445448
if (!jtype.StartsWith (".*", StringComparison.Ordinal)) {
446449
return jtype == ptype;
447450
}

0 commit comments

Comments
 (0)