@@ -45,13 +45,15 @@ public static void Fixup (IList<ClassFile> classes)
4545 FixupJavaMethods ( c . Methods ) ;
4646
4747 foreach ( var met in metadata . Functions )
48- FixupFunction ( FindJavaMethod ( class_metadata , met , c ) , met , class_metadata ) ;
48+ FixupFunction ( FindJavaMethod ( metadata , met , c ) , met , class_metadata ) ;
4949
5050 foreach ( var prop in metadata . Properties ) {
51- var getter = FindJavaPropertyGetter ( class_metadata , prop , c ) ;
52- var setter = FindJavaPropertySetter ( class_metadata , prop , c ) ;
51+ var getter = FindJavaPropertyGetter ( metadata , prop , c ) ;
52+ var setter = FindJavaPropertySetter ( metadata , prop , c ) ;
5353
5454 FixupProperty ( getter , setter , prop ) ;
55+
56+ FixupField ( FindJavaFieldProperty ( metadata , prop , c ) , prop ) ;
5557 }
5658
5759 } catch ( Exception ex ) {
@@ -96,7 +98,6 @@ static void FixupConstructor (MethodInfo method, KotlinConstructor metadata)
9698 Log . Debug ( $ "Kotlin: Hiding internal constructor { method . DeclaringType ? . ThisClass . Name . Value } - { metadata . GetSignature ( ) } ") ;
9799 method . AccessFlags = MethodAccessFlags . Private ;
98100 }
99-
100101 }
101102
102103 static void FixupFunction ( MethodInfo method , KotlinFunction metadata , KotlinClass kotlinClass )
@@ -111,18 +112,24 @@ static void FixupFunction (MethodInfo method, KotlinFunction metadata, KotlinCla
111112 return ;
112113 }
113114
114- // Kotlin provides actual parameter names
115115 var java_parameters = method . GetFilteredParameters ( ) ;
116116
117117 for ( var i = 0 ; i < java_parameters . Length ; i ++ ) {
118118 var java_p = java_parameters [ i ] ;
119119 var kotlin_p = metadata . ValueParameters [ i ] ;
120120
121+ // Kotlin provides actual parameter names
121122 if ( TypesMatch ( java_p . Type , kotlin_p . Type , kotlinClass ) && java_p . IsUnnamedParameter ( ) && ! kotlin_p . IsUnnamedParameter ( ) ) {
122123 Log . Debug ( $ "Kotlin: Renaming parameter { method . DeclaringType ? . ThisClass . Name . Value } - { method . Name } - { java_p . Name } -> { kotlin_p . Name } ") ;
123124 java_p . Name = kotlin_p . Name ;
124125 }
126+
127+ // Handle erasure of Kotlin unsigned types
128+ java_p . KotlinType = GetKotlinType ( java_p . Type . TypeSignature , kotlin_p . Type . ClassName ) ;
125129 }
130+
131+ // Handle erasure of Kotlin unsigned types
132+ method . KotlinReturnType = GetKotlinType ( method . ReturnType . TypeSignature , metadata . ReturnType . ClassName ) ;
126133 }
127134
128135 static void FixupExtensionMethod ( MethodInfo method )
@@ -158,16 +165,32 @@ static void FixupProperty (MethodInfo getter, MethodInfo setter, KotlinProperty
158165 return ;
159166 }
160167
168+ // Handle erasure of Kotlin unsigned types
169+ if ( getter != null )
170+ getter . KotlinReturnType = GetKotlinType ( getter . ReturnType . TypeSignature , metadata . ReturnType . ClassName ) ;
171+
161172 if ( setter != null ) {
162173 var setter_parameter = setter . GetParameters ( ) . First ( ) ;
163174
164- if ( setter_parameter . IsUnnamedParameter ( ) ) {
175+ if ( setter_parameter . IsUnnamedParameter ( ) || setter_parameter . Name == "<set-?>" ) {
165176 Log . Debug ( $ "Kotlin: Renaming setter parameter { setter . DeclaringType ? . ThisClass . Name . Value } - { setter . Name } - { setter_parameter . Name } -> value") ;
166177 setter_parameter . Name = "value" ;
167178 }
179+
180+ // Handle erasure of Kotlin unsigned types
181+ setter_parameter . KotlinType = GetKotlinType ( setter_parameter . Type . TypeSignature , metadata . ReturnType . ClassName ) ;
168182 }
169183 }
170184
185+ static void FixupField ( FieldInfo field , KotlinProperty metadata )
186+ {
187+ if ( field is null )
188+ return ;
189+
190+ // Handle erasure of Kotlin unsigned types
191+ field . KotlinType = GetKotlinType ( field . Descriptor , metadata . ReturnType . ClassName ) ;
192+ }
193+
171194 static MethodInfo FindJavaConstructor ( KotlinClass kotlinClass , KotlinConstructor constructor , ClassFile klass )
172195 {
173196 var all_constructors = klass . Methods . Where ( method => method . Name == "<init>" || method . Name == "<clinit>" ) ;
@@ -181,16 +204,16 @@ static MethodInfo FindJavaConstructor (KotlinClass kotlinClass, KotlinConstructo
181204 return null ;
182205 }
183206
184- static MethodInfo FindJavaMethod ( KotlinClass kotlinClass , KotlinFunction function , ClassFile klass )
207+ static MethodInfo FindJavaMethod ( KotlinFile kotlinFile , KotlinFunction function , ClassFile klass )
185208 {
186- var possible_methods = klass . Methods . Where ( method => method . GetMethodNameWithoutSuffix ( ) == function . Name &&
209+ var possible_methods = klass . Methods . Where ( method => method . Name == function . JvmName &&
187210 method . GetFilteredParameters ( ) . Length == function . ValueParameters . Count ) ;
188211
189212 foreach ( var method in possible_methods ) {
190- if ( ! TypesMatch ( method . ReturnType , function . ReturnType , kotlinClass ) )
213+ if ( ! TypesMatch ( method . ReturnType , function . ReturnType , kotlinFile ) )
191214 continue ;
192215
193- if ( ! ParametersMatch ( kotlinClass , method , function . ValueParameters ) )
216+ if ( ! ParametersMatch ( kotlinFile , method , function . ValueParameters ) )
194217 continue ;
195218
196219 return method ;
@@ -199,7 +222,15 @@ static MethodInfo FindJavaMethod (KotlinClass kotlinClass, KotlinFunction functi
199222 return null ;
200223 }
201224
202- static MethodInfo FindJavaPropertyGetter ( KotlinClass kotlinClass , KotlinProperty property , ClassFile klass )
225+ static FieldInfo FindJavaFieldProperty ( KotlinFile kotlinClass , KotlinProperty property , ClassFile klass )
226+ {
227+ var possible_methods = klass . Fields . Where ( field => field . Name == property . Name &&
228+ TypesMatch ( new TypeInfo ( field . Descriptor , field . Descriptor ) , property . ReturnType , kotlinClass ) ) ;
229+
230+ return possible_methods . FirstOrDefault ( ) ;
231+ }
232+
233+ static MethodInfo FindJavaPropertyGetter ( KotlinFile kotlinClass , KotlinProperty property , ClassFile klass )
203234 {
204235 var possible_methods = klass . Methods . Where ( method => ( string . Compare ( method . GetMethodNameWithoutSuffix ( ) , $ "get{ property . Name } ", true ) == 0 ||
205236 string . Compare ( method . GetMethodNameWithoutSuffix ( ) , property . Name , true ) == 0 ) &&
@@ -209,7 +240,7 @@ static MethodInfo FindJavaPropertyGetter (KotlinClass kotlinClass, KotlinPropert
209240 return possible_methods . FirstOrDefault ( ) ;
210241 }
211242
212- static MethodInfo FindJavaPropertySetter ( KotlinClass kotlinClass , KotlinProperty property , ClassFile klass )
243+ static MethodInfo FindJavaPropertySetter ( KotlinFile kotlinClass , KotlinProperty property , ClassFile klass )
213244 {
214245 var possible_methods = klass . Methods . Where ( method => string . Compare ( method . GetMethodNameWithoutSuffix ( ) , $ "set{ property . Name } ", true ) == 0 &&
215246 property . ReturnType != null &&
@@ -219,7 +250,7 @@ static MethodInfo FindJavaPropertySetter (KotlinClass kotlinClass, KotlinPropert
219250 return possible_methods . FirstOrDefault ( ) ;
220251 }
221252
222- static bool ParametersMatch ( KotlinClass kotlinClass , MethodInfo method , List < KotlinValueParameter > kotlinParameters )
253+ static bool ParametersMatch ( KotlinFile kotlinClass , MethodInfo method , List < KotlinValueParameter > kotlinParameters )
223254 {
224255 var java_parameters = method . GetFilteredParameters ( ) ;
225256
@@ -237,13 +268,13 @@ static bool ParametersMatch (KotlinClass kotlinClass, MethodInfo method, List<Ko
237268 return true ;
238269 }
239270
240- static bool TypesMatch ( TypeInfo javaType , KotlinType kotlinType , KotlinClass kotlinClass )
271+ static bool TypesMatch ( TypeInfo javaType , KotlinType kotlinType , KotlinFile kotlinFile )
241272 {
242273 // Generic type
243274 if ( ! string . IsNullOrWhiteSpace ( kotlinType . TypeParameterName ) && $ "T{ kotlinType . TypeParameterName } ;" == javaType . TypeSignature )
244275 return true ;
245276
246- if ( javaType . BinaryName == KotlinUtilities . ConvertKotlinTypeSignature ( kotlinType , kotlinClass ) )
277+ if ( javaType . BinaryName == KotlinUtilities . ConvertKotlinTypeSignature ( kotlinType , kotlinFile ) )
247278 return true ;
248279
249280 // Could be a generic type erasure
@@ -253,17 +284,32 @@ static bool TypesMatch (TypeInfo javaType, KotlinType kotlinType, KotlinClass ko
253284 // Sometimes Kotlin keeps its native types rather than converting them to Java native types
254285 // ie: "Lkotlin/UShort;" instead of "S"
255286 if ( javaType . BinaryName . StartsWith ( "L" , StringComparison . Ordinal ) && javaType . BinaryName . EndsWith ( ";" , StringComparison . Ordinal ) ) {
256- if ( KotlinUtilities . ConvertKotlinClassToJava ( javaType . BinaryName . Substring ( 1 , javaType . BinaryName . Length - 2 ) ) == KotlinUtilities . ConvertKotlinTypeSignature ( kotlinType , kotlinClass ) )
287+ if ( KotlinUtilities . ConvertKotlinClassToJava ( javaType . BinaryName . Substring ( 1 , javaType . BinaryName . Length - 2 ) ) == KotlinUtilities . ConvertKotlinTypeSignature ( kotlinType , kotlinFile ) )
257288 return true ;
258289 }
259290
260291 // Same for some arrays
261292 if ( javaType . BinaryName . StartsWith ( "[L" , StringComparison . Ordinal ) && javaType . BinaryName . EndsWith ( ";" , StringComparison . Ordinal ) ) {
262- if ( "[" + KotlinUtilities . ConvertKotlinClassToJava ( javaType . BinaryName . Substring ( 2 , javaType . BinaryName . Length - 3 ) ) == KotlinUtilities . ConvertKotlinTypeSignature ( kotlinType , kotlinClass ) )
293+ if ( "[" + KotlinUtilities . ConvertKotlinClassToJava ( javaType . BinaryName . Substring ( 2 , javaType . BinaryName . Length - 3 ) ) == KotlinUtilities . ConvertKotlinTypeSignature ( kotlinType , kotlinFile ) )
263294 return true ;
264295 }
265296
266297 return false ;
267298 }
299+
300+ static string GetKotlinType ( string jvmType , string kotlinClass )
301+ {
302+ // Handle erasure of Kotlin unsigned types
303+ if ( jvmType == "I" && kotlinClass == "kotlin/UInt;" )
304+ return "uint" ;
305+ if ( jvmType == "S" && kotlinClass == "kotlin/UShort;" )
306+ return "ushort" ;
307+ if ( jvmType == "J" && kotlinClass == "kotlin/ULong;" )
308+ return "ulong" ;
309+ if ( jvmType == "B" && kotlinClass == "kotlin/UByte;" )
310+ return "ubyte" ;
311+
312+ return null ;
313+ }
268314 }
269315}
0 commit comments