Skip to content

Commit 01e4fb9

Browse files
committed
Include JavaConvert collection factory refactor
1 parent e89f3a0 commit 01e4fb9

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

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

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,9 @@ static class JavaConvert {
141141
if (factoryConverter != null)
142142
return factoryConverter;
143143
} else if (RuntimeFeature.IsMonoRuntime || RuntimeFeature.IsCoreClrRuntime) {
144-
if (target.GetGenericTypeDefinition() == typeof (IDictionary<,>)) {
145-
Type t = typeof (JavaDictionary<,>).MakeGenericType (target.GetGenericArguments ());
146-
return GetJniHandleConverterForType (t);
147-
}
148-
if (target.GetGenericTypeDefinition() == typeof (IList<>)) {
149-
Type t = typeof (JavaList<>).MakeGenericType (target.GetGenericArguments ());
150-
return GetJniHandleConverterForType (t);
151-
}
152-
if (target.GetGenericTypeDefinition() == typeof (ICollection<>)) {
153-
Type t = typeof (JavaCollection<>).MakeGenericType (target.GetGenericArguments ());
154-
return GetJniHandleConverterForType (t);
155-
}
144+
var factoryConverter = TryMakeGenericCollectionTypeFactory (target);
145+
if (factoryConverter != null)
146+
return factoryConverter;
156147
}
157148
}
158149

@@ -164,6 +155,26 @@ static class JavaConvert {
164155
return (h, t) => JavaCollection.FromJniHandle (h, t);
165156

166157
return null;
158+
159+
[UnconditionalSuppressMessage ("ReflectionAnalysis", "IL2055:RequiresUnreferencedCode",
160+
Justification = "The target generic type is expected to be preserved by the trimmer as the target type in marshaling.")]
161+
static Func<IntPtr, JniHandleOwnership, object?>? TryMakeGenericCollectionTypeFactory (Type target)
162+
{
163+
if (target.GetGenericTypeDefinition() == typeof (IDictionary<,>)) {
164+
Type t = typeof (JavaDictionary<,>).MakeGenericType (target.GetGenericArguments ());
165+
return GetJniHandleConverterForType (t);
166+
}
167+
if (target.GetGenericTypeDefinition() == typeof (IList<>)) {
168+
Type t = typeof (JavaList<>).MakeGenericType (target.GetGenericArguments ());
169+
return GetJniHandleConverterForType (t);
170+
}
171+
if (target.GetGenericTypeDefinition() == typeof (ICollection<>)) {
172+
Type t = typeof (JavaCollection<>).MakeGenericType (target.GetGenericArguments ());
173+
return GetJniHandleConverterForType (t);
174+
}
175+
176+
return null;
177+
}
167178
}
168179

169180
/// <summary>
@@ -242,27 +253,17 @@ static Func<IntPtr, JniHandleOwnership, object> GetJniHandleConverterForType ([D
242253

243254
internal readonly struct ArrayElementConverter
244255
{
245-
[DynamicallyAccessedMembers (Constructors)]
246256
readonly Type? elementType;
247-
readonly Func<IntPtr, JniHandleOwnership, object>? converter;
257+
readonly Func<IntPtr, JniHandleOwnership, object?>? converter;
248258
readonly bool useRuntimeTypeMapping;
249259

250260
public ArrayElementConverter (Array array)
251261
{
252-
elementType = GetArrayElementType (array);
262+
elementType = array.GetType ().GetElementType ();
253263
converter = elementType != null ? GetJniHandleConverter (elementType) : null;
254264
useRuntimeTypeMapping = elementType is null || elementType == typeof (object);
255265
}
256266

257-
// Array.GetType ().GetElementType () cannot statically carry the constructor annotations that
258-
// peer construction (Java.Lang.Object.GetObject) requires. The element types of arrays that are
259-
// marshaled back to managed peers are preserved by the Android linker steps, so isolate the
260-
// unprovable flow here rather than suppressing the whole conversion path.
261-
[UnconditionalSuppressMessage ("Trimming", "IL2073",
262-
Justification = "Array element types marshaled to managed peers are preserved by the Android linker steps.")]
263-
[return: DynamicallyAccessedMembers (Constructors)]
264-
static Type? GetArrayElementType (Array array) => array.GetType ().GetElementType ();
265-
266267
public object? FromJniHandle (IntPtr handle, JniHandleOwnership transfer)
267268
{
268269
if (handle == IntPtr.Zero)
@@ -461,7 +462,9 @@ public static T? FromJniHandle<
461462
{
462463
var lref = JniEnvironment.Types.GetObjectClass (new JniObjectReference (handle));
463464
try {
464-
string className = JniEnvironment.Types.GetJniTypeNameFromClass (lref);
465+
string? className = JniEnvironment.Types.GetJniTypeNameFromClass (lref);
466+
if (className is null)
467+
return null;
465468
if (TypeMappings.TryGetValue (className, out var match))
466469
return match;
467470
if (JniEnvironment.Types.IsAssignableFrom (lref, new JniObjectReference (JavaDictionary.map_class)))

0 commit comments

Comments
 (0)