Skip to content

Commit b7ee46b

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

19 files changed

+72
-57
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

+2-2
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
{
@@ -40,7 +40,7 @@ public unsafe virtual Android.Telephony.CellIdentity CellIdentity {
4040
}
4141
}
4242

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

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

+3-3
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
{
@@ -86,7 +86,7 @@ public override object ConvertFrom (ITypeDescriptorContext context,
8686
return new Point (numSubs[0], numSubs[1]);
8787
}
8888

89-
public override object ConvertTo (ITypeDescriptorContext context,
89+
public override object? ConvertTo (ITypeDescriptorContext context,
9090
CultureInfo culture,
9191
object value,
9292
Type destinationType)
@@ -130,7 +130,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
130130
return true;
131131
}
132132

133-
public override PropertyDescriptorCollection GetProperties (
133+
public override PropertyDescriptorCollection? GetProperties (
134134
ITypeDescriptorContext context,
135135
object value, Attribute[] attributes)
136136
{

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

+3-3
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
{
@@ -88,7 +88,7 @@ public override object ConvertFrom (ITypeDescriptorContext context,
8888
return new Rectangle (numSubs[0], numSubs[1], numSubs[2], numSubs[3]);
8989
}
9090

91-
public override object ConvertTo (ITypeDescriptorContext context,
91+
public override object? ConvertTo (ITypeDescriptorContext context,
9292
CultureInfo culture,
9393
object value,
9494
Type destinationType)
@@ -144,7 +144,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
144144
return true;
145145
}
146146

147-
public override PropertyDescriptorCollection GetProperties (
147+
public override PropertyDescriptorCollection? GetProperties (
148148
ITypeDescriptorContext context,
149149
object value, Attribute[] attributes)
150150
{

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

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

68-
public override object ConvertFrom (ITypeDescriptorContext context,
68+
public override object? ConvertFrom (ITypeDescriptorContext context,
6969
CultureInfo culture,
7070
object value)
7171
{
@@ -89,7 +89,7 @@ public override object ConvertFrom (ITypeDescriptorContext context,
8989
return new Size (numSubs[0], numSubs[1]);
9090
}
9191

92-
public override object ConvertTo (ITypeDescriptorContext context,
92+
public override object? ConvertTo (ITypeDescriptorContext context,
9393
CultureInfo culture,
9494
object value,
9595
Type destinationType)
@@ -132,7 +132,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
132132
return true;
133133
}
134134

135-
public override PropertyDescriptorCollection GetProperties (
135+
public override PropertyDescriptorCollection? GetProperties (
136136
ITypeDescriptorContext context,
137137
object value, Attribute[] attributes)
138138
{

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

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

69-
public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
69+
public override object? ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
7070
{
7171
var s = value as string;
7272
if (s == null)
@@ -87,7 +87,7 @@ public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo
8787

8888
}
8989

90-
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
90+
public override object? ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
9191
{
9292
if (value is SizeF) {
9393
SizeF size = (SizeF) value;
@@ -115,7 +115,7 @@ public override bool GetCreateInstanceSupported (ITypeDescriptorContext context)
115115
return true;
116116
}
117117

118-
public override PropertyDescriptorCollection GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
118+
public override PropertyDescriptorCollection? GetProperties (ITypeDescriptorContext context, object value, Attribute[] attributes)
119119
{
120120
if (value is SizeF)
121121
return TypeDescriptor.GetProperties (value, attributes);

src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ protected virtual Task SetupRequest (HttpRequestMessage request, HttpURLConnecti
310310
}
311311

312312
[DynamicDependency (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor, typeof (AndroidMessageHandler))]
313-
object GetUnderlyingHandler ()
313+
object? GetUnderlyingHandler ()
314314
{
315315
var fieldName = "_nativeHandler";
316316
FieldInfo? field = null;

0 commit comments

Comments
 (0)