Refactoring: SingleAbstractMethodUtils -> JavaSingleAbstractMethodUtils
This commit is contained in:
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
|||||||
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
|
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
|
||||||
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor;
|
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor;
|
||||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
|
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils;
|
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils;
|
||||||
import org.jetbrains.kotlin.resolve.sam.SamConversionResolverImplKt;
|
import org.jetbrains.kotlin.resolve.sam.SamConversionResolverImplKt;
|
||||||
import org.jetbrains.kotlin.types.KotlinType;
|
import org.jetbrains.kotlin.types.KotlinType;
|
||||||
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
|
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
|
||||||
@@ -47,7 +47,7 @@ public class SamType {
|
|||||||
return create(TypeMapperUtilsKt.removeExternalProjections(originalTypeToUse));
|
return create(TypeMapperUtilsKt.removeExternalProjections(originalTypeToUse));
|
||||||
}
|
}
|
||||||
public static SamType create(@NotNull KotlinType originalType) {
|
public static SamType create(@NotNull KotlinType originalType) {
|
||||||
if (!SingleAbstractMethodUtils.isSamType(originalType)) return null;
|
if (!JavaSingleAbstractMethodUtils.isSamType(originalType)) return null;
|
||||||
return new SamType(originalType);
|
return new SamType(originalType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -41,8 +41,8 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class SingleAbstractMethodUtils {
|
public class JavaSingleAbstractMethodUtils {
|
||||||
private SingleAbstractMethodUtils() {
|
private JavaSingleAbstractMethodUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
+15
-12
@@ -32,7 +32,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
|||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
|
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor
|
import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor
|
import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.resolve.calls.components.isVararg
|
import org.jetbrains.kotlin.resolve.calls.components.isVararg
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.wrapWithCapturingSubstitution
|
import org.jetbrains.kotlin.resolve.calls.inference.wrapWithCapturingSubstitution
|
||||||
@@ -70,17 +70,20 @@ class SamAdapterFunctionsScope(
|
|||||||
|
|
||||||
private val samAdapterForStaticFunction =
|
private val samAdapterForStaticFunction =
|
||||||
storageManager.createMemoizedFunction<JavaMethodDescriptor, SamAdapterDescriptor<JavaMethodDescriptor>> { function ->
|
storageManager.createMemoizedFunction<JavaMethodDescriptor, SamAdapterDescriptor<JavaMethodDescriptor>> { function ->
|
||||||
SingleAbstractMethodUtils.createSamAdapterFunction(function, samResolver, samConversionOracle)
|
JavaSingleAbstractMethodUtils
|
||||||
|
.createSamAdapterFunction(function, samResolver, samConversionOracle)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val samConstructorForClassifier =
|
private val samConstructorForClassifier =
|
||||||
storageManager.createMemoizedFunction<ClassDescriptor, SamConstructorDescriptor> { classifier ->
|
storageManager.createMemoizedFunction<ClassDescriptor, SamConstructorDescriptor> { classifier ->
|
||||||
SingleAbstractMethodUtils.createSamConstructorFunction(classifier.containingDeclaration, classifier, samResolver, samConversionOracle)
|
JavaSingleAbstractMethodUtils
|
||||||
|
.createSamConstructorFunction(classifier.containingDeclaration, classifier, samResolver, samConversionOracle)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val samConstructorForJavaConstructor =
|
private val samConstructorForJavaConstructor =
|
||||||
storageManager.createMemoizedFunction<JavaClassConstructorDescriptor, ClassConstructorDescriptor> { constructor ->
|
storageManager.createMemoizedFunction<JavaClassConstructorDescriptor, ClassConstructorDescriptor> { constructor ->
|
||||||
SingleAbstractMethodUtils.createSamAdapterConstructor(constructor, samResolver, samConversionOracle) as ClassConstructorDescriptor
|
JavaSingleAbstractMethodUtils
|
||||||
|
.createSamAdapterConstructor(constructor, samResolver, samConversionOracle) as ClassConstructorDescriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
private val samConstructorForTypeAliasConstructor =
|
private val samConstructorForTypeAliasConstructor =
|
||||||
@@ -92,7 +95,7 @@ class SamAdapterFunctionsScope(
|
|||||||
private fun extensionForFunctionNotCached(function: FunctionDescriptor): FunctionDescriptor? {
|
private fun extensionForFunctionNotCached(function: FunctionDescriptor): FunctionDescriptor? {
|
||||||
if (!function.visibility.isVisibleOutside()) return null
|
if (!function.visibility.isVisibleOutside()) return null
|
||||||
if (!function.hasJavaOriginInHierarchy()) return null //TODO: should we go into base at all?
|
if (!function.hasJavaOriginInHierarchy()) return null //TODO: should we go into base at all?
|
||||||
if (!SingleAbstractMethodUtils.isSamAdapterNecessary(function)) return null
|
if (!JavaSingleAbstractMethodUtils.isSamAdapterNecessary(function)) return null
|
||||||
if (function.returnType == null) return null
|
if (function.returnType == null) return null
|
||||||
if (deprecationResolver.isHiddenInResolution(function)) return null
|
if (deprecationResolver.isHiddenInResolution(function)) return null
|
||||||
return SamAdapterExtensionFunctionDescriptorImpl.create(function, samResolver, samConversionOracle)
|
return SamAdapterExtensionFunctionDescriptorImpl.create(function, samResolver, samConversionOracle)
|
||||||
@@ -181,7 +184,7 @@ class SamAdapterFunctionsScope(
|
|||||||
private fun recordSamLookupsToClassifier(classifier: ClassifierDescriptor, location: LookupLocation) {
|
private fun recordSamLookupsToClassifier(classifier: ClassifierDescriptor, location: LookupLocation) {
|
||||||
if (classifier !is JavaClassDescriptor || classifier.kind != ClassKind.INTERFACE) return
|
if (classifier !is JavaClassDescriptor || classifier.kind != ClassKind.INTERFACE) return
|
||||||
// TODO: We should also record SAM lookups even when the interface is not SAM
|
// TODO: We should also record SAM lookups even when the interface is not SAM
|
||||||
if (!SingleAbstractMethodUtils.isSamType(classifier.defaultType)) return
|
if (!JavaSingleAbstractMethodUtils.isSamType(classifier.defaultType)) return
|
||||||
|
|
||||||
lookupTracker.record(location, classifier, SAM_LOOKUP_NAME)
|
lookupTracker.record(location, classifier, SAM_LOOKUP_NAME)
|
||||||
}
|
}
|
||||||
@@ -217,7 +220,7 @@ class SamAdapterFunctionsScope(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createJavaSamAdapterConstructor(constructor: JavaClassConstructorDescriptor): ClassConstructorDescriptor? {
|
private fun createJavaSamAdapterConstructor(constructor: JavaClassConstructorDescriptor): ClassConstructorDescriptor? {
|
||||||
if (!SingleAbstractMethodUtils.isSamAdapterNecessary(constructor)) return null
|
if (!JavaSingleAbstractMethodUtils.isSamAdapterNecessary(constructor)) return null
|
||||||
return samConstructorForJavaConstructor(constructor)
|
return samConstructorForJavaConstructor(constructor)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +233,7 @@ class SamAdapterFunctionsScope(
|
|||||||
return functions.mapNotNull { function ->
|
return functions.mapNotNull { function ->
|
||||||
if (function !is JavaMethodDescriptor) return@mapNotNull null
|
if (function !is JavaMethodDescriptor) return@mapNotNull null
|
||||||
if (function.dispatchReceiverParameter != null) return@mapNotNull null // consider only statics
|
if (function.dispatchReceiverParameter != null) return@mapNotNull null // consider only statics
|
||||||
if (!SingleAbstractMethodUtils.isSamAdapterNecessary(function)) return@mapNotNull null
|
if (!JavaSingleAbstractMethodUtils.isSamAdapterNecessary(function)) return@mapNotNull null
|
||||||
if (samViaSyntheticScopeDisabled && !function.shouldGenerateCandidateForVarargAfterSamAndHasVararg)
|
if (samViaSyntheticScopeDisabled && !function.shouldGenerateCandidateForVarargAfterSamAndHasVararg)
|
||||||
return@mapNotNull null
|
return@mapNotNull null
|
||||||
|
|
||||||
@@ -261,16 +264,16 @@ class SamAdapterFunctionsScope(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (classifier !is ClassDescriptor) return null
|
if (classifier !is ClassDescriptor) return null
|
||||||
if (!SingleAbstractMethodUtils.isSamClassDescriptor(classifier)) return null
|
if (!JavaSingleAbstractMethodUtils.isSamClassDescriptor(classifier)) return null
|
||||||
|
|
||||||
return samConstructorForClassifier(classifier)
|
return samConstructorForClassifier(classifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? {
|
private fun getTypeAliasSamConstructor(classifier: TypeAliasDescriptor): SamConstructorDescriptor? {
|
||||||
val classDescriptor = classifier.classDescriptor ?: return null
|
val classDescriptor = classifier.classDescriptor ?: return null
|
||||||
if (!SingleAbstractMethodUtils.isSamClassDescriptor(classDescriptor)) return null
|
if (!JavaSingleAbstractMethodUtils.isSamClassDescriptor(classDescriptor)) return null
|
||||||
|
|
||||||
return SingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(
|
return JavaSingleAbstractMethodUtils.createTypeAliasSamConstructorFunction(
|
||||||
classifier, samConstructorForClassifier(classDescriptor), samResolver, samConversionOracle
|
classifier, samConstructorForClassifier(classDescriptor), samResolver, samConversionOracle
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -313,7 +316,7 @@ class SamAdapterFunctionsScope(
|
|||||||
val typeSubstitutor = DescriptorSubstitutor.substituteTypeParameters(sourceTypeParams, TypeSubstitution.EMPTY, descriptor, typeParameters)
|
val typeSubstitutor = DescriptorSubstitutor.substituteTypeParameters(sourceTypeParams, TypeSubstitution.EMPTY, descriptor, typeParameters)
|
||||||
|
|
||||||
val returnType = typeSubstitutor.safeSubstitute(sourceFunction.returnType!!, Variance.INVARIANT)
|
val returnType = typeSubstitutor.safeSubstitute(sourceFunction.returnType!!, Variance.INVARIANT)
|
||||||
val valueParameters = SingleAbstractMethodUtils.createValueParametersForSamAdapter(
|
val valueParameters = JavaSingleAbstractMethodUtils.createValueParametersForSamAdapter(
|
||||||
sourceFunction, descriptor, typeSubstitutor, samResolver, samConversionOracle
|
sourceFunction, descriptor, typeSubstitutor, samResolver, samConversionOracle
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
|
|||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor
|
import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor
|
import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils
|
||||||
import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability
|
import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability
|
||||||
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
|
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
|
||||||
import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions
|
import org.jetbrains.kotlin.psi2ir.generators.GeneratorExtensions
|
||||||
@@ -50,7 +50,7 @@ class JvmGeneratorExtensions(private val generateFacades: Boolean = true) : Gene
|
|||||||
descriptor is SamConstructorDescriptor
|
descriptor is SamConstructorDescriptor
|
||||||
|
|
||||||
override fun isSamType(type: KotlinType): Boolean =
|
override fun isSamType(type: KotlinType): Boolean =
|
||||||
SingleAbstractMethodUtils.isSamType(type)
|
JavaSingleAbstractMethodUtils.isSamType(type)
|
||||||
|
|
||||||
override fun getSamTypeInfoForValueParameter(valueParameter: ValueParameterDescriptor): KotlinType? {
|
override fun getSamTypeInfoForValueParameter(valueParameter: ValueParameterDescriptor): KotlinType? {
|
||||||
val samType = SamType.createByValueParameter(valueParameter) ?: return null
|
val samType = SamType.createByValueParameter(valueParameter) ?: return null
|
||||||
|
|||||||
-1
@@ -42,7 +42,6 @@ import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
|||||||
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
import org.jetbrains.kotlin.idea.util.application.runReadAction
|
||||||
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
|
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
|
||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.idea.util.getResolutionScope
|
|||||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||||
import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor
|
import org.jetbrains.kotlin.load.java.sam.SamAdapterDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor
|
import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
@@ -246,7 +246,7 @@ class RedundantSamConstructorInspection : AbstractKotlinInspection() {
|
|||||||
val samAdapterOriginalFunction = SamCodegenUtil.getOriginalIfSamAdapter(samAdapter)?.original
|
val samAdapterOriginalFunction = SamCodegenUtil.getOriginalIfSamAdapter(samAdapter)?.original
|
||||||
if (samAdapterOriginalFunction != originalFunction) return false
|
if (samAdapterOriginalFunction != originalFunction) return false
|
||||||
|
|
||||||
val parametersWithSamTypeCount = originalFunction.valueParameters.count { SingleAbstractMethodUtils.isSamType(it.type) }
|
val parametersWithSamTypeCount = originalFunction.valueParameters.count { JavaSingleAbstractMethodUtils.isSamType(it.type) }
|
||||||
return parametersWithSamTypeCount == samConstructorsCount
|
return parametersWithSamTypeCount == samConstructorsCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.idea.util.CommentSaver
|
|||||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
@@ -65,7 +65,7 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
|
|||||||
override fun applicabilityRange(element: KtObjectLiteralExpression): TextRange? {
|
override fun applicabilityRange(element: KtObjectLiteralExpression): TextRange? {
|
||||||
val (baseTypeRef, baseType, singleFunction) = extractData(element) ?: return null
|
val (baseTypeRef, baseType, singleFunction) = extractData(element) ?: return null
|
||||||
|
|
||||||
if (!SingleAbstractMethodUtils.isSamType(baseType)) return null
|
if (!JavaSingleAbstractMethodUtils.isSamType(baseType)) return null
|
||||||
|
|
||||||
val functionDescriptor = singleFunction.resolveToDescriptorIfAny(BodyResolveMode.FULL) ?: return null
|
val functionDescriptor = singleFunction.resolveToDescriptorIfAny(BodyResolveMode.FULL) ?: return null
|
||||||
val overridden = functionDescriptor.overriddenDescriptors.singleOrNull() ?: return null
|
val overridden = functionDescriptor.overriddenDescriptors.singleOrNull() ?: return null
|
||||||
|
|||||||
+2
-2
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.idea.core.replaced
|
|||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
|
||||||
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
|
import org.jetbrains.kotlin.load.java.sam.JavaSingleAbstractMethodUtils
|
||||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||||
import org.jetbrains.kotlin.psi.KtLambdaExpression
|
import org.jetbrains.kotlin.psi.KtLambdaExpression
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
@@ -71,7 +71,7 @@ class SamConversionToAnonymousObjectIntention : SelfTargetingRangeIntention<KtCa
|
|||||||
|
|
||||||
private fun KtCallExpression.getSingleAbstractMethod(context: BindingContext): FunctionDescriptor? {
|
private fun KtCallExpression.getSingleAbstractMethod(context: BindingContext): FunctionDescriptor? {
|
||||||
val type = getType(context) ?: return null
|
val type = getType(context) ?: return null
|
||||||
if (!SingleAbstractMethodUtils.isSamType(type)) return null
|
if (!JavaSingleAbstractMethodUtils.isSamType(type)) return null
|
||||||
val javaClass = type.constructor.declarationDescriptor as? JavaClassDescriptor ?: return null
|
val javaClass = type.constructor.declarationDescriptor as? JavaClassDescriptor ?: return null
|
||||||
return getSingleAbstractMethodOrNull(javaClass)
|
return getSingleAbstractMethodOrNull(javaClass)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user