Skip to content

Commit e747bac

Browse files
committed
Use C# 9's T? instead of [return: MaybeNull] for better annotations.
1 parent de52d41 commit e747bac

File tree

14 files changed

+22
-47
lines changed

14 files changed

+22
-47
lines changed

src/Mono.Android/Android.App/Dialog.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics.CodeAnalysis;
32
using Android.Runtime;
43

54
namespace Android.App {
@@ -9,8 +8,7 @@ public partial class Dialog {
98
protected Dialog (Android.Content.Context context, bool cancelable, EventHandler cancelHandler)
109
: this (context, cancelable, new Android.Content.IDialogInterfaceOnCancelListenerImplementor () { Handler = cancelHandler }) {}
1110

12-
[return: MaybeNull]
13-
public T FindViewById<T> (int id)
11+
public T? FindViewById<T> (int id)
1412
where T : Android.Views.View
1513
{
1614
return this.FindViewById (id).JavaCast<T> ();

src/Mono.Android/Android.App/FragmentManager.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
using System.Diagnostics.CodeAnalysis;
21
using Android.OS;
32
using Android.Runtime;
43

54
#if ANDROID_11
65
namespace Android.App {
76
public partial class FragmentManager {
8-
[return: MaybeNull]
9-
public T FindFragmentById<T> (int id) where T : Fragment
7+
public T? FindFragmentById<T> (int id) where T : Fragment
108
{
119
return FindFragmentById (id).JavaCast<T> ();
1210
}
13-
[return: MaybeNull]
14-
public T FindFragmentByTag<T> (string tag) where T : Fragment
11+
public T? FindFragmentByTag<T> (string tag) where T : Fragment
1512
{
1613
return FindFragmentByTag (tag).JavaCast<T> ();
1714
}
18-
[return: MaybeNull]
19-
public T GetFragment<T> (Bundle bundle, string key) where T : Fragment
15+
public T? GetFragment<T> (Bundle bundle, string key) where T : Fragment
2016
{
2117
return GetFragment (bundle, key).JavaCast<T> ();
2218
}

src/Mono.Android/Android.OS/AsyncTask.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ public AsyncTask ()
8888
}
8989

9090
static IntPtr id_get;
91-
[return: MaybeNull]
9291
[Register ("get", "()Ljava/lang/Object;", "")]
93-
public TResult GetResult ()
92+
public TResult? GetResult ()
9493
{
9594
if (id_get == IntPtr.Zero)
9695
id_get = JNIEnv.GetMethodID (class_ref, "get", "()Ljava/lang/Object;");

src/Mono.Android/Android.Runtime/JavaCollection.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
386386
return GetEnumerator ()!;
387387
}
388388

389-
[return: MaybeNull]
390-
public IEnumerator<T> GetEnumerator ()
389+
public IEnumerator<T?> GetEnumerator ()
391390
{
392391
return System.Linq.Extensions.ToEnumerator_Dispose<T> (Iterator());
393392
}

src/Mono.Android/Android.Runtime/JavaDictionary.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ public JavaDictionary (IDictionary<K, V> items) : this ()
444444
//
445445
// https://developer.android.com/reference/java/util/Map#get(java.lang.Object)
446446
//
447-
[return: MaybeNull]
448-
internal V Get (K key)
447+
internal V? Get (K key)
449448
{
450449
if (id_get == IntPtr.Zero)
451450
id_get = JNIEnv.GetMethodID (map_class, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");

src/Mono.Android/Android.Runtime/JavaList.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,7 @@ public JavaList (IEnumerable<T> items) : this ()
727727
//
728728
// https://developer.android.com/reference/java/util/List.html?hl=en#get(int)
729729
//
730-
[return: MaybeNull]
731-
internal unsafe T InternalGet (int location)
730+
internal unsafe T? InternalGet (int location)
732731
{
733732
const string id = "get.(I)Ljava/lang/Object;";
734733
JniObjectReference obj;
@@ -880,8 +879,7 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
880879
return GetEnumerator ()!;
881880
}
882881

883-
[return: MaybeNull]
884-
public IEnumerator<T> GetEnumerator ()
882+
public IEnumerator<T?> GetEnumerator ()
885883
{
886884
return System.Linq.Extensions.ToEnumerator_Dispose<T> (Iterator ());
887885
}

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

+1-2
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

@@ -390,7 +390,6 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
390390
return GetEnumerator ()!;
391391
}
392392

393-
[return: MaybeNull]
394393
public IEnumerator<T> GetEnumerator ()
395394
{
396395
return System.Linq.Extensions.ToEnumerator_Dispose<T> (Iterator ());

src/Mono.Android/Android.Views/View.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public bool PerformAccessibilityAction (GlobalAction action, Bundle arguments)
2222
}
2323
#endif
2424

25-
[return: MaybeNull]
26-
public T FindViewById<T> (int id)
25+
public T? FindViewById<T> (int id)
2726
where T : Android.Views.View
2827
{
2928
return this.FindViewById (id).JavaCast<T> ();

src/Mono.Android/Android.Views/Window.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ namespace Android.Views {
66

77
partial class Window {
88

9-
[return: MaybeNull]
10-
public T FindViewById<T> (int id)
9+
public T? FindViewById<T> (int id)
1110
where T : Android.Views.View
1211
{
1312
return this.FindViewById (id).JavaCast<T> ();

src/Mono.Android/Android.Widget/ArrayAdapter.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ public void Add (T @object)
185185
}
186186

187187
static IntPtr id_getItem_I;
188-
[return: MaybeNull]
189188
[Register ("getItem", "(I)Ljava/lang/Object;", "GetGetItem_IHandler")]
190-
public T GetItem (int position)
189+
public T? GetItem (int position)
191190
{
192191
if (id_getItem_I == IntPtr.Zero)
193192
id_getItem_I = JNIEnv.GetMethodID (class_ref, "getItem", "(I)Ljava/lang/Object;");

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ static Func<IntPtr, JniHandleOwnership, object> GetJniHandleConverterForType ([D
9292
typeof (Func<IntPtr, JniHandleOwnership, object>), m);
9393
}
9494

95-
[return: MaybeNull]
96-
public static T FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer)
95+
public static T? FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer)
9796
{
9897
bool set;
9998
return FromJniHandle<T>(handle, transfer, out set);
10099
}
101100

102-
[return: MaybeNull]
103-
public static T FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer, out bool set)
101+
public static T? FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer, out bool set)
104102
{
105103
if (handle == IntPtr.Zero) {
106104
set = false;
@@ -208,15 +206,13 @@ public static T FromJniHandle<T>(IntPtr handle, JniHandleOwnership transfer, out
208206
return null;
209207
}
210208

211-
[return: MaybeNull]
212-
public static T FromJavaObject<T>(IJavaObject? value)
209+
public static T? FromJavaObject<T>(IJavaObject? value)
213210
{
214211
bool set;
215212
return FromJavaObject<T>(value, out set);
216213
}
217214

218-
[return: MaybeNull]
219-
public static T FromJavaObject<T>(IJavaObject? value, out bool set)
215+
public static T? FromJavaObject<T>(IJavaObject? value, out bool set)
220216
{
221217
if (value == null) {
222218
set = false;

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public static JavaDictionary<K,V> ToInteroperableCollection<K,V> (this IDictiona
5252
return _JavaCast<TResult> (instance);
5353
}
5454

55-
[return: MaybeNull]
56-
internal static TResult _JavaCast<TResult> (this IJavaObject? instance)
55+
internal static TResult? _JavaCast<TResult> (this IJavaObject? instance)
5756
{
5857
if (instance == null)
5958
return default (TResult);

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ protected void SetHandle (IntPtr value, JniHandleOwnership transfer)
263263
return peeked;
264264
}
265265

266-
[return: MaybeNull]
267-
internal static T PeekObject <T> (IntPtr handle)
266+
internal static T? PeekObject <T> (IntPtr handle)
268267
{
269268
return (T?)PeekObject (handle, typeof (T));
270269
}
@@ -282,8 +281,7 @@ protected void SetHandle (IntPtr value, JniHandleOwnership transfer)
282281
return _GetObject<T>(handle, transfer);
283282
}
284283

285-
[return: MaybeNull]
286-
internal static T _GetObject<T> (IntPtr handle, JniHandleOwnership transfer)
284+
internal static T? _GetObject<T> (IntPtr handle, JniHandleOwnership transfer)
287285
{
288286
if (handle == IntPtr.Zero)
289287
return default (T);
@@ -306,8 +304,7 @@ internal static T _GetObject<T> (IntPtr handle, JniHandleOwnership transfer)
306304
}
307305

308306
[EditorBrowsable (EditorBrowsableState.Never)]
309-
[return: MaybeNull]
310-
public T[] ToArray<T>()
307+
public T[]? ToArray<T>()
311308
{
312309
return JNIEnv.GetArray<T>(Handle);
313310
}

src/Mono.Android/System.Linq/Extensions.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ internal static IEnumerator ToEnumerator_Dispose (this Java.Util.IIterator sourc
4040
}
4141
}
4242

43-
[return: MaybeNull]
44-
public static IEnumerable<T> ToEnumerable<T> (this Java.Lang.IIterable source)
43+
public static IEnumerable<T?> ToEnumerable<T> (this Java.Lang.IIterable source)
4544
{
4645
if (source == null)
4746
throw new ArgumentNullException ("source");
@@ -53,7 +52,6 @@ public static IEnumerable<T> ToEnumerable<T> (this Java.Lang.IIterable source)
5352
}
5453
}
5554

56-
[return: MaybeNull]
5755
internal static IEnumerator<T> ToEnumerator_Dispose<T> (this Java.Util.IIterator source)
5856
{
5957
using (source)

0 commit comments

Comments
 (0)