Skip to content

Commit c483dba

Browse files
committed
[Mono.Android] Fix NRT warnings in hand bound code.
1 parent 0c97d20 commit c483dba

20 files changed

+96
-69
lines changed

src/Mono.Android/Android.Graphics/Color.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public override void DestroyGenericArgumentState (Color value, ref JniValueMarsh
416416

417417
public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType)
418418
{
419-
var c = typeof (Color).GetConstructor (new[]{typeof (int)});
419+
var c = typeof (Color).GetConstructor (new[]{typeof (int)})!;
420420
var v = Expression.Variable (typeof (Color), sourceValue.Name + "_val");
421421
context.LocalVariables.Add (v);
422422
context.CreationStatements.Add (Expression.Assign (v, Expression.New (c, sourceValue)));

src/Mono.Android/Android.Runtime/JNIEnv.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ public static void CopyArray (IntPtr src, string[] dest)
747747

748748
static TValue GetConverter<TValue>(Dictionary<Type, TValue> dict, Type? elementType, IntPtr array)
749749
{
750-
TValue converter;
750+
TValue? converter;
751751

752752
if (elementType != null) {
753753
if (elementType.IsEnum)

src/Mono.Android/Android.Runtime/JavaSet.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public void CopyTo (T[] array, int array_index)
381381
throw new ArgumentException ("array");
382382

383383
int i = 0;
384-
foreach (T item in this)
384+
foreach (T item in this!)
385385
array [array_index + i++] = item;
386386
}
387387

src/Mono.Android/Android.Runtime/XmlPullParserReader.cs

+15-10
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public QName (XmlPullParserReader r, string name)
7272
}
7373

7474
public string LocalName;
75-
public string Namespace;
75+
public string? Namespace;
7676
}
7777

7878
public XmlPullParserReader (IJavaObject source)
@@ -140,7 +140,7 @@ public override bool EOF {
140140
throw new ArgumentOutOfRangeException ();
141141
}
142142

143-
public override string? GetAttribute (string localName, string namespaceName)
143+
public override string? GetAttribute (string localName, string? namespaceName)
144144
{
145145
return namespaceName == xmlns_uri ? source.GetNamespace (localName) : source.GetAttributeValue (namespaceName, localName);
146146
}
@@ -179,7 +179,7 @@ public override string? LocalName {
179179
}
180180
}
181181

182-
public override string LookupNamespace (string prefix)
182+
public override string? LookupNamespace (string prefix)
183183
{
184184
return nsmgr.LookupNamespace (prefix);
185185
}
@@ -192,7 +192,7 @@ public override void MoveToAttribute (int i)
192192
attr_value = false;
193193
}
194194

