Approximate projections in SAM type when creating SAM adapter
Use the same approach as Java 8 applies to function types (see non-wildcard parametrization in §9.9 of JLS) #KT-6918 Fixed
This commit is contained in:
+8
-38
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.load.java.structure.*;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils;
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.CapturedTypeConstructorKt;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
|
||||
import org.jetbrains.kotlin.resolve.jvm.JavaResolverUtils;
|
||||
import org.jetbrains.kotlin.types.*;
|
||||
@@ -37,6 +38,7 @@ import org.jetbrains.kotlin.types.*;
|
||||
import java.util.*;
|
||||
|
||||
import static org.jetbrains.kotlin.types.Variance.INVARIANT;
|
||||
import static org.jetbrains.kotlin.types.Variance.IN_VARIANCE;
|
||||
|
||||
public class SingleAbstractMethodUtils {
|
||||
private SingleAbstractMethodUtils() {
|
||||
@@ -53,38 +55,6 @@ public class SingleAbstractMethodUtils {
|
||||
return abstractMembers;
|
||||
}
|
||||
|
||||
private static KotlinType fixProjections(@NotNull KotlinType functionType) {
|
||||
//removes redundant projection kinds and detects conflicts
|
||||
|
||||
List<TypeParameterDescriptor> typeParameters = functionType.getConstructor().getParameters();
|
||||
List<TypeProjection> arguments = new ArrayList<TypeProjection>(typeParameters.size());
|
||||
for (TypeParameterDescriptor typeParameter : typeParameters) {
|
||||
Variance variance = typeParameter.getVariance();
|
||||
TypeProjection argument = functionType.getArguments().get(typeParameter.getIndex());
|
||||
Variance kind = argument.getProjectionKind();
|
||||
if (kind != INVARIANT && variance != INVARIANT) {
|
||||
if (kind == variance) {
|
||||
arguments.add(new TypeProjectionImpl(argument.getType()));
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
arguments.add(argument);
|
||||
}
|
||||
}
|
||||
ClassifierDescriptor classifier = functionType.getConstructor().getDeclarationDescriptor();
|
||||
assert classifier instanceof ClassDescriptor : "Not class: " + classifier;
|
||||
return KotlinTypeImpl.create(
|
||||
functionType.getAnnotations(),
|
||||
functionType.getConstructor(),
|
||||
functionType.isMarkedNullable(),
|
||||
arguments,
|
||||
((ClassDescriptor) classifier).getMemberScope(arguments)
|
||||
);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static KotlinType getFunctionTypeForSamType(@NotNull KotlinType samType) {
|
||||
// e.g. samType == Comparator<String>?
|
||||
@@ -95,13 +65,13 @@ public class SingleAbstractMethodUtils {
|
||||
KotlinType functionTypeDefault = ((JavaClassDescriptor) classifier).getFunctionTypeForSamInterface();
|
||||
|
||||
if (functionTypeDefault != null) {
|
||||
KotlinType noProjectionsSamType = SingleAbstractMethodUtilsKt.nonProjectionParametrization(samType);
|
||||
if (noProjectionsSamType == null) return null;
|
||||
|
||||
// Function2<String, String, Int>?
|
||||
KotlinType substitute = TypeSubstitutor.create(samType).substitute(functionTypeDefault, Variance.INVARIANT);
|
||||
|
||||
if (substitute == null) return null;
|
||||
|
||||
KotlinType type = fixProjections(substitute);
|
||||
if (type == null) return null;
|
||||
KotlinType type = TypeSubstitutor.create(noProjectionsSamType).substitute(functionTypeDefault, IN_VARIANCE);
|
||||
assert type != null : "Substitution based on type with no projections '" + noProjectionsSamType +
|
||||
"' should not end with conflict";
|
||||
|
||||
if (FlexibleTypesKt.isNullabilityFlexible(samType)) {
|
||||
return LazyJavaTypeResolver.FlexibleJavaClassifierTypeCapabilities.create(type, TypeUtils.makeNullable(type));
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.load.java.sam
|
||||
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.types.replace
|
||||
import org.jetbrains.kotlin.types.typeUtil.asTypeProjection
|
||||
import org.jetbrains.kotlin.types.typeUtil.containsSpecialType
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
|
||||
// If type 'samType' contains no projection, then it's non-projection parametrization is 'samType' itself
|
||||
// Else each projection type argument 'out/in A_i' (but star projections) is replaced with it's bound 'A_i'
|
||||
// Star projections are treated specially:
|
||||
// - If first upper bound of corresponding type parameter does not contain any type parameter of 'samType' class,
|
||||
// then use this upper bound instead of star projection
|
||||
// - Otherwise no non-projection parametrization exists for such 'samType'
|
||||
//
|
||||
// See Non-wildcard parametrization in §9.9 of JLS 8 for clarification
|
||||
internal fun nonProjectionParametrization(samType: KotlinType): KotlinType? {
|
||||
if (samType.arguments.none { it.projectionKind != Variance.INVARIANT }) return samType
|
||||
val parameters = samType.constructor.parameters
|
||||
val parametersSet = parameters.toSet()
|
||||
|
||||
return samType.replace(
|
||||
newArguments = samType.arguments.zip(parameters).map {
|
||||
val (projection, parameter) = it
|
||||
when {
|
||||
projection.projectionKind == Variance.INVARIANT -> projection
|
||||
|
||||
projection.isStarProjection ->
|
||||
parameter.upperBounds.first().check {
|
||||
t -> !t.containsSpecialType { it.constructor.declarationDescriptor in parametersSet }
|
||||
}?.asTypeProjection() ?: return@nonProjectionParametrization null
|
||||
|
||||
else -> projection.type.asTypeProjection()
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user