@@ -27,20 +27,22 @@ public static partial class JNIEnv {
2727 static Array ArrayCreateInstance ( Type elementType , int length )
2828 {
2929 if ( RuntimeFeature . TrimmableTypeMap ) {
30- if ( System . Runtime . CompilerServices . RuntimeFeature . IsDynamicCodeSupported ) {
30+ if ( RuntimeFeature . IsCoreClrRuntime ) {
3131 // CoreCLR runtime type loader can construct any T[] dynamically.
3232 // IsDynamicCodeSupported is a [FeatureGuard] so this branch is
3333 // dead-coded under PublishAot.
3434 return Array . CreateInstance ( elementType , length ) ;
3535 }
3636
37- // NativeAOT: resolve via per-rank typemap + Array.CreateInstanceFromArrayType.
38- if ( TrimmableTypeMap . Instance . TryGetArrayType ( elementType , out var arrayType ) ) {
39- return Array . CreateInstanceFromArrayType ( arrayType , length ) ;
37+ if ( RuntimeFeature . IsNativeAotRuntime ) {
38+ // NativeAOT: resolve via per-rank typemap + generated array proxy.
39+ if ( TrimmableTypeMap . Instance . TryGetArrayProxy ( elementType , additionalRank : 1 , out var arrayProxy ) ) {
40+ return arrayProxy . CreateManagedArray ( length ) ;
41+ }
4042 }
4143
4244 throw new NotSupportedException (
43- $ "No TrimmableTypeMap array entry for element type '{ elementType } '. " +
45+ $ "No TrimmableTypeMap array proxy entry for element type '{ elementType } '. " +
4446 $ "Array lookups use the element type within the per-rank __ArrayMapRank{ GetArrayRank ( elementType ) } typemap group; " +
4547 $ "ensure the mapping is emitted for that rank (for example by increasing _AndroidTrimmableTypeMapMaxArrayRank) or report an issue.") ;
4648 }
@@ -64,13 +66,6 @@ static int GetArrayRank (Type elementType)
6466 return rank ;
6567 }
6668
67- static Type MakeArrayType ( Type type ) =>
68- // FIXME: https://github.com/xamarin/xamarin-android/issues/8724
69- // IL3050 disabled in source: if someone uses NativeAOT, they will get the warning.
70- #pragma warning disable IL3050
71- type . MakeArrayType ( ) ;
72- #pragma warning restore IL3050
73-
7469 internal static IntPtr IdentityHash ( IntPtr v )
7570 {
7671 return JniEnvironment . References . GetIdentityHashCode ( new JniObjectReference ( v ) ) ;
@@ -309,10 +304,7 @@ public static IntPtr FindClass (System.Type type)
309304 }
310305 sig = sig . AddArrayRank ( rank ) ;
311306
312- JniObjectReference local_ref = JniEnvironment . Types . FindClass ( sig . Name ) ;
313- IntPtr global_ref = local_ref . NewGlobalRef ( ) . Handle ;
314- JniObjectReference . Dispose ( ref local_ref ) ;
315- return global_ref ;
307+ return FindClass ( sig . Name ) ;
316308 } catch ( Java . Lang . Throwable e ) {
317309 if ( ! ( ( e is Java . Lang . NoClassDefFoundError ) || ( e is Java . Lang . ClassNotFoundException ) ) )
318310 throw ;
@@ -577,9 +569,9 @@ public static unsafe IntPtr NewString (char[]? text, int length)
577569 return JniEnvironment . Strings . NewString ( s , length ) . Handle ;
578570 }
579571
580- static void AssertCompatibleArrayTypes ( Type sourceType , IntPtr destArray )
572+ static void AssertCompatibleArrayTypes ( Type srcElementType , IntPtr destArray )
581573 {
582- IntPtr grefSource = FindClass ( sourceType ) ;
574+ IntPtr grefSource = FindArrayClassByElementType ( srcElementType ) ;
583575 IntPtr lrefDest = GetObjectClass ( destArray ) ;
584576 try {
585577 if ( ! IsAssignableFrom ( grefSource , lrefDest ) ) {
@@ -592,9 +584,9 @@ static void AssertCompatibleArrayTypes (Type sourceType, IntPtr destArray)
592584 }
593585 }
594586
595- static void AssertCompatibleArrayTypes ( IntPtr sourceArray , Type destType )
587+ static void AssertCompatibleArrayTypes ( IntPtr sourceArray , Type destElementType )
596588 {
597- IntPtr grefDest = FindClass ( destType ) ;
589+ IntPtr grefDest = FindArrayClassByElementType ( destElementType ) ;
598590 IntPtr lrefSource = GetObjectClass ( sourceArray ) ;
599591 try {
600592 if ( ! IsAssignableFrom ( lrefSource , grefDest ) ) {
@@ -607,12 +599,19 @@ static void AssertCompatibleArrayTypes (IntPtr sourceArray, Type destType)
607599 }
608600 }
609601
602+ static IntPtr FindArrayClassByElementType ( Type elementType )
603+ {
604+ int rank = JavaNativeTypeManager . GetArrayInfo ( elementType , out elementType ) + 1 ;
605+ var typeSignature = JniRuntime . CurrentRuntime . TypeManager . GetTypeSignature ( elementType ) . AddArrayRank ( rank ) ;
606+ return FindClass ( typeSignature . Name ) ;
607+ }
608+
610609 public static void CopyArray ( IntPtr src , bool [ ] dest )
611610 {
612611 if ( dest == null )
613612 throw new ArgumentNullException ( "dest" ) ;
614613
615- AssertCompatibleArrayTypes ( src , typeof ( bool [ ] ) ) ;
614+ AssertCompatibleArrayTypes ( src , destElementType : typeof ( bool ) ) ;
616615
617616 _GetBooleanArrayRegion ( src , 0 , dest . Length , dest ) ;
618617 }
@@ -804,7 +803,7 @@ public static void CopyArray (IntPtr src, Array dest, Type? elementType = null)
804803 throw new ArgumentNullException ( "dest" ) ;
805804
806805 if ( elementType != null && elementType . IsValueType )
807- AssertCompatibleArrayTypes ( src , MakeArrayType ( elementType ) ) ;
806+ AssertCompatibleArrayTypes ( src , destElementType : elementType ) ;
808807
809808 if ( elementType != null && elementType . IsArray ) {
810809 for ( int i = 0 ; i < dest . Length ; ++ i ) {
@@ -840,7 +839,7 @@ public static void CopyArray<T> (IntPtr src, T[] dest)
840839 throw new ArgumentNullException ( "dest" ) ;
841840
842841 if ( typeof ( T ) . IsValueType )
843- AssertCompatibleArrayTypes ( src , typeof ( T [ ] ) ) ;
842+ AssertCompatibleArrayTypes ( src , destElementType : typeof ( T ) ) ;
844843
845844 if ( typeof ( T ) . IsArray ) {
846845 CopyArray ( src , dest , typeof ( T ) ) ;
@@ -858,7 +857,7 @@ public static unsafe void CopyArray (bool[] src, IntPtr dest)
858857 if ( src == null )
859858 throw new ArgumentNullException ( "src" ) ;
860859
861- AssertCompatibleArrayTypes ( typeof ( bool [ ] ) , dest ) ;
860+ AssertCompatibleArrayTypes ( srcElementType : typeof ( bool ) , dest ) ;
862861
863862 fixed ( bool * p = src )
864863 JniEnvironment . Arrays . SetBooleanArrayRegion ( new JniObjectReference ( dest ) , 0 , src . Length , p ) ;
@@ -947,7 +946,7 @@ public static void CopyArray (Array source, Type elementType, IntPtr dest)
947946 throw new ArgumentNullException ( "elementType" ) ;
948947
949948 if ( elementType . IsValueType )
950- AssertCompatibleArrayTypes ( MakeArrayType ( elementType ) , dest ) ;
949+ AssertCompatibleArrayTypes ( srcElementType : elementType , dest ) ;
951950
952951 Action < Array , IntPtr > converter = GetConverter ( CopyManagedToNativeArray , elementType , dest ) ;
953952
@@ -1072,7 +1071,7 @@ public static void CopyArray<T> (T[] src, IntPtr dest)
10721071 return null ;
10731072
10741073 if ( element_type != null && element_type . IsValueType )
1075- AssertCompatibleArrayTypes ( array_ptr , MakeArrayType ( element_type ) ) ;
1074+ AssertCompatibleArrayTypes ( array_ptr , destElementType : element_type ) ;
10761075
10771076 int cnt = _GetArrayLength ( array_ptr ) ;
10781077
@@ -1119,7 +1118,7 @@ static int _GetArrayLength (IntPtr array_ptr)
11191118 return null ;
11201119
11211120 if ( typeof ( T ) . IsValueType )
1122- AssertCompatibleArrayTypes ( array_ptr , typeof ( T [ ] ) ) ;
1121+ AssertCompatibleArrayTypes ( array_ptr , destElementType : typeof ( T ) ) ;
11231122
11241123 int cnt = _GetArrayLength ( array_ptr ) ;
11251124 T [ ] ret = new T [ cnt ] ;
0 commit comments