Use safe resolveToDescriptorIfAny() when possible (related to EA-105681)

This commit is contained in:
Mikhail Glukhikh
2017-09-11 11:48:42 +03:00
parent 94e3a51a45
commit 71c2489ade
18 changed files with 47 additions and 46 deletions
@@ -159,7 +159,7 @@ object SourceNavigationHelper {
}
for (candidate in candidates) {
val candidateDescriptor = candidate.resolveToDescriptor() as CallableDescriptor
val candidateDescriptor = candidate.resolveToDescriptorIfAny() as? CallableDescriptor ?: continue
if (receiversMatch(declaration, candidateDescriptor)
&& valueParametersTypesMatch(declaration, candidateDescriptor)
&& typeParametersMatch(declaration as KtTypeParameterListOwner, candidateDescriptor.typeParameters)) {
@@ -27,7 +27,7 @@ import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.*
@@ -81,7 +81,7 @@ class KotlinStructureViewElement(val element: NavigatablePsiElement,
return runReadAction {
if (!DumbService.isDumb(element.getProject())) {
return@runReadAction element.resolveToDescriptor()
return@runReadAction element.resolveToDescriptorIfAny()
}
null
}
@@ -25,7 +25,7 @@ import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.SmartList
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.KotlinIcons
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.completion.*
import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler
import org.jetbrains.kotlin.idea.core.*
@@ -39,7 +39,6 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.isError
@@ -340,7 +339,7 @@ class SmartCompletion(
if (declaration != null) {
val originalDeclaration = toFromOriginalFileMapper.toOriginalFile(declaration)
if (originalDeclaration != null) {
val originalDescriptor = originalDeclaration.resolveToDescriptor(BodyResolveMode.PARTIAL) as? CallableDescriptor
val originalDescriptor = originalDeclaration.resolveToDescriptorIfAny() as? CallableDescriptor
val returnType = originalDescriptor?.returnType
if (returnType != null && !returnType.isError) {
return listOf(ExpectedInfo(returnType, declaration.name, null))
@@ -27,16 +27,17 @@ import com.intellij.openapi.ui.DialogWrapper
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.insertMembersAfter
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
abstract class OverrideImplementMembersHandler : LanguageCodeInsightActionHandler {
fun collectMembersToGenerate(classOrObject: KtClassOrObject): Collection<OverrideMemberChooserObject> {
val descriptor = classOrObject.resolveToDescriptor() as? ClassDescriptor ?: return emptySet()
val descriptor = classOrObject.resolveToDescriptorIfAny(BodyResolveMode.FULL) as? ClassDescriptor ?: return emptySet()
return collectMembersToGenerate(descriptor, classOrObject.project)
}
@@ -24,7 +24,7 @@ import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
@@ -188,7 +188,7 @@ fun KtDeclaration.toDescriptor(): DeclarationDescriptor? {
val bindingContext = analyze()
// TODO: temporary code
if (this is KtPrimaryConstructor) {
return (this.getContainingClassOrObject().resolveToDescriptor() as ClassDescriptor).unsubstitutedPrimaryConstructor
return (this.getContainingClassOrObject().resolveToDescriptorIfAny() as? ClassDescriptor)?.unsubstitutedPrimaryConstructor
}
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this]
@@ -220,7 +220,7 @@ fun KtDeclaration.implicitVisibility(): KtModifierKeywordToken? =
else KtTokens.DEFAULT_VISIBILITY_KEYWORD
}
hasModifier(KtTokens.OVERRIDE_KEYWORD) -> {
(resolveToDescriptor(BodyResolveMode.PARTIAL) as? CallableMemberDescriptor)
(resolveToDescriptorIfAny() as? CallableMemberDescriptor)
?.overriddenDescriptors
?.let { OverridingUtil.findMaxVisibility(it) }
?.toKeywordToken()
@@ -271,7 +271,7 @@ fun KtDeclaration.isOverridable(): Boolean {
}
fun KtDeclaration.getModalityFromDescriptor(): KtModifierKeywordToken? {
val descriptor = this.resolveToDescriptor(BodyResolveMode.PARTIAL)
val descriptor = this.resolveToDescriptorIfAny()
if (descriptor is MemberDescriptor) {
return mapModality(descriptor.modality)
}
@@ -24,7 +24,7 @@ import com.sun.jdi.Location
import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.*
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.getDirectlyOverriddenDeclarations
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.load.java.JvmAbi
@@ -66,9 +66,9 @@ class KotlinBasicStepMethodFilter(
}
if (declaration is KtClass && method.name() == "<init>") {
(declaration.resolveToDescriptor() as? ClassDescriptor)?.unsubstitutedPrimaryConstructor
(declaration.resolveToDescriptorIfAny() as? ClassDescriptor)?.unsubstitutedPrimaryConstructor
} else {
declaration?.resolveToDescriptor()
declaration?.resolveToDescriptorIfAny()
}
} ?: return false // TODO: Check that we can always find a descriptor (libraries with sources, libraries without sources)
@@ -21,7 +21,7 @@ import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.presentation.java.SymbolPresentationUtil
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.renderer.ParameterNameRenderingPolicy
@@ -37,11 +37,13 @@ class KotlinSearchEverywherePsiRenderer(private val list: JList<*>) : DefaultPsi
override fun getElementText(element: PsiElement?): String {
if (element is KtNamedFunction) {
val descriptor = element.resolveToDescriptor() as FunctionDescriptor
return buildString {
descriptor.extensionReceiverParameter?.let { append(RENDERER.renderType(it.type)).append('.') }
append(element.name)
descriptor.valueParameters.joinTo(this, prefix = "(", postfix = ")") { RENDERER.renderType(it.type) }
val descriptor = element.resolveToDescriptorIfAny() as? FunctionDescriptor
if (descriptor != null) {
return buildString {
descriptor.extensionReceiverParameter?.let { append(RENDERER.renderType(it.type)).append('.') }
append(element.name)
descriptor.valueParameters.joinTo(this, prefix = "(", postfix = ")") { RENDERER.renderType(it.type) }
}
}
}
return super.getElementText(element)
@@ -18,7 +18,7 @@ package org.jetbrains.kotlin.idea.highlighter.markers
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.toDescriptor
import org.jetbrains.kotlin.idea.highlighter.sourceKind
import org.jetbrains.kotlin.lexer.KtTokens
@@ -86,7 +86,7 @@ internal fun DeclarationDescriptor.liftToHeader(): DeclarationDescriptor? {
}
internal fun KtDeclaration.liftToHeader(): KtDeclaration? {
val descriptor = resolveToDescriptor()
val headerDescriptor = descriptor.liftToHeader() ?: return null
val descriptor = resolveToDescriptorIfAny()
val headerDescriptor = descriptor?.liftToHeader() ?: return null
return DescriptorToSourceUtils.descriptorToDeclaration(headerDescriptor) as? KtDeclaration
}
@@ -20,7 +20,7 @@ import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.MainFunctionDetector
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.util.addAnnotation
import org.jetbrains.kotlin.idea.util.findAnnotation
import org.jetbrains.kotlin.name.FqName
@@ -37,7 +37,7 @@ class AddJvmStaticIntention : SelfTargetingIntention<KtNamedFunction>(
if (element.findAnnotation(annotationFqName) != null) return false
if (element.isTopLevel) return false
val detector = MainFunctionDetector { function ->
function.resolveToDescriptor() as FunctionDescriptor
function.resolveToDescriptorIfAny() as? FunctionDescriptor
}
return detector.isMain(element, false)
}
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.moveCaret
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.core.unblockDocument
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.idea.util.psi.patternMatching.matches
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import org.jetbrains.kotlin.utils.addToStdlib.lastIsInstanceOrNull
@@ -213,7 +212,7 @@ class InvertIfConditionIntention : SelfTargetingIntention<KtIfExpression>(KtIfEx
is KtNamedFunction -> {
if (parent.bodyExpression == expression) {
if (!parent.hasBlockBody()) return null
val returnType = (parent.resolveToDescriptor(BodyResolveMode.PARTIAL) as FunctionDescriptor).returnType
val returnType = (parent.resolveToDescriptorIfAny() as? FunctionDescriptor)?.returnType
if (returnType == null || !returnType.isUnit()) return null
return KtPsiFactory(expression).createExpression("return")
}
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.descriptors.ParameterDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.util.CommentSaver
@@ -106,7 +106,7 @@ class MovePropertyToConstructorIntention :
}
else {
val typeText = element.typeReference?.text ?:
(element.resolveToDescriptor(BodyResolveMode.PARTIAL) as? PropertyDescriptor)?.type?.render() ?: return
(element.resolveToDescriptorIfAny() as? PropertyDescriptor)?.type?.render() ?: return
val parameterText = buildString {
element.modifierList?.getModifiersText()?.let(this::append)
propertyAnnotationsText?.takeIf(String::isNotBlank)?.let { appendWithSpaceBefore(it) }
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.ShortenReferences
@@ -40,7 +39,6 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.utils.ifEmpty
@@ -88,7 +86,7 @@ class SpecifyTypeExplicitlyIntention :
else -> return null
}
if (declaration.containingClassOrObject?.isLocal ?: false) return null
if (declaration.containingClassOrObject?.isLocal == true) return null
val callable = declaration.resolveToDescriptorIfAny() as? CallableDescriptor ?: return null
if (publicAPIOnly && !callable.visibility.isPublicAPI) return null
@@ -103,7 +101,7 @@ class SpecifyTypeExplicitlyIntention :
}
fun getTypeForDeclaration(declaration: KtCallableDeclaration): KotlinType {
val descriptor = declaration.analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, declaration]
val descriptor = declaration.resolveToDescriptorIfAny()
val type = (descriptor as? CallableDescriptor)?.returnType
if (type != null && type.isError && descriptor is PropertyDescriptor) {
return descriptor.setterType ?: ErrorUtils.createErrorType("null type")
@@ -18,8 +18,9 @@ package org.jetbrains.kotlin.idea.j2k
import org.jetbrains.kotlin.j2k.ResolverForConverter
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
object IdeaResolverForConverter : ResolverForConverter {
override fun resolveToDescriptor(declaration: KtDeclaration) = declaration.resolveToDescriptor()
override fun resolveToDescriptor(declaration: KtDeclaration) = declaration.resolveToDescriptorIfAny(BodyResolveMode.FULL)
}
@@ -26,7 +26,7 @@ import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.IterableTypesDetection
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.resolve.ideService
@@ -74,7 +74,7 @@ class SuggestVariableNameMacro : KotlinMacro() {
suggestIterationVariableName(parent, nameValidator)?.let { return it }
}
val descriptor = declaration.resolveToDescriptor(BodyResolveMode.PARTIAL) as? VariableDescriptor ?: return emptyList()
val descriptor = declaration.resolveToDescriptorIfAny() as? VariableDescriptor ?: return emptyList()
return KotlinNameSuggester.suggestNamesByType(descriptor.type, nameValidator, null)
}
@@ -43,7 +43,7 @@ import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult
import org.jetbrains.kotlin.idea.caches.resolve.getJavaClassDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.imports.importableFqName
@@ -619,7 +619,7 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
private fun postprocessDeclaration(declaration: KtNamedDeclaration) {
if (callableInfo is PropertyInfo && callableInfo.isLateinitPreferred) {
if (declaration.containingClassOrObject == null) return
val propertyDescriptor = declaration.resolveToDescriptor() as? PropertyDescriptor ?: return
val propertyDescriptor = declaration.resolveToDescriptorIfAny() as? PropertyDescriptor ?: return
val returnType = propertyDescriptor.returnType ?: return
if (TypeUtils.isNullableType(returnType) || KotlinBuiltIns.isPrimitiveType(returnType)) return
declaration.addModifier(KtTokens.LATEINIT_KEYWORD)
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder
import com.intellij.psi.PsiElement
import com.intellij.util.ArrayUtil
import org.jetbrains.kotlin.descriptors.ClassDescriptorWithResolutionScopes
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.ClassInfo
import org.jetbrains.kotlin.idea.util.getResolutionScope
@@ -104,7 +104,7 @@ abstract class TypeInfo(val variance: Variance) {
}
}
return when (containingElement) {
is KtClassOrObject -> (containingElement.resolveToDescriptor() as? ClassDescriptorWithResolutionScopes)?.scopeForMemberDeclarationResolution
is KtClassOrObject -> (containingElement.resolveToDescriptorIfAny() as? ClassDescriptorWithResolutionScopes)?.scopeForMemberDeclarationResolution
is KtBlockExpression -> (containingElement.statements.firstOrNull() ?: containingElement).getResolutionScope()
is KtElement -> containingElement.containingKtFile.getResolutionScope()
else -> null
@@ -20,7 +20,7 @@ import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.CallableInfo
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.ParameterInfo
@@ -45,7 +45,7 @@ object CreateConstructorFromDelegationCallActionFactory : CreateCallableMemberFr
val project = currentClass.project
val classDescriptor = currentClass.resolveToDescriptor() as? ClassDescriptor ?: return null
val classDescriptor = currentClass.resolveToDescriptorIfAny() as? ClassDescriptor ?: return null
val targetClass = if (calleeExpression.isThis) {
currentClass
@@ -73,6 +73,7 @@ import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getJavaMemberDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.core.util.showYesNoCancelDialog
@@ -783,7 +784,7 @@ fun KtExpression.removeTemplateEntryBracesIfPossible(): KtExpression {
}
fun dropOverrideKeywordIfNecessary(element: KtNamedDeclaration) {
val callableDescriptor = element.resolveToDescriptor(BodyResolveMode.PARTIAL) as? CallableDescriptor ?: return
val callableDescriptor = element.resolveToDescriptorIfAny() as? CallableDescriptor ?: return
if (callableDescriptor.overriddenDescriptors.isEmpty()) {
element.removeModifier(KtTokens.OVERRIDE_KEYWORD)
}