Search in sources :

Example 56 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.

the class GrReferenceExpressionImpl method getNominalType.

@Nullable
private PsiType getNominalType(boolean forceRValue) {
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(multiResolve(false, forceRValue));
    PsiElement resolved = resolveResult.getElement();
    for (GrReferenceTypeEnhancer enhancer : GrReferenceTypeEnhancer.EP_NAME.getExtensions()) {
        PsiType type = enhancer.getReferenceType(this, resolved);
        if (type != null) {
            return type;
        }
    }
    IElementType dotType = getDotTokenType();
    if (dotType == GroovyTokenTypes.mMEMBER_POINTER) {
        return GrClosureType.create(multiResolve(false), this);
    }
    if (ResolveUtil.isDefinitelyKeyOfMap(this)) {
        final PsiType type = getTypeFromMapAccess(this);
        if (type != null) {
            return type;
        }
    }
    PsiType result = getNominalTypeInner(resolved);
    if (result == null)
        return null;
    result = TypesUtil.substituteAndNormalizeType(result, resolveResult.getSubstitutor(), resolveResult.getSpreadState(), this);
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrReferenceTypeEnhancer(org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrReferenceTypeEnhancer) Nullable(org.jetbrains.annotations.Nullable)

Example 57 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.

the class GrReferenceExpressionImpl method isReferenceTo.

@Override
public boolean isReferenceTo(PsiElement element) {
    GroovyResolveResult[] results = multiResolve(false);
    for (GroovyResolveResult result : results) {
        if (!result.isValidResult())
            continue;
        PsiElement baseTarget = result.getElement();
        if (baseTarget == null)
            continue;
        if (getManager().areElementsEquivalent(element, baseTarget)) {
            return true;
        }
        PsiElement target = GroovyTargetElementEvaluator.correctSearchTargets(baseTarget);
        if (target != baseTarget && getManager().areElementsEquivalent(element, target)) {
            return true;
        }
        if (element instanceof PsiMethod && target instanceof PsiMethod) {
            PsiMethod[] superMethods = ((PsiMethod) target).findSuperMethods(false);
            //noinspection SuspiciousMethodCalls
            if (Arrays.asList(superMethods).contains(element)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)

Example 58 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.

the class GrReferenceExpressionImpl method doPolyResolve.

@NotNull
GroovyResolveResult[] doPolyResolve(boolean incompleteCode, boolean forceRValue) {
    final PsiElement nameElement = getReferenceNameElement();
    final String name = getReferenceName();
    if (name == null || nameElement == null)
        return GroovyResolveResult.EMPTY_ARRAY;
    try {
        ResolveProfiler.start();
        boolean canBeMethod = ResolveUtil.canResolveToMethod(this);
        if (!canBeMethod) {
            if (ResolveUtil.isDefinitelyKeyOfMap(this))
                return GroovyResolveResult.EMPTY_ARRAY;
            final IElementType nameType = nameElement.getNode().getElementType();
            if (nameType == GroovyTokenTypes.kTHIS) {
                final GroovyResolveResult[] results = GrThisReferenceResolver.resolveThisExpression(this);
                if (results != null)
                    return results;
            } else if (nameType == GroovyTokenTypes.kSUPER) {
                final GroovyResolveResult[] results = GrSuperReferenceResolver.resolveSuperExpression(this);
                if (results != null)
                    return results;
            }
        }
        final GroovyResolveResult[] results = resolveReferenceExpression(this, forceRValue, incompleteCode);
        if (results.length == 0) {
            return GroovyResolveResult.EMPTY_ARRAY;
        } else if (!canBeMethod) {
            if (!ResolveUtil.mayBeKeyOfMap(this)) {
                return results;
            } else {
                //filter out all members from super classes. We should return only accessible members from map classes
                final List<GroovyResolveResult> filtered = filterMembersFromSuperClasses(results);
                return ContainerUtil.toArray(filtered, new GroovyResolveResult[filtered.size()]);
            }
        } else {
            return results;
        }
    } finally {
        final long time = ResolveProfiler.finish();
        ResolveProfiler.write("ref", this, time);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) SmartList(com.intellij.util.SmartList) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) NotNull(org.jetbrains.annotations.NotNull)

Example 59 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.

the class GrReferenceExpressionImpl method filterMembersFromSuperClasses.

@NotNull
private static List<GroovyResolveResult> filterMembersFromSuperClasses(GroovyResolveResult[] results) {
    List<GroovyResolveResult> filtered = new ArrayList<>();
    for (GroovyResolveResult result : results) {
        final PsiElement element = result.getElement();
        if (element instanceof PsiMember) {
            if (((PsiMember) element).hasModifierProperty(PsiModifier.PRIVATE))
                continue;
            final PsiClass containingClass = ((PsiMember) element).getContainingClass();
            if (containingClass != null) {
                if (!InheritanceUtil.isInheritor(containingClass, CommonClassNames.JAVA_UTIL_MAP))
                    continue;
                final String name = containingClass.getQualifiedName();
                if (name != null && name.startsWith("java."))
                    continue;
                if (containingClass.getLanguage() != GroovyLanguage.INSTANCE && !InheritanceUtil.isInheritor(containingClass, GroovyCommonClassNames.GROOVY_OBJECT)) {
                    continue;
                }
            }
        }
        filtered.add(result);
    }
    return filtered;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) NotNull(org.jetbrains.annotations.NotNull)

Example 60 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.

the class GrSuperReferenceResolver method resolveSuperExpression.

@Nullable("null if ref is not 'super' reference")
public static GroovyResolveResult[] resolveSuperExpression(@NotNull GrReferenceExpression ref) {
    GrExpression qualifier = ref.getQualifier();
    if (qualifier == null) {
        final PsiElement parent = ref.getParent();
        if (parent instanceof GrConstructorInvocation) {
            return ((GrConstructorInvocation) parent).multiResolve(false);
        }
        PsiClass aClass = PsiUtil.getContextClass(ref);
        if (aClass != null) {
            return getSuperClass(aClass);
        }
    } else if (qualifier instanceof GrReferenceExpression) {
        GroovyResolveResult result = ((GrReferenceExpression) qualifier).advancedResolve();
        PsiElement resolved = result.getElement();
        if (resolved instanceof PsiClass) {
            PsiClass superClass = (PsiClass) resolved;
            GrTypeDefinition scopeClass = PsiTreeUtil.getParentOfType(ref, GrTypeDefinition.class, true);
            if (scopeClass != null && GrTraitUtil.isTrait(superClass) && scopeClass.isInheritor(superClass, false)) {
                PsiSubstitutor superClassSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, scopeClass, PsiSubstitutor.EMPTY);
                return new GroovyResolveResultImpl[] { new GroovyResolveResultImpl(superClass, null, null, superClassSubstitutor, true, true) };
            }
            if (PsiUtil.hasEnclosingInstanceInScope(superClass, ref, false)) {
                return getSuperClass(superClass);
            }
        }
    }
    return null;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass) PsiSubstitutor(com.intellij.psi.PsiSubstitutor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)132 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)33 Nullable (org.jetbrains.annotations.Nullable)29 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)29 NotNull (org.jetbrains.annotations.NotNull)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)23 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)14 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)13 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 IElementType (com.intellij.psi.tree.IElementType)7 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)7 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)7 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)7 Project (com.intellij.openapi.project.Project)6