Supported SAM adapters with type parameters.

This commit is contained in:
Evgeny Gerashchenko
2013-04-18 17:15:40 +04:00
parent db8d285b25
commit 8c4e45de9a
7 changed files with 73 additions and 3 deletions
@@ -145,12 +145,19 @@ public class SingleAbstractMethodUtils {
CallableMemberDescriptor.Kind.SYNTHESIZED
);
JetType returnType = original.getReturnType();
TypeParameters typeParameters = recreateAndInitializeTypeParameters(original.getTypeParameters(), result);
JetType returnTypeUnsubstituted = original.getReturnType();
assert returnTypeUnsubstituted != null : original;
JetType returnType = typeParameters.substitutor.substitute(returnTypeUnsubstituted, Variance.OUT_VARIANCE);
assert returnType != null : "couldn't substitute type: " + returnType + ", substitutor = " + typeParameters.substitutor;
List<ValueParameterDescriptor> valueParameters = Lists.newArrayList();
for (ValueParameterDescriptor originalParam : original.getValueParameters()) {
JetType originalType = originalParam.getType();
JetType newType = isSamType(originalType) ? getFunctionTypeForSamType(originalType) : originalType;
JetType newTypeUnsubstituted = isSamType(originalType) ? getFunctionTypeForSamType(originalType) : originalType;
JetType newType = typeParameters.substitutor.substitute(newTypeUnsubstituted, Variance.IN_VARIANCE);
assert newType != null : "couldn't substitute type: " + newTypeUnsubstituted + ", substitutor = " + typeParameters.substitutor;
ValueParameterDescriptor newParam = new ValueParameterDescriptorImpl(
result, originalParam.getIndex(), originalParam.getAnnotations(), originalParam.getName(), newType, false, null);
@@ -160,7 +167,7 @@ public class SingleAbstractMethodUtils {
result.initialize(
null,
original.getExpectedThisObject(),
Collections.<TypeParameterDescriptor>emptyList(), // TODO copy type parameters
typeParameters.descriptors,
valueParameters,
returnType,
original.getModality(),
@@ -0,0 +1,11 @@
import java.util.*;
class WeirdComparator {
public static <T> T max(Comparator<T> comparator, T value1, T value2) {
return comparator.compare(value1, value2) > 0 ? value1 : value2;
}
public static <T extends CharSequence> T max2(Comparator<T> comparator, T value1, T value2) {
return comparator.compare(value1, value2) > 0 ? value1 : value2;
}
}
@@ -0,0 +1,9 @@
fun box(): String {
val result = WeirdComparator.max<String>({ a, b -> a.length - b.length }, "java", "kotlin")
if (result != "kotlin") return "Wrong: $result"
val result2 = WeirdComparator.max2<String>({ a, b -> a.length - b.length }, "java", "kotlin")
if (result2 != "kotlin") return "Wrong: $result"
return "OK"
}
@@ -0,0 +1,19 @@
package test;
import java.lang.UnsupportedOperationException;
import java.util.*;
import java.util.Comparator;
public class TypeParameterOfMethod {
public static <T> T max(Comparator<T> comparator, T value1, T value2) {
return comparator.compare(value1, value2) > 0 ? value1 : value2;
}
public static <T extends CharSequence> T max2(Comparator<T> comparator, T value1, T value2) {
return comparator.compare(value1, value2) > 0 ? value1 : value2;
}
public static <A extends CharSequence, B extends List<A>> void method(Comparator<A> a, B b) {
throw new UnsupportedOperationException();
}
}
@@ -0,0 +1,14 @@
package test
public open class TypeParameterOfMethod : java.lang.Object {
public constructor TypeParameterOfMethod()
}
package TypeParameterOfMethod {
public open /*synthesized*/ fun </*0*/ T> max(/*0*/ p0 : ((T?, T?) -> jet.Int)?, /*1*/ p1 : T?, /*2*/ p2 : T?) : T?
public open fun </*0*/ T> max(/*0*/ p0 : java.util.Comparator<T>?, /*1*/ p1 : T?, /*2*/ p2 : T?) : T?
public open /*synthesized*/ fun </*0*/ T : jet.CharSequence?> max2(/*0*/ p0 : ((T?, T?) -> jet.Int)?, /*1*/ p1 : T?, /*2*/ p2 : T?) : T?
public open fun </*0*/ T : jet.CharSequence?> max2(/*0*/ p0 : java.util.Comparator<T>?, /*1*/ p1 : T?, /*2*/ p2 : T?) : T?
public open /*synthesized*/ fun </*0*/ A : jet.CharSequence?, /*1*/ B : jet.List<A>?> method(/*0*/ p0 : ((A?, A?) -> jet.Int)?, /*1*/ p1 : B?) : jet.Unit
public open fun </*0*/ A : jet.CharSequence?, /*1*/ B : jet.List<A>?> method(/*0*/ p0 : java.util.Comparator<A>?, /*1*/ p1 : B?) : jet.Unit
}
@@ -142,6 +142,11 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege
doTestWithJava("compiler/testData/codegen/boxWithJava/samAdapters/typeParameterOfClass.kt");
}
@TestMetadata("typeParameterOfMethod.kt")
public void testTypeParameterOfMethod() throws Exception {
doTestWithJava("compiler/testData/codegen/boxWithJava/samAdapters/typeParameterOfMethod.kt");
}
}
@TestMetadata("compiler/testData/codegen/boxWithJava/staticFun")
@@ -1244,6 +1244,11 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
doTestCompiledJava("compiler/testData/loadJava/compiledJava/singleAbstractMethod/adapter/TypeParameterOfClass.java");
}
@TestMetadata("TypeParameterOfMethod.java")
public void testTypeParameterOfMethod() throws Exception {
doTestCompiledJava("compiler/testData/loadJava/compiledJava/singleAbstractMethod/adapter/TypeParameterOfMethod.java");
}
}
public static Test innerSuite() {