|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using Android.Runtime; |
| 9 | + |
| 10 | +namespace Java.Interop |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// AOT-safe factory for creating typed containers (arrays, lists, collections, dictionaries) |
| 14 | + /// without using <c>MakeGenericType()</c> or <c>Array.CreateInstance()</c>. |
| 15 | + /// </summary> |
| 16 | + public abstract class JavaPeerContainerFactory |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Creates a typed jagged array. Rank 1 = T[], rank 2 = T[][], etc. |
| 20 | + /// </summary> |
| 21 | + internal abstract Array CreateArray (int length, int rank); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a typed <c>JavaList<T></c> from a JNI handle. |
| 25 | + /// </summary> |
| 26 | + internal abstract IList CreateList (IntPtr handle, JniHandleOwnership transfer); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Creates a typed <c>JavaCollection<T></c> from a JNI handle. |
| 30 | + /// </summary> |
| 31 | + internal abstract ICollection CreateCollection (IntPtr handle, JniHandleOwnership transfer); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Creates a typed <c>JavaDictionary<TKey, TValue></c> using the visitor pattern. |
| 35 | + /// This factory provides the value type; <paramref name="keyFactory"/> provides the key type. |
| 36 | + /// </summary> |
| 37 | + internal virtual IDictionary? CreateDictionary (JavaPeerContainerFactory keyFactory, IntPtr handle, JniHandleOwnership transfer) |
| 38 | + => null; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Visitor callback invoked by the value factory's <see cref="CreateDictionary"/>. |
| 42 | + /// Override in <see cref="JavaPeerContainerFactory{T}"/> to provide both type parameters. |
| 43 | + /// </summary> |
| 44 | + internal virtual IDictionary? CreateDictionaryWithValueFactory<TValue> ( |
| 45 | + JavaPeerContainerFactory<TValue> valueFactory, IntPtr handle, JniHandleOwnership transfer) |
| 46 | + where TValue : class, IJavaPeerable |
| 47 | + => null; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Creates a <see cref="JavaPeerContainerFactory{T}"/> singleton for the specified type. |
| 51 | + /// </summary> |
| 52 | + public static JavaPeerContainerFactory Create< |
| 53 | + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] |
| 54 | + T |
| 55 | + > () where T : class, IJavaPeerable |
| 56 | + => JavaPeerContainerFactory<T>.Instance; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Typed container factory. All creation uses direct <c>new</c> expressions — fully AOT-safe. |
| 61 | + /// </summary> |
| 62 | + /// <typeparam name="T">The Java peer element type.</typeparam> |
| 63 | + public sealed class JavaPeerContainerFactory< |
| 64 | + // TODO (https://github.com/dotnet/android/issues/10794): Remove this DAM annotation — it preserves too much reflection metadata on all types in the typemap. |
| 65 | + [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] |
| 66 | + T |
| 67 | + > : JavaPeerContainerFactory |
| 68 | + where T : class, IJavaPeerable |
| 69 | + { |
| 70 | + internal static readonly JavaPeerContainerFactory<T> Instance = new (); |
| 71 | + |
| 72 | + JavaPeerContainerFactory () { } |
| 73 | + |
| 74 | + // TODO (https://github.com/dotnet/android/issues/10794): This might cause unnecessary code bloat for NativeAOT. Revisit |
| 75 | + // how we use this API and instead use a differnet approach that uses AOT-safe `Array.CreateInstanceFromArrayType` |
| 76 | + // with statically provided array types based on a statically known array type. |
| 77 | + internal override Array CreateArray (int length, int rank) => rank switch { |
| 78 | + 1 => new T [length], |
| 79 | + 2 => new T [length][], |
| 80 | + 3 => new T [length][][], |
| 81 | + _ when rank >= 0 => CreateHigherRankArray (length, rank), |
| 82 | + _ => throw new ArgumentOutOfRangeException (nameof (rank), rank, "Rank must be non-negative."), |
| 83 | + }; |
| 84 | + |
| 85 | + static Array CreateHigherRankArray (int length, int rank) |
| 86 | + { |
| 87 | + if (!RuntimeFeature.IsDynamicCodeSupported) { |
| 88 | + throw new NotSupportedException ($"Cannot create array of rank {rank} because dynamic code is not supported."); |
| 89 | + } |
| 90 | + |
| 91 | + var arrayType = typeof (T); |
| 92 | + for (int i = 0; i < rank; i++) { |
| 93 | + arrayType = arrayType.MakeArrayType (); |
| 94 | + } |
| 95 | + |
| 96 | + return Array.CreateInstanceFromArrayType (arrayType, length); |
| 97 | + } |
| 98 | + |
| 99 | + internal override IList CreateList (IntPtr handle, JniHandleOwnership transfer) |
| 100 | + => new Android.Runtime.JavaList<T> (handle, transfer); |
| 101 | + |
| 102 | + internal override ICollection CreateCollection (IntPtr handle, JniHandleOwnership transfer) |
| 103 | + => new Android.Runtime.JavaCollection<T> (handle, transfer); |
| 104 | + |
| 105 | + internal override IDictionary? CreateDictionary (JavaPeerContainerFactory keyFactory, IntPtr handle, JniHandleOwnership transfer) |
| 106 | + => keyFactory.CreateDictionaryWithValueFactory (this, handle, transfer); |
| 107 | + |
| 108 | + #pragma warning disable IL2091 // DynamicallyAccessedMembers on base method type parameter cannot be repeated on override in C# |
| 109 | + internal override IDictionary? CreateDictionaryWithValueFactory<TValue> ( |
| 110 | + JavaPeerContainerFactory<TValue> valueFactory, IntPtr handle, JniHandleOwnership transfer) |
| 111 | + => new Android.Runtime.JavaDictionary<T, TValue> (handle, transfer); |
| 112 | + #pragma warning restore IL2091 |
| 113 | + } |
| 114 | +} |
0 commit comments