diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConversionResolverImpl.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConversionResolverImpl.kt index fccd048386f..e7ee9d71672 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConversionResolverImpl.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamConversionResolverImpl.kt @@ -25,7 +25,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor -import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.SimpleType object SamConversionResolverImpl : SamConversionResolver { override fun resolveSamConstructor(constructorOwner: DeclarationDescriptor, classifier: () -> ClassifierDescriptor?): SamConstructorDescriptor? { @@ -44,7 +44,7 @@ object SamConversionResolverImpl : SamConversionResolver { } } - override fun resolveFunctionTypeIfSamInterface(classDescriptor: JavaClassDescriptor): KotlinType? { + override fun resolveFunctionTypeIfSamInterface(classDescriptor: JavaClassDescriptor): SimpleType? { val abstractMethod = SingleAbstractMethodUtils.getSingleAbstractMethodOrNull(classDescriptor) ?: return null return SingleAbstractMethodUtils.getFunctionTypeForAbstractMethod(abstractMethod) } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java index b422f71e4ea..e6999c57cb6 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.java @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl; import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl; import org.jetbrains.kotlin.load.java.descriptors.*; -import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.resolve.DescriptorUtils; import org.jetbrains.kotlin.resolve.FunctionTypeResolveUtilsKt; @@ -55,15 +54,31 @@ public class SingleAbstractMethodUtils { @Nullable public static KotlinType getFunctionTypeForSamType(@NotNull KotlinType samType) { + UnwrappedType unwrappedType = samType.unwrap(); + if (unwrappedType instanceof FlexibleType) { + SimpleType lower = getFunctionTypeForSamType(((FlexibleType) unwrappedType).getLowerBound()); + SimpleType upper = getFunctionTypeForSamType(((FlexibleType) unwrappedType).getUpperBound()); + assert (lower == null) == (upper == null) : "Illegal flexible type: " + unwrappedType; + + if (upper == null) return null; + return KotlinTypeFactory.flexibleType(lower, upper); + } + else { + return getFunctionTypeForSamType((SimpleType) unwrappedType); + } + } + + @Nullable + private static SimpleType getFunctionTypeForSamType(@NotNull SimpleType samType) { // e.g. samType == Comparator? ClassifierDescriptor classifier = samType.getConstructor().getDeclarationDescriptor(); if (classifier instanceof JavaClassDescriptor) { // Function2 - KotlinType functionTypeDefault = ((JavaClassDescriptor) classifier).getFunctionTypeForSamInterface(); + SimpleType functionTypeDefault = ((JavaClassDescriptor) classifier).getFunctionTypeForSamInterface(); if (functionTypeDefault != null) { - KotlinType noProjectionsSamType = SingleAbstractMethodUtilsKt.nonProjectionParametrization(samType); + SimpleType noProjectionsSamType = SingleAbstractMethodUtilsKt.nonProjectionParametrization(samType); if (noProjectionsSamType == null) return null; // Function2? @@ -71,19 +86,16 @@ public class SingleAbstractMethodUtils { assert type != null : "Substitution based on type with no projections '" + noProjectionsSamType + "' should not end with conflict"; - if (FlexibleTypesKt.isNullabilityFlexible(samType)) { - SimpleType simpleType = KotlinTypeKt.asSimpleType(type); - return KotlinTypeFactory.flexibleType(simpleType, simpleType.makeNullableAsSpecified(true)); - } + SimpleType simpleType = KotlinTypeKt.asSimpleType(type); - return TypeUtils.makeNullableAsSpecified(type, samType.isMarkedNullable()); + return simpleType.makeNullableAsSpecified(samType.isMarkedNullable()); } } return null; } @NotNull - public static KotlinType getFunctionTypeForAbstractMethod(@NotNull FunctionDescriptor function) { + public static SimpleType getFunctionTypeForAbstractMethod(@NotNull FunctionDescriptor function) { KotlinType returnType = function.getReturnType(); assert returnType != null : "function is not initialized: " + function; List valueParameters = function.getValueParameters(); diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.kt index 7d1d8c00a49..a25fd2ae229 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SingleAbstractMethodUtils.kt @@ -16,7 +16,7 @@ package org.jetbrains.kotlin.load.java.sam -import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.SimpleType import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.replace import org.jetbrains.kotlin.types.typeUtil.asTypeProjection @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.check // - Otherwise no non-projection parametrization exists for such 'samType' // // See Non-wildcard parametrization in JLS 8 p.9.9 for clarification -internal fun nonProjectionParametrization(samType: KotlinType): KotlinType? { +internal fun nonProjectionParametrization(samType: SimpleType): SimpleType? { if (samType.arguments.none { it.projectionKind != Variance.INVARIANT }) return samType val parameters = samType.constructor.parameters val parametersSet = parameters.toSet() diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/SamConversionResolver.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/SamConversionResolver.kt index 12e5c947b11..2ebd3f2a740 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/SamConversionResolver.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/components/SamConversionResolver.kt @@ -21,18 +21,18 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor import org.jetbrains.kotlin.load.java.descriptors.SamConstructorDescriptor -import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.SimpleType interface SamConversionResolver { companion object EMPTY : SamConversionResolver { override fun resolveSamAdapter(original: D) = null override fun resolveSamConstructor(constructorOwner: DeclarationDescriptor, classifier: () -> ClassifierDescriptor?) = null - override fun resolveFunctionTypeIfSamInterface(classDescriptor: JavaClassDescriptor): KotlinType? = null + override fun resolveFunctionTypeIfSamInterface(classDescriptor: JavaClassDescriptor): SimpleType? = null } fun resolveSamConstructor(constructorOwner: DeclarationDescriptor, classifier: () -> ClassifierDescriptor?): SamConstructorDescriptor? fun resolveSamAdapter(original: D): D? - fun resolveFunctionTypeIfSamInterface(classDescriptor: JavaClassDescriptor): KotlinType? + fun resolveFunctionTypeIfSamInterface(classDescriptor: JavaClassDescriptor): SimpleType? } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaClassDescriptor.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaClassDescriptor.java index d3f9c94ddce..203a898f5fe 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaClassDescriptor.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaClassDescriptor.java @@ -18,9 +18,9 @@ package org.jetbrains.kotlin.load.java.descriptors; import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.ClassDescriptor; -import org.jetbrains.kotlin.types.KotlinType; +import org.jetbrains.kotlin.types.SimpleType; public interface JavaClassDescriptor extends ClassDescriptor { @Nullable - KotlinType getFunctionTypeForSamInterface(); + SimpleType getFunctionTypeForSamInterface(); } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt index fbc0c6c26f7..bfd73e258ed 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/lazy/descriptors/LazyJavaClassDescriptor.kt @@ -126,7 +126,7 @@ class LazyJavaClassDescriptor( override fun getDeclaredTypeParameters() = declaredParameters() - override fun getFunctionTypeForSamInterface(): KotlinType? = functionTypeForSamInterface() + override fun getFunctionTypeForSamInterface(): SimpleType? = functionTypeForSamInterface() override fun isCompanionObject() = false