@@ -899,34 +899,47 @@ open class KotlinUsesExtractor(
899899 return id
900900 }
901901
902- fun kotlinFunctionToJavaEquivalent (f : IrFunction ) =
903- f.parent.let {
904- when (it) {
905- is IrClass ->
906- getJavaEquivalentClass(it)?.let { javaClass ->
907- if (javaClass != it)
908- javaClass.declarations.find { decl ->
909- decl is IrFunction && decl.name == f.name && decl.valueParameters.size == f.valueParameters.size
910- } as IrFunction
911- else
902+ fun kotlinFunctionToJavaEquivalent (f : IrFunction , noReplace : Boolean ) =
903+ if (noReplace)
904+ f
905+ else
906+ f.parentClassOrNull?.let { parentClass ->
907+ getJavaEquivalentClass(parentClass)?.let { javaClass ->
908+ if (javaClass != parentClass)
909+ // Look for an exact type match...
910+ javaClass.declarations.find { decl ->
911+ decl is IrFunction &&
912+ decl.name == f.name &&
913+ decl.valueParameters.size == f.valueParameters.size &&
914+ decl.valueParameters.zip(f.valueParameters).all { p -> p.first.type == p.second.type }
915+ } ? :
916+ // Or if there is none, look for the only viable overload
917+ javaClass.declarations.singleOrNull { decl ->
918+ decl is IrFunction &&
919+ decl.name == f.name &&
920+ decl.valueParameters.size == f.valueParameters.size
921+ } ? :
922+ run {
923+ logger.warn(" Couldn't find a Java equivalent function to ${f.name} " )
912924 null
913- }
914- else -> null
915- } ? : f
916- }
925+ }
926+ else
927+ null
928+ }
929+ } as IrFunction ? ? : f
917930
918- fun <T : DbCallable > useFunction (f : IrFunction , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? = null): Label <out T > {
931+ fun <T : DbCallable > useFunction (f : IrFunction , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? = null, noReplace : Boolean = false ): Label <out T > {
919932 if (f.isLocalFunction()) {
920933 val ids = getLocallyVisibleFunctionLabels(f)
921934 return ids.function.cast<T >()
922935 } else {
923- val realFunction = kotlinFunctionToJavaEquivalent(f)
936+ val realFunction = kotlinFunctionToJavaEquivalent(f, noReplace )
924937 return useFunctionCommon<T >(realFunction, getFunctionLabel(realFunction, classTypeArgsIncludingOuterClasses))
925938 }
926939 }
927940
928- fun <T : DbCallable > useFunction (f : IrFunction , parentId : Label <out DbElement >, classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? ) =
929- kotlinFunctionToJavaEquivalent(f).let {
941+ fun <T : DbCallable > useFunction (f : IrFunction , parentId : Label <out DbElement >, classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? , noReplace : Boolean = false ) =
942+ kotlinFunctionToJavaEquivalent(f, noReplace ).let {
930943 useFunctionCommon<T >(it, getFunctionLabel(it, parentId, classTypeArgsIncludingOuterClasses))
931944 }
932945
0 commit comments