Pass SamConversionResolver explicitly cause it should be call site module-local

This commit is contained in:
Yan Zhulanow
2017-08-09 16:47:28 +03:00
committed by Pavel V. Talanov
parent f0f6a252a5
commit 6f180416b1
8 changed files with 69 additions and 41 deletions
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.kotlin.load.java.components.SamConversionResolver;
import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor;
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor;
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor;
@@ -57,29 +58,41 @@ public class SingleAbstractMethodUtils {
}
@Nullable
public static KotlinType getFunctionTypeForSamType(@NotNull KotlinType samType) {
public static SimpleType getFunctionTypeForSamInterface(
@NotNull JavaClassDescriptor clazz,
@Nullable SamConversionResolver samResolver
) {
if (samResolver == null) {
return clazz.getDefaultFunctionTypeForSamInterface();
}
return samResolver.resolveFunctionTypeIfSamInterface(clazz);
}
@Nullable
public static KotlinType getFunctionTypeForSamType(@NotNull KotlinType samType, @Nullable SamConversionResolver samResolver) {
UnwrappedType unwrappedType = samType.unwrap();
if (unwrappedType instanceof FlexibleType) {
SimpleType lower = getFunctionTypeForSamType(((FlexibleType) unwrappedType).getLowerBound());
SimpleType upper = getFunctionTypeForSamType(((FlexibleType) unwrappedType).getUpperBound());
SimpleType lower = getFunctionTypeForSamType(((FlexibleType) unwrappedType).getLowerBound(), samResolver);
SimpleType upper = getFunctionTypeForSamType(((FlexibleType) unwrappedType).getUpperBound(), samResolver);
assert (lower == null) == (upper == null) : "Illegal flexible type: " + unwrappedType;
if (upper == null) return null;
return KotlinTypeFactory.flexibleType(lower, upper);
}
else {
return getFunctionTypeForSamType((SimpleType) unwrappedType);
return getFunctionTypeForSamType((SimpleType) unwrappedType, samResolver);
}
}
@Nullable
private static SimpleType getFunctionTypeForSamType(@NotNull SimpleType samType) {
private static SimpleType getFunctionTypeForSamType(@NotNull SimpleType samType, @Nullable SamConversionResolver samResolver) {
// e.g. samType == Comparator<String>?
ClassifierDescriptor classifier = samType.getConstructor().getDeclarationDescriptor();
if (classifier instanceof JavaClassDescriptor) {
// Function2<T, T, Int>
SimpleType functionTypeDefault = ((JavaClassDescriptor) classifier).getFunctionTypeForSamInterface();
SimpleType functionTypeDefault = getFunctionTypeForSamInterface((JavaClassDescriptor) classifier, samResolver);
if (functionTypeDefault != null) {
SimpleType noProjectionsSamType = SingleAbstractMethodUtilsKt.nonProjectionParametrization(samType);
@@ -157,7 +170,8 @@ public class SingleAbstractMethodUtils {
@NotNull
public static SamConstructorDescriptor createSamConstructorFunction(
@NotNull DeclarationDescriptor owner,
@NotNull JavaClassDescriptor samInterface
@NotNull JavaClassDescriptor samInterface,
@NotNull SamConversionResolver samResolver
) {
assert getSingleAbstractMethodOrNull(samInterface) != null : samInterface;
@@ -165,7 +179,7 @@ public class SingleAbstractMethodUtils {
List<TypeParameterDescriptor> samTypeParameters = samInterface.getTypeConstructor().getParameters();
SimpleType unsubstitutedSamType = samInterface.getDefaultType();
initializeSamConstructorDescriptor(samInterface, result, samTypeParameters, unsubstitutedSamType);
initializeSamConstructorDescriptor(samInterface, result, samTypeParameters, unsubstitutedSamType, samResolver);
return result;
}
@@ -174,11 +188,12 @@ public class SingleAbstractMethodUtils {
@NotNull JavaClassDescriptor samInterface,
@NotNull SimpleFunctionDescriptorImpl samConstructor,
@NotNull List<TypeParameterDescriptor> samTypeParameters,
@NotNull KotlinType unsubstitutedSamType
@NotNull KotlinType unsubstitutedSamType,
@NotNull SamConversionResolver samResolver
) {
TypeParameters typeParameters = recreateAndInitializeTypeParameters(samTypeParameters, samConstructor);
KotlinType parameterTypeUnsubstituted = getFunctionTypeForSamType(unsubstitutedSamType);
KotlinType parameterTypeUnsubstituted = getFunctionTypeForSamType(unsubstitutedSamType, samResolver);
assert parameterTypeUnsubstituted != null : "couldn't get function type for SAM type " + unsubstitutedSamType;
KotlinType parameterType = typeParameters.substitutor.substitute(parameterTypeUnsubstituted, Variance.IN_VARIANCE);
assert parameterType != null : "couldn't substitute type: " + parameterTypeUnsubstituted +
@@ -207,20 +222,21 @@ public class SingleAbstractMethodUtils {
public static SamConstructorDescriptor createTypeAliasSamConstructorFunction(
@NotNull TypeAliasDescriptor typeAliasDescriptor,
@NotNull SamConstructorDescriptor underlyingSamConstructor
@NotNull SamConstructorDescriptor underlyingSamConstructor,
@NotNull SamConversionResolver samResolver
) {
SamTypeAliasConstructorDescriptorImpl result = new SamTypeAliasConstructorDescriptorImpl(typeAliasDescriptor, underlyingSamConstructor);
JavaClassDescriptor samInterface = underlyingSamConstructor.getBaseDescriptorForSynthetic();
List<TypeParameterDescriptor> samTypeParameters = typeAliasDescriptor.getTypeConstructor().getParameters();
SimpleType unsubstitutedSamType = typeAliasDescriptor.getExpandedType();
initializeSamConstructorDescriptor(samInterface, result, samTypeParameters, unsubstitutedSamType);
initializeSamConstructorDescriptor(samInterface, result, samTypeParameters, unsubstitutedSamType, samResolver);
return result;
}
public static boolean isSamType(@NotNull KotlinType type) {
return getFunctionTypeForSamType(type) != null;
return getFunctionTypeForSamType(type, null) != null;
}
public static boolean isSamAdapterNecessary(@NotNull FunctionDescriptor fun) {
@@ -233,7 +249,10 @@ public class SingleAbstractMethodUtils {
}
@NotNull
public static SamAdapterDescriptor<JavaMethodDescriptor> createSamAdapterFunction(@NotNull JavaMethodDescriptor original) {
public static SamAdapterDescriptor<JavaMethodDescriptor> createSamAdapterFunction(
@NotNull JavaMethodDescriptor original,
@NotNull SamConversionResolver samResolver
) {
SamAdapterFunctionDescriptor result = new SamAdapterFunctionDescriptor(original);
return initSamAdapter(original, result, new FunctionInitializer() {
@Override
@@ -252,11 +271,14 @@ public class SingleAbstractMethodUtils {
original.getVisibility()
);
}
});
}, samResolver);
}
@NotNull
public static SamAdapterDescriptor<JavaClassConstructorDescriptor> createSamAdapterConstructor(@NotNull JavaClassConstructorDescriptor original) {
public static SamAdapterDescriptor<JavaClassConstructorDescriptor> createSamAdapterConstructor(
@NotNull JavaClassConstructorDescriptor original,
@NotNull SamConversionResolver samResolver
) {
SamAdapterClassConstructorDescriptor result = new SamAdapterClassConstructorDescriptor(original);
return initSamAdapter(original, result, new FunctionInitializer() {
@Override
@@ -268,14 +290,15 @@ public class SingleAbstractMethodUtils {
result.initialize(valueParameters, original.getVisibility());
result.setReturnType(returnType);
}
});
}, samResolver);
}
@NotNull
private static <F extends FunctionDescriptor> SamAdapterDescriptor<F> initSamAdapter(
@NotNull F original,
@NotNull SamAdapterDescriptor<F> adapter,
@NotNull FunctionInitializer initializer
@NotNull FunctionInitializer initializer,
@NotNull SamConversionResolver samResolver
) {
TypeParameters typeParameters = recreateAndInitializeTypeParameters(original.getTypeParameters(), adapter);
@@ -288,7 +311,7 @@ public class SingleAbstractMethodUtils {
", substitutor = " + substitutor;
List<ValueParameterDescriptor> valueParameters = createValueParametersForSamAdapter(original, adapter, substitutor);
List<ValueParameterDescriptor> valueParameters = createValueParametersForSamAdapter(original, adapter, substitutor, samResolver);
initializer.initialize(typeParameters.descriptors, valueParameters, returnType);
@@ -298,13 +321,14 @@ public class SingleAbstractMethodUtils {
public static List<ValueParameterDescriptor> createValueParametersForSamAdapter(
@NotNull FunctionDescriptor original,
@NotNull FunctionDescriptor samAdapter,
@NotNull TypeSubstitutor substitutor
@NotNull TypeSubstitutor substitutor,
@NotNull SamConversionResolver samResolver
) {
List<ValueParameterDescriptor> originalValueParameters = original.getValueParameters();
List<ValueParameterDescriptor> valueParameters = new ArrayList<>(originalValueParameters.size());
for (ValueParameterDescriptor originalParam : originalValueParameters) {
KotlinType originalType = originalParam.getType();
KotlinType functionType = getFunctionTypeForSamType(originalType);
KotlinType functionType = getFunctionTypeForSamType(originalType, samResolver);
KotlinType newTypeUnsubstituted = functionType != null ? functionType : originalType;
KotlinType newType = substitutor.substitute(newTypeUnsubstituted, Variance.IN_VARIANCE);
assert newType != null : "couldn't substitute type: " + newTypeUnsubstituted + ", substitutor = " + substitutor;
@@ -18,16 +18,18 @@ package org.jetbrains.kotlin.synthetic
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.load.java.components.SamConversionResolver
import org.jetbrains.kotlin.resolve.scopes.SyntheticScopes
import org.jetbrains.kotlin.storage.StorageManager
class JavaSyntheticScopes(
storageManager: StorageManager,
lookupTracker: LookupTracker,
languageVersionSettings: LanguageVersionSettings
languageVersionSettings: LanguageVersionSettings,
samConventionResolver: SamConversionResolver
): SyntheticScopes {
override val scopes = listOf(
JavaSyntheticPropertiesScope(storageManager, lookupTracker),
SamAdapterFunctionsScope(storageManager, languageVersionSettings)
SamAdapterFunctionsScope(storageManager, languageVersionSettings, samConventionResolver)
)
}
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor
import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptorImpl
import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor
import org.jetbrains.kotlin.incremental.components.LookupLocation
import org.jetbrains.kotlin.load.java.components.SamConversionResolver
import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
@@ -49,7 +50,8 @@ interface SamAdapterExtensionFunctionDescriptor : FunctionDescriptor, SyntheticM
class SamAdapterFunctionsScope(
storageManager: StorageManager,
private val languageVersionSettings: LanguageVersionSettings
private val languageVersionSettings: LanguageVersionSettings,
private val samResolver: SamConversionResolver
) : SyntheticScope {
private val extensionForFunction = storageManager.createMemoizedFunctionWithNullableValues<FunctionDescriptor, FunctionDescriptor> { function ->
extensionForFunctionNotCached(function)
@@ -57,17 +59,17 @@ class SamAdapterFunctionsScope(
private val samAdapterForStaticFunction =
storageManager.createMemoizedFunction<JavaMethodDescriptor, SamAdapterDescriptor<JavaMethodDescriptor>> { function ->
SingleAbstractMethodUtils.createSamAdapterFunction(function)
SingleAbstractMethodUtils.createSamAdapterFunction(function, samResolver)
}
private val samConstructorForClassifier =
storageManager.createMemoizedFunction<JavaClassDescriptor, SamConstructorDescriptor> { classifier ->
SingleAbstractMethodUtils.createSamConstructorFunction(classifier.containingDeclaration, classifier)
SingleAbstractMethodUtils.createSamConstructorFunction(classifier.containingDeclaration, classifier, samResolver)
}
private val samConstructorForJavaConstructor =
storageManager.createMemoizedFunction<JavaClassConstructorDescriptor, ClassConstructorDescriptor> { constructor ->
SingleAbstractMethodUtils.createSamAdapterConstructor(constructor) as ClassConstructorDescriptor
SingleAbstractMethodUtils.createSamAdapterConstructor(constructor, samResolver) as ClassConstructorDescriptor
}
private val samConstructorForTypeAliasConstructor =
@@ -82,7 +84,7 @@ class SamAdapterFunctionsScope(
if (!SingleAbstractMethodUtils.isSamAdapterNecessary(function)) return null
if (function.returnType == null) return null
if (function.isHiddenInResolution(languageVersionSettings)) return null
return MyFunctionDescriptor.create(function)
return MyFunctionDescriptor.create(function, samResolver)
}
override fun getSyntheticMemberFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
@@ -199,15 +201,16 @@ class SamAdapterFunctionsScope(
return getTypeAliasSamConstructor(classifier)
}
if (classifier !is LazyJavaClassDescriptor || classifier.functionTypeForSamInterface == null) return null
if (classifier !is LazyJavaClassDescriptor || classifier.defaultFunctionTypeForSamInterface == null) return null
return samConstructorForClassifier(classifier)
}
private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? {
val classDescriptor = classifier.classDescriptor ?: return null
if (classDescriptor !is LazyJavaClassDescriptor || classDescriptor.functionTypeForSamInterface == null) return null
if (classDescriptor !is LazyJavaClassDescriptor || classDescriptor.defaultFunctionTypeForSamInterface == null) return null
return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(classifier, samConstructorForClassifier(classDescriptor))
return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(
classifier, samConstructorForClassifier(classDescriptor), samResolver)
}
private class MyFunctionDescriptor(
@@ -227,7 +230,7 @@ class SamAdapterFunctionsScope(
}
companion object {
fun create(sourceFunction: FunctionDescriptor): MyFunctionDescriptor {
fun create(sourceFunction: FunctionDescriptor, samResolver: SamConversionResolver): MyFunctionDescriptor {
val descriptor = MyFunctionDescriptor(sourceFunction.containingDeclaration,
null,
sourceFunction.annotations,
@@ -243,7 +246,8 @@ class SamAdapterFunctionsScope(
val typeSubstitutor = DescriptorSubstitutor.substituteTypeParameters(sourceTypeParams, TypeSubstitution.EMPTY, descriptor, typeParameters)
val returnType = typeSubstitutor.safeSubstitute(sourceFunction.returnType!!, Variance.INVARIANT)
val valueParameters = SingleAbstractMethodUtils.createValueParametersForSamAdapter(sourceFunction, descriptor, typeSubstitutor)
val valueParameters = SingleAbstractMethodUtils.createValueParametersForSamAdapter(
sourceFunction, descriptor, typeSubstitutor, samResolver)
val visibility = syntheticVisibility(sourceFunction, isUsedForExtension = false)