Search in sources :

Example 1 with KotlinCodeAnalyzer

use of org.jetbrains.kotlin.resolve.lazy.KotlinCodeAnalyzer in project kotlin by JetBrains.

the class SourceNavigationHelper method convertPropertyOrFunction.

@Nullable
private static KtNamedDeclaration convertPropertyOrFunction(@NotNull KtNamedDeclaration declaration, @NotNull NavigationKind navigationKind) {
    if (declaration instanceof KtPrimaryConstructor) {
        KtClassOrObject sourceClassOrObject = findClassOrObject(((KtPrimaryConstructor) declaration).getContainingClassOrObject(), navigationKind);
        KtPrimaryConstructor primaryConstructor = sourceClassOrObject != null ? sourceClassOrObject.getPrimaryConstructor() : null;
        return primaryConstructor != null ? primaryConstructor : sourceClassOrObject;
    }
    String memberNameAsString = declaration.getName();
    if (memberNameAsString == null) {
        LOG.debug("JetSourceNavigationHelper.convertPropertyOrFunction(): null name for declaration " + declaration);
        return null;
    }
    Name memberName = Name.identifier(memberNameAsString);
    PsiElement decompiledContainer = declaration.getParent();
    Collection<KtNamedDeclaration> candidates;
    if (decompiledContainer instanceof KtFile) {
        candidates = getInitialTopLevelCandidates(declaration, navigationKind);
    } else if (decompiledContainer instanceof KtClassBody) {
        KtClassOrObject decompiledClassOrObject = (KtClassOrObject) decompiledContainer.getParent();
        KtClassOrObject sourceClassOrObject = findClassOrObject(decompiledClassOrObject, navigationKind);
        //noinspection unchecked
        candidates = sourceClassOrObject == null ? Collections.<KtNamedDeclaration>emptyList() : getInitialMemberCandidates(sourceClassOrObject, memberName, (Class<KtNamedDeclaration>) declaration.getClass());
        if (candidates.isEmpty()) {
            if (declaration instanceof KtProperty && sourceClassOrObject instanceof KtClass) {
                return findSpecialProperty(memberName, (KtClass) sourceClassOrObject);
            }
        }
    } else {
        throw new IllegalStateException("Unexpected container of " + (navigationKind == NavigationKind.CLASS_FILES_TO_SOURCES ? "decompiled" : "source") + " declaration: " + decompiledContainer.getClass().getSimpleName());
    }
    if (candidates.isEmpty()) {
        return null;
    }
    candidates = filterByOrderEntries(declaration, candidates);
    if (!forceResolve) {
        candidates = filterByReceiverPresenceAndParametersCount(declaration, candidates);
        if (candidates.size() <= 1) {
            return candidates.isEmpty() ? null : candidates.iterator().next();
        }
        if (!haveRenamesInImports(getContainingFiles(candidates))) {
            candidates = filterByReceiverAndParameterTypes(declaration, candidates);
            if (candidates.size() <= 1) {
                return candidates.isEmpty() ? null : candidates.iterator().next();
            }
        }
    }
    KotlinCodeAnalyzer analyzer = createAnalyzer(candidates, declaration.getProject());
    for (KtNamedDeclaration candidate : candidates) {
        //noinspection unchecked
        CallableDescriptor candidateDescriptor = (CallableDescriptor) analyzer.resolveToDescriptor(candidate);
        if (receiversMatch(declaration, candidateDescriptor) && valueParametersTypesMatch(declaration, candidateDescriptor) && typeParametersMatch((KtTypeParameterListOwner) declaration, candidateDescriptor.getTypeParameters())) {
            return candidate;
        }
    }
    return null;
}
Also used : KotlinCodeAnalyzer(org.jetbrains.kotlin.resolve.lazy.KotlinCodeAnalyzer) FqName(org.jetbrains.kotlin.name.FqName) Name(org.jetbrains.kotlin.name.Name) PsiClass(com.intellij.psi.PsiClass) LightClassUtilsKt.toLightClass(org.jetbrains.kotlin.asJava.LightClassUtilsKt.toLightClass) CallableDescriptor(org.jetbrains.kotlin.descriptors.CallableDescriptor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiClass (com.intellij.psi.PsiClass)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1 LightClassUtilsKt.toLightClass (org.jetbrains.kotlin.asJava.LightClassUtilsKt.toLightClass)1 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)1 FqName (org.jetbrains.kotlin.name.FqName)1 Name (org.jetbrains.kotlin.name.Name)1 KotlinCodeAnalyzer (org.jetbrains.kotlin.resolve.lazy.KotlinCodeAnalyzer)1