@@ -8,7 +8,7 @@ namespace Xamarin.Android.Tools.Bytecode
88{
99 public static class KotlinUtilities
1010 {
11- public static string ConvertKotlinTypeSignature ( KotlinType ? type , KotlinFile ? metadata = null )
11+ public static string ConvertKotlinTypeSignature ( KotlinType ? type , KotlinFile ? metadata = null , bool convertUnsignedToPrimitive = true )
1212 {
1313 if ( type is null )
1414 return string . Empty ;
@@ -27,15 +27,15 @@ public static string ConvertKotlinTypeSignature (KotlinType? type, KotlinFile? m
2727 return "Ljava/lang/Object;" ;
2828 }
2929
30- var result = ConvertKotlinClassToJava ( class_name ) ;
30+ var result = ConvertKotlinClassToJava ( class_name , convertUnsignedToPrimitive ) ;
3131
3232 if ( result == "[" )
33- result += ConvertKotlinTypeSignature ( type . Arguments ? . FirstOrDefault ( ) ? . Type ) ;
33+ result += ConvertKotlinTypeSignature ( type . Arguments ? . FirstOrDefault ( ) ? . Type , null , convertUnsignedToPrimitive ) ;
3434
3535 return result ;
3636 }
3737
38- public static string ConvertKotlinClassToJava ( string ? className )
38+ public static string ConvertKotlinClassToJava ( string ? className , bool convertUnsignedToPrimitive = true )
3939 {
4040 if ( className == null || string . IsNullOrWhiteSpace ( className ) )
4141 return string . Empty ;
@@ -45,6 +45,9 @@ public static string ConvertKotlinClassToJava (string? className)
4545 if ( type_map . TryGetValue ( className . TrimEnd ( ';' ) , out var result ) )
4646 return result ;
4747
48+ if ( convertUnsignedToPrimitive && unsigned_type_map . TryGetValue ( className . TrimEnd ( ';' ) , out var result2 ) )
49+ return result2 ;
50+
4851 return "L" + className ;
4952 }
5053
@@ -92,6 +95,14 @@ public static bool IsDefaultConstructorMarker (this MethodInfo method)
9295 parameters [ parameters . Length - 1 ] . Type . TypeSignature == "Lkotlin/jvm/internal/DefaultConstructorMarker;" ;
9396 }
9497
98+ // Sometimes the Kotlin provided JvmSignature is null (or unhelpful), so we need to construct one ourselves
99+ public static string ConstructJvmSignature ( this KotlinFunction function )
100+ {
101+ // The receiver type (if specified) is a "hidden" parameter passed at the beginning
102+ // of the Java parameter list, so we include it so the Signature/Descriptors match.
103+ return $ "({ function . ReceiverType ? . GetSignature ( false ) } { string . Concat ( function . ValueParameters ? . Select ( p => p . Type ? . GetSignature ( false ) ) ?? Enumerable . Empty < string > ( ) ) } ){ function . ReturnType ? . GetSignature ( false ) } ";
104+ }
105+
95106 internal static List < TResult > ? ToList < TSource , TResult > ( this IEnumerable < TSource > ? self , JvmNameResolver resolver , Func < TSource , JvmNameResolver , TResult ? > creator )
96107 where TResult : class
97108 {
@@ -114,37 +125,42 @@ public static bool IsDefaultConstructorMarker (this MethodInfo method)
114125
115126 public static bool IsUnnamedParameter ( this ParameterInfo parameter ) => parameter . Name . Length > 1 && parameter . Name . StartsWith ( "p" , StringComparison . Ordinal ) && int . TryParse ( parameter . Name . Substring ( 1 ) , out var _ ) ;
116127
128+ public static bool IsCompilerNamed ( this ParameterInfo parameter ) => parameter . Name . Length > 0 && parameter . Name . StartsWith ( "$" , StringComparison . Ordinal ) ;
129+
117130 public static bool IsUnnamedParameter ( this KotlinValueParameter parameter ) => parameter . Name ? . Length > 1 &&
118131 parameter . Name . StartsWith ( "p" , StringComparison . Ordinal ) &&
119132 int . TryParse ( parameter . Name . Substring ( 1 ) , out var _ ) ;
120133
134+ static Dictionary < string , string > unsigned_type_map = new Dictionary < string , string > {
135+ { "kotlin/UInt" , "I" } ,
136+ { "kotlin/ULong" , "J" } ,
137+ { "kotlin/UShort" , "S" } ,
138+ { "kotlin/UByte" , "B" } ,
139+ { "kotlin/UIntArray" , "[I" } ,
140+ { "kotlin/ULongArray" , "[J" } ,
141+ { "kotlin/UShortArray" , "[S" } ,
142+ { "kotlin/UByteArray" , "[B" } ,
143+ } ;
144+
121145 static Dictionary < string , string > type_map = new Dictionary < string , string > {
122146 { "kotlin/Int" , "I" } ,
123- { "kotlin/UInt" , "I" } ,
124147 { "kotlin/Double" , "D" } ,
125148 { "kotlin/Char" , "C" } ,
126149 { "kotlin/Long" , "J" } ,
127- { "kotlin/ULong" , "J" } ,
128150 { "kotlin/Float" , "F" } ,
129151 { "kotlin/Short" , "S" } ,
130- { "kotlin/UShort" , "S" } ,
131152 { "kotlin/Byte" , "B" } ,
132- { "kotlin/UByte" , "B" } ,
133153 { "kotlin/Boolean" , "Z" } ,
134154 { "kotlin/Unit" , "V" } ,
135155
136156 { "kotlin/Array" , "[" } ,
137157 { "kotlin/IntArray" , "[I" } ,
138- { "kotlin/UIntArray" , "[I" } ,
139158 { "kotlin/DoubleArray" , "[D" } ,
140159 { "kotlin/CharArray" , "[C" } ,
141160 { "kotlin/LongArray" , "[J" } ,
142- { "kotlin/ULongArray" , "[J" } ,
143161 { "kotlin/FloatArray" , "[F" } ,
144162 { "kotlin/ShortArray" , "[S" } ,
145- { "kotlin/UShortArray" , "[S" } ,
146163 { "kotlin/ByteArray" , "[B" } ,
147- { "kotlin/UByteArray" , "[B" } ,
148164 { "kotlin/BooleanArray" , "[Z" } ,
149165
150166 { "kotlin/Any" , "Ljava/lang/Object;" } ,
0 commit comments