Make JetTypeMapper aware of SAM constructor arguments

This obsoletes 'replaceName' workaround that was present in ClosureCodegen but
missing in other crucial call sites of mapSignature, e.g. generation of
EnclosingMethod info

 #KT-6691 Fixed
This commit is contained in:
Alexander Udalov
2015-02-06 15:50:14 +03:00
parent ca2cfb9859
commit e7a744b315
5 changed files with 50 additions and 11 deletions
@@ -161,8 +161,7 @@ public class ClosureCodegen extends MemberCodegen<JetElement> {
erasedInterfaceFunction = samType.getAbstractMethod().getOriginal();
}
JvmMethodSignature jvmMethodSignature =
typeMapper.mapSignature(funDescriptor).replaceName(erasedInterfaceFunction.getName().toString());
JvmMethodSignature jvmMethodSignature = typeMapper.mapSignature(funDescriptor);
generateBridge(typeMapper.mapSignature(erasedInterfaceFunction).getAsmMethod(), jvmMethodSignature.getAsmMethod());
functionCodegen.generateMethod(OtherOrigin(element, funDescriptor), jvmMethodSignature, funDescriptor, strategy);
@@ -38,7 +38,10 @@ import org.jetbrains.kotlin.load.kotlin.nativeDeclarations.NativeDeclarationsPac
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.FqNameUnsafe;
import org.jetbrains.kotlin.name.SpecialNames;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.psi.JetFunctionLiteral;
import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
@@ -584,7 +587,7 @@ public class JetTypeMapper {
}
@NotNull
private static String mapFunctionName(@NotNull FunctionDescriptor descriptor) {
private String mapFunctionName(@NotNull FunctionDescriptor descriptor) {
String platformName = getPlatformName(descriptor);
if (platformName != null) return platformName;
@@ -601,7 +604,21 @@ public class JetTypeMapper {
return PropertyCodegen.setterName(property.getName());
}
}
else if (isLocalNamedFun(descriptor) || descriptor instanceof AnonymousFunctionDescriptor) {
else if (isLocalNamedFun(descriptor)) {
return "invoke";
}
else if (descriptor instanceof AnonymousFunctionDescriptor) {
PsiElement element = DescriptorToSourceUtils.callableDescriptorToDeclaration(descriptor);
if (element instanceof JetFunctionLiteral) {
PsiElement expression = element.getParent();
if (expression instanceof JetFunctionLiteralExpression) {
SamType samType = bindingContext.get(SAM_VALUE, (JetExpression) expression);
if (samType != null) {
return samType.getAbstractMethod().getName().asString();
}
}
}
return "invoke";
}
else {
@@ -58,13 +58,6 @@ public class JvmMethodSignature {
return asmMethod.getReturnType();
}
@NotNull
public JvmMethodSignature replaceName(@NotNull String newName) {
return newName.equals(asmMethod.getName()) ?
this :
new JvmMethodSignature(new Method(newName, asmMethod.getDescriptor()), genericsSignature, valueParameters);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -0,0 +1,24 @@
var lambda = {}
class A {
val prop = Runnable {
lambda = { println("") }
}
}
fun box(): String {
A().prop.run()
val javaClass = lambda.javaClass
val enclosingMethod = javaClass.getEnclosingMethod()
if (enclosingMethod?.getName() != "run") return "method: $enclosingMethod"
val enclosingClass = javaClass.getEnclosingClass()!!.getName()
if (enclosingClass != "A\$prop\$1") return "enclosing class: $enclosingClass"
val declaringClass = javaClass.getDeclaringClass()
if (declaringClass != null) return "anonymous function has a declaring class: $declaringClass"
return "OK"
}
@@ -2492,6 +2492,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("kt6691_lambdaInSamConstructor.kt")
public void testKt6691_lambdaInSamConstructor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/kt6691_lambdaInSamConstructor.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("lambdaInConstructor.kt")
public void testLambdaInConstructor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/reflection/enclosing/lambda/lambdaInConstructor.kt");