195-
public override bool MoveToAttribute (string localName, string namespaceName)
195+
public override bool MoveToAttribute (string localName, string? namespaceName)
196196
{
197197
if (namespaceName == xmlns_uri) {
198198
for (int i = 0; i < ns_count; i++)
@@ -251,7 +251,7 @@ public override string? Name {
251251
get { return String.IsNullOrEmpty (Prefix) ? LocalName : Prefix + ':' + LocalName; }
252252
}
253253

254-
public override XmlNameTable NameTable {
254+
public override XmlNameTable? NameTable {
255255
get { return nsmgr.NameTable; }
256256
}
257257

@@ -297,9 +297,14 @@ public override XmlNodeType NodeType {
297297
}
298298
}
299299

300-
public override string Prefix {
300+
public override string? Prefix {
301301
// getPrefix(), getAttributePrefix(), getNamespacePrefix() are not supported!!!
302-
get { return nsmgr.LookupPrefix (NamespaceURI); }
302+
get {
303+
if (NamespaceURI is null)
304+
return null;
305+
306+
return nsmgr.LookupPrefix (NamespaceURI);
307+
}
303308
}
304309

305310
public override bool Read ()
@@ -331,14 +336,14 @@ public override bool Read ()
331336
if (wasEmptyElement || NodeType == XmlNodeType.EndElement)
332337
nsmgr.PopScope ();
333338
if (NodeType == XmlNodeType.Element) {
334-
if (NamespaceURI != String.Empty && nsmgr.LookupPrefix (NamespaceURI) != String.Empty)
339+
if (!string.IsNullOrEmpty (NamespaceURI) && nsmgr.LookupPrefix (NamespaceURI) != String.Empty)
335340
nsmgr.AddNamespace (String.Empty, NamespaceURI);
336341
else if (NamespaceURI == String.Empty && nsmgr.DefaultNamespace != String.Empty)
337342
nsmgr.AddNamespace (String.Empty, String.Empty);
338343
for (int i = 0; i < source.AttributeCount; i++) {
339344
string? ns = source.GetAttributeNamespace (i);
340-
if (ns != String.Empty && nsmgr.LookupPrefix (ns) == null)
341-
nsmgr.AddNamespace ("p" + i, source.GetAttributeNamespace (i));
345+
if (!string.IsNullOrEmpty (ns) && nsmgr.LookupPrefix (ns) == null)
346+
nsmgr.AddNamespace ("p" + i, ns);
342347
}
343348
nsmgr.PushScope ();
344349
}

src/Mono.Android/Android.Runtime/XmlReaderPullParser.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ public int GetIdAttributeResourceValue (int defaultValue)
111111
return GetAttributeResourceValue (null, "id", defaultValue);
112112
}
113113

114-
public string ClassAttribute {
114+
public string? ClassAttribute {
115115
get { return GetAttributeValue (null, "class"); }
116116
}
117117

118-
public string IdAttribute {
118+
public string? IdAttribute {
119119
get { return GetAttributeValue (null, "id"); }
120120
}
121121

@@ -181,8 +181,11 @@ public string GetAttributeValue (int index)
181181
return r.GetAttribute (index);
182182
}
183183

184-
public string GetAttributeValue (string? namespaceURI, string? name)
184+
public string? GetAttributeValue (string? namespaceURI, string? name)
185185
{
186+
if (name is null)
187+
return null;
188+
186189
return r.GetAttribute (name, namespaceURI);
187190
}
188191

@@ -196,7 +199,7 @@ public bool GetFeature (string? name)
196199
return false;
197200
}
198201

199-
public string GetNamespace (string? prefix)
202+
public string? GetNamespace (string prefix)
200203
{
201204
return r.LookupNamespace (prefix);
202205
}

src/Mono.Android/Android.Telephony/CellInfo.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Android.Telephony {
99

1010
public partial class CellInfo {
1111

12-
static Delegate cb_getCellIdentity;
12+
static Delegate? cb_getCellIdentity;
1313
#pragma warning disable 0169
1414
static Delegate GetGetCellIdentityHandler ()
1515
{
@@ -20,7 +20,7 @@ static Delegate GetGetCellIdentityHandler ()
2020

2121
static IntPtr n_GetCellIdentity (IntPtr jnienv, IntPtr native__this)
2222
{
23-
Android.Telephony.CellInfo __this = global::Java.Lang.Object.GetObject<Android.Telephony.CellInfo> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
23+
var __this = global::Java.Lang.Object.GetObject<Android.Telephony.CellInfo> (jnienv, native__this, JniHandleOwnership.DoNotTransfer)!;
2424
return JNIEnv.ToLocalJniHandle (__this.CellIdentity);
2525
}
2626
#pragma warning restore 0169
@@ -32,15 +32,15 @@ public unsafe virtual Android.Telephony.CellIdentity CellIdentity {
3232
const string __id = "getCellIdentity.()Landroid/telephony/CellIdentity;";
3333
try {
3434
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, null);
35-
return global::Java.Lang.Object.GetObject<Android.Telephony.CellIdentity> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
35+
return global::Java.Lang.Object.GetObject<Android.Telephony.CellIdentity> (__rm.Handle, JniHandleOwnership.TransferLocalRef)!;
3636
}
3737
catch (Java.Lang.NoSuchMethodError) {
3838
throw new Java.Lang.AbstractMethodError (__id);
3939
}
4040
}
4141
}
4242

43-
static Delegate cb_getCellSignalStrength;
43+
static Delegate? cb_getCellSignalStrength;
4444
#pragma warning disable 0169
4545
static Delegate GetGetCellSignalStrengthHandler ()
4646
{
@@ -51,7 +51,7 @@ static Delegate GetGetCellSignalStrengthHandler ()
5151

5252
static IntPtr n_GetCellSignalStrength (IntPtr jnienv, IntPtr native__this)
5353
{
54-
Android.Telephony.CellInfo __this = global::Java.Lang.Object.GetObject<Android.Telephony.CellInfo> (jnienv, native__this, JniHandleOwnership.DoNotTransfer);
54+
var __this = global::Java.Lang.Object.GetObject<Android.Telephony.CellInfo> (jnienv, native__this, JniHandleOwnership.DoNotTransfer)!;
5555
return JNIEnv.ToLocalJniHandle (__this.CellSignalStrength);
5656
}
5757
#pragma warning restore 0169
@@ -63,7 +63,7 @@ public unsafe virtual Android.Telephony.CellSignalStrength CellSignalStrength {
6363
const string __id = "getCellSignalStrength.()Landroid/telephony/CellSignalStrength;";
6464
try {
6565
var __rm = _members.InstanceMethods.InvokeVirtualObjectMethod (__id, this, null);
66-
return global::Java.Lang.Object.GetObject<Android.Telephony.CellSignalStrength> (__rm.Handle, JniHandleOwnership.TransferLocalRef);
66+
return global::Java.Lang.Object.GetObject<Android.Telephony.CellSignalStrength> (__rm.Handle, JniHandleOwnership.TransferLocalRef)!;
6767
}
6868
catch (Java.Lang.NoSuchMethodError) {
6969
throw new Java.Lang.AbstractMethodError (__id);

src/Mono.Android/Java.Interop/JavaConvert.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static T FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer, out
109109

110110
var interned = (IJavaObject?) Java.Lang.Object.PeekObject (handle);
111111
if (interned != null) {
112-
T r = FromJavaObject<T>(interned, out set);
112+
T? r = FromJavaObject<T>(interned, out set);
113113
if (set) {
114114
JNIEnv.DeleteRef (handle, transfer);
115115
return r;
@@ -119,20 +119,20 @@ public static T FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer, out
119119
set = true;
120120

121121
if (typeof (IJavaObject).IsAssignableFrom (typeof (T)))
122-
return (T) Java.Lang.Object._GetObject<T> (handle, transfer);
122+
return (T?) Java.Lang.Object._GetObject<T> (handle, transfer);
123123

124124
var converter = GetJniHandleConverter (typeof (T)) ??
125125
GetJniHandleConverter (GetTypeMapping (handle));
126126
if (converter != null)
127-
return (T) converter (handle, transfer);
127+
return (T?) converter (handle, transfer);
128128

129129
var v = Java.Lang.Object.GetObject (handle, transfer);
130130
if (v is T)
131131
return (T) v;
132132

133133
// hail mary pass; perhaps there's a MCW which participates in normal
134134
// .NET type conversion?
135-
return (T) Convert.ChangeType (v, typeof (T), CultureInfo.InvariantCulture);
135+
return (T?) Convert.ChangeType (v, typeof (T), CultureInfo.InvariantCulture);
136136
}
137137

138138
public static object? FromJniHandle (IntPtr handle, JniHandleOwnership transfer, Type? targetType = null)
@@ -244,7 +244,7 @@ public static T FromJavaObject<T>(IJavaObject? value, out bool set)
244244
set = true;
245245
var converter = GetJniHandleConverter (typeof (T));
246246
if (converter != null)
247-
return (T) converter (lrefValue, JniHandleOwnership.TransferLocalRef);
247+
return (T?) converter (lrefValue, JniHandleOwnership.TransferLocalRef);
248248
JNIEnv.DeleteLocalRef (lrefValue);
249249
return (T) Convert.ChangeType (value, typeof (T), CultureInfo.InvariantCulture);
250250
}

src/Mono.Android/Java.Interop/JavaObjectExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal static TResult _JavaCast<TResult> (this IJavaObject? instance)
6969
return (TResult) CastClass (instance, resultType);
7070
}
7171
else if (resultType.IsInterface) {
72-
return (TResult) Java.Lang.Object.GetObject (instance.Handle, JniHandleOwnership.DoNotTransfer, resultType);
72+
return (TResult?) Java.Lang.Object.GetObject (instance.Handle, JniHandleOwnership.DoNotTransfer, resultType);
7373
}
7474
else
7575
throw new NotSupportedException (FormattableString.Invariant ($"Unable to convert type '{instance.GetType ().FullName}' to '{resultType.FullName}'."));

src/Mono.Android/Java.Interop/TypeManager.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Java.Interop.Tools.TypeNameMappings;
88

99
using Android.Runtime;
10+
using System.Diagnostics.CodeAnalysis;
1011

1112
namespace Java.Interop {
1213

@@ -166,7 +167,7 @@ static void n_Activate (IntPtr jnienv, IntPtr jclass, IntPtr typename_ptr, IntPt
166167
internal static void Activate (IntPtr jobject, ConstructorInfo cinfo, object? []? parms)
167168
{
168169
try {
169-
var newobj = RuntimeHelpers.GetUninitializedObject (cinfo.DeclaringType);
170+
var newobj = RuntimeHelpers.GetUninitializedObject (cinfo.DeclaringType!);
170171
if (newobj is Java.Lang.Object o) {
171172
o.handle = jobject;
172173
} else if (newobj is Java.Lang.Throwable throwable) {
@@ -177,7 +178,7 @@ internal static void Activate (IntPtr jobject, ConstructorInfo cinfo, object? []
177178
cinfo.Invoke (newobj, parms);
178179
} catch (Exception e) {
179180
var m = FormattableString.Invariant (
180-
$"Could not activate JNI Handle 0x{jobject:x} (key_handle 0x{JNIEnv.IdentityHash (jobject):x}) of Java type '{JNIEnv.GetClassNameFromInstance (jobject)}' as managed type '{cinfo.DeclaringType.FullName}'.");
181+
$"Could not activate JNI Handle 0x{jobject:x} (key_handle 0x{JNIEnv.IdentityHash (jobject):x}) of Java type '{JNIEnv.GetClassNameFromInstance (jobject)}' as managed type '{cinfo?.DeclaringType?.FullName}'.");
181182
Logger.Log (LogLevel.Warn, "monodroid", m);
182183
Logger.Log (LogLevel.Warn, "monodroid", CreateJavaLocationException ().ToString ());
183184

@@ -238,7 +239,7 @@ static Exception CreateJavaLocationException ()
238239
Type? type = null;
239240
int ls = class_name.LastIndexOf ('/');
240241
var package = ls >= 0 ? class_name.Substring (0, ls) : "";
241-
if (packageLookup.TryGetValue (package, out var mappers)) {
242+
if (packageLookup!.TryGetValue (package, out var mappers)) {
242243
foreach (Converter<string, Type?> c in mappers) {
243244
type = c (class_name);
244245
if (type == null)
@@ -360,6 +361,7 @@ public static void RegisterType (string java_class, Type t)
360361

361362
static Dictionary<string, List<Converter<string, Type?>>>? packageLookup;
362363

364+
[MemberNotNull (nameof (packageLookup))]
363365
static void LazyInitPackageLookup ()
364366
{
365367
if (packageLookup == null)

src/Mono.Android/Java.Lang/Object.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected void SetHandle (IntPtr value, JniHandleOwnership transfer)
266266
[return: MaybeNull]
267267
internal static T PeekObject <T> (IntPtr handle)
268268
{
269-
return (T)PeekObject (handle, typeof (T));
269+
return (T?)PeekObject (handle, typeof (T));
270270
}
271271

272272
public static T? GetObject<T> (IntPtr jnienv, IntPtr handle, JniHandleOwnership transfer)
@@ -288,7 +288,7 @@ internal static T _GetObject<T> (IntPtr handle, JniHandleOwnership transfer)
288288
if (handle == IntPtr.Zero)
289289
return default (T);
290290

291-
return (T) GetObject (handle, transfer, typeof (T));
291+
return (T?) GetObject (handle, transfer, typeof (T));
292292
}
293293

294294
internal static IJavaPeerable? GetObject (IntPtr handle, JniHandleOwnership transfer, Type? type = null)

src/Mono.Android/System.Drawing/PointConverter.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public override bool CanConvertTo (ITypeDescriptorContext context,
6262
return base.CanConvertTo (context, destinationType);
6363
}
6464

65-
public override object ConvertFrom (ITypeDescriptorContext context,
65+
public override object? ConvertFrom (ITypeDescriptorContext context,
6666
CultureInfo culture,
6767
object value)
6868
{
@@ -77,7 +77,10 @@ public override object ConvertFrom (ITypeDescriptorContext context,
7777
Int32Converter converter = new Int32Converter ();
7878
int[] numSubs = new int[subs.Length];
7979
for (int i = 0; i < numSubs.Length; i++) {
80-
numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
80+
if (converter.ConvertFromString (context, culture, subs[i]) is int num)
81+
numSubs[i] = num;
82+
else
83+
throw new ArgumentException ($"Could not parse string '{subs[i]}' as integer");
8184
}
8285

8386
if (subs.Length != 2)
@@ -86,7 +89,7 @@ public override object ConvertFrom (ITypeDescriptorContext context,
8689
return new Point (numSubs[0], numSubs[1]);
8790
}
8891

89-
public override object ConvertTo (ITypeDescriptorContext context,
92+
public override object? ConvertTo (ITypeDescriptorContext context,
9093
CultureInfo culture,
9194
object value,
9295
Type destinationType)
@@ -130,7 +133,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
130133
return true;
131134
}
132135

133-
public override PropertyDescriptorCollection GetProperties (
136+
public override PropertyDescriptorCollection? GetProperties (
134137
ITypeDescriptorContext context,
135138
object value, Attribute[] attributes)
136139
{

src/Mono.Android/System.Drawing/RectangleConverter.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public override bool CanConvertTo (ITypeDescriptorContext context,
6666
return base.CanConvertTo (context, destinationType);
6767
}
6868

69-
public override object ConvertFrom (ITypeDescriptorContext context,
69+
public override object? ConvertFrom (ITypeDescriptorContext context,
7070
CultureInfo culture,
7171
object value)
7272
{
@@ -79,7 +79,10 @@ public override object ConvertFrom (ITypeDescriptorContext context,
7979
Int32Converter converter = new Int32Converter ();
8080
int[] numSubs = new int[subs.Length];
8181
for (int i = 0; i < numSubs.Length; i++) {
82-
numSubs[i] = (int) converter.ConvertFromString (context, culture, subs[i]);
82+
if (converter.ConvertFromString (context, culture, subs[i]) is int num)
83+
numSubs[i] = num;
84+
else
85+
throw new ArgumentException ($"Could not parse string '{subs[i]}' as integer");
8386
}
8487

8588
if (subs.Length != 4)
@@ -88,7 +91,7 @@ public override object ConvertFrom (ITypeDescriptorContext context,
8891
return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
8992
}
9093

91-
public override object ConvertTo (ITypeDescriptorContext context,
94+
public override object? ConvertTo (ITypeDescriptorContext context,
9295
CultureInfo culture,
9396
object value,
9497
Type destinationType)
@@ -144,7 +147,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
144147
return true;
145148
}
146149

147-
public override PropertyDescriptorCollection GetProperties (
150+
public override PropertyDescriptorCollection? GetProperties (
148151
ITypeDescriptorContext context,
149152
object value, Attribute[] attributes)
150153
{

0 commit comments

Comments
 (0)