Supported calls of SAM adapters for constructors.

This commit is contained in:
Evgeny Gerashchenko
2013-06-07 18:15:43 +04:00
parent 5e2c3fcb50
commit 4980dacd33
4 changed files with 30 additions and 8 deletions
@@ -1891,17 +1891,17 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
@Nullable
private SimpleFunctionDescriptor getOriginalIfSamAdapter(@NotNull CallableDescriptor fun) {
if (!(fun instanceof SimpleFunctionDescriptor)) {
private FunctionDescriptor getOriginalIfSamAdapter(@NotNull CallableDescriptor fun) {
if (!(fun instanceof FunctionDescriptor)) {
return null;
}
SimpleFunctionDescriptor original = ((SimpleFunctionDescriptor) fun).getOriginal();
FunctionDescriptor original = ((FunctionDescriptor) fun).getOriginal();
if (original.getKind() == CallableMemberDescriptor.Kind.SYNTHESIZED) {
return (SimpleFunctionDescriptor) bindingContext.get(SAM_ADAPTER_FUNCTION_TO_ORIGINAL, original); // TODO support constructor
return bindingContext.get(SAM_ADAPTER_FUNCTION_TO_ORIGINAL, original);
}
if (original.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
for (FunctionDescriptor overridden : original.getOverriddenDescriptors()) {
SimpleFunctionDescriptor originalIfSamAdapter = getOriginalIfSamAdapter(overridden);
FunctionDescriptor originalIfSamAdapter = getOriginalIfSamAdapter(overridden);
if (originalIfSamAdapter != null) {
return originalIfSamAdapter;
}
@@ -2059,7 +2059,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return typeMapper.mapToFunctionInvokeCallableMethod(createInvoke(fd));
}
else {
SimpleFunctionDescriptor originalOfSamAdapter = getOriginalIfSamAdapter(fd);
SimpleFunctionDescriptor originalOfSamAdapter = (SimpleFunctionDescriptor) getOriginalIfSamAdapter(fd);
return typeMapper.mapToCallableMethod(originalOfSamAdapter != null ? originalOfSamAdapter : fd, superCall,
isCallInsideSameClassAsDeclared(fd, context),
isCallInsideSameModuleAsDeclared(fd, context),
@@ -2293,7 +2293,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
int mask = 0;
SimpleFunctionDescriptor originalOfSamAdapter = getOriginalIfSamAdapter(fd);
FunctionDescriptor originalOfSamAdapter = getOriginalIfSamAdapter(fd);
for (ValueParameterDescriptor valueParameter : fd.getValueParameters()) {
ResolvedValueArgument resolvedValueArgument = valueArguments.get(valueParameter.getIndex());
@@ -3263,7 +3263,8 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
//See StackValue.receiver for more info
pushClosureOnStack(closure, resolvedCall.getThisObject().exists() || resolvedCall.getReceiverArgument().exists());
CallableMethod method = typeMapper.mapToCallableMethod(constructorDescriptor);
ConstructorDescriptor originalOfSamAdapter = (ConstructorDescriptor) getOriginalIfSamAdapter(constructorDescriptor);
CallableMethod method = typeMapper.mapToCallableMethod(originalOfSamAdapter == null ? constructorDescriptor : originalOfSamAdapter);
invokeMethodWithArguments(method, resolvedCall, null, StackValue.none());
return StackValue.onStack(type);
@@ -0,0 +1,11 @@
class JavaClass {
private Runnable r;
public JavaClass(Runnable r) {
this.r = r;
}
public void run() {
r.run();
}
}
@@ -0,0 +1,5 @@
fun box(): String {
var v = "FAIL"
JavaClass { v = "OK" }.run()
return v
}
@@ -145,6 +145,11 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege
doTestWithJava("compiler/testData/codegen/boxWithJava/samAdapters/comparator.kt");
}
@TestMetadata("constructor.kt")
public void testConstructor() throws Exception {
doTestWithJava("compiler/testData/codegen/boxWithJava/samAdapters/constructor.kt");
}
@TestMetadata("fileFilter.kt")
public void testFileFilter() throws Exception {
doTestWithJava("compiler/testData/codegen/boxWithJava/samAdapters/fileFilter.kt");