Search in sources :

Example 76 with GrReferenceExpression

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

the class GrReassignedLocalVarsChecker method getUsedVarsInsideBlock.

@NotNull
private static Set<String> getUsedVarsInsideBlock(@NotNull final GrCodeBlock block) {
    return CachedValuesManager.getCachedValue(block, () -> {
        final Set<String> result = ContainerUtil.newHashSet();
        block.acceptChildren(new GroovyRecursiveElementVisitor() {

            @Override
            public void visitOpenBlock(@NotNull GrOpenBlock openBlock) {
                result.addAll(getUsedVarsInsideBlock(openBlock));
            }

            @Override
            public void visitClosure(@NotNull GrClosableBlock closure) {
                result.addAll(getUsedVarsInsideBlock(closure));
            }

            @Override
            public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
                if (referenceExpression.getQualifier() == null && referenceExpression.getReferenceName() != null) {
                    result.add(referenceExpression.getReferenceName());
                }
            }
        });
        return CachedValueProvider.Result.create(result, block);
    });
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 77 with GrReferenceExpression

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

the class ReachingDefinitionsCollector method addClosureUsages.

private static void addClosureUsages(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrStatement first, final GrStatement last, GrControlFlowOwner flowOwner) {
    flowOwner.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitClosure(@NotNull GrClosableBlock closure) {
            addUsagesInClosure(imap, omap, closure, first, last);
            super.visitClosure(closure);
        }

        private void addUsagesInClosure(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrClosableBlock closure, final GrStatement first, final GrStatement last) {
            closure.accept(new GroovyRecursiveElementVisitor() {

                @Override
                public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
                    if (refExpr.isQualified()) {
                        return;
                    }
                    PsiElement resolved = refExpr.resolve();
                    if (!(resolved instanceof GrVariable)) {
                        return;
                    }
                    GrVariable variable = (GrVariable) resolved;
                    if (PsiTreeUtil.isAncestor(closure, variable, true)) {
                        return;
                    }
                    if (variable instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(closure, ((ClosureSyntheticParameter) variable).getClosure(), false)) {
                        return;
                    }
                    String name = variable.getName();
                    if (!(variable instanceof GrField)) {
                        if (!isInFragment(first, last, resolved)) {
                            if (isInFragment(first, last, closure)) {
                                addVariable(name, imap, variable.getManager(), variable.getType());
                            }
                        } else {
                            if (!isInFragment(first, last, closure)) {
                                addVariable(name, omap, variable.getManager(), variable.getType());
                            }
                        }
                    }
                }
            });
        }
    });
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) NotNull(org.jetbrains.annotations.NotNull) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 78 with GrReferenceExpression

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

the class GrReferenceExpressionImpl method bindWithQualifiedRef.

@Override
protected GrReferenceExpression bindWithQualifiedRef(@NotNull String qName) {
    GrReferenceExpression qualifiedRef = GroovyPsiElementFactory.getInstance(getProject()).createReferenceExpressionFromText(qName);
    final GrTypeArgumentList list = getTypeArgumentList();
    if (list != null) {
        qualifiedRef.getNode().addChild(list.copy().getNode());
    }
    getNode().getTreeParent().replaceChild(getNode(), qualifiedRef.getNode());
    return qualifiedRef;
}
Also used : GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 79 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression 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)

Example 80 with GrReferenceExpression

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

the class DefaultCallExpressionTypeCalculator method calculateReturnType.

@Override
public PsiType calculateReturnType(@NotNull GrMethodCall callExpression, GroovyResolveResult[] resolveResults) {
    GrExpression invoked = callExpression.getInvokedExpression();
    if (invoked instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) invoked;
        PsiManager manager = callExpression.getManager();
        PsiType result = null;
        for (GroovyResolveResult resolveResult : resolveResults) {
            PsiType returnType = calculateReturnTypeInner(callExpression, refExpr, resolveResult);
            if (returnType == null)
                return null;
            PsiType nonVoid = PsiType.VOID.equals(returnType) && !isCompileStatic(callExpression) ? PsiType.NULL : returnType;
            PsiType normalized = nonVoid instanceof GrLiteralClassType ? nonVoid : TypesUtil.substituteAndNormalizeType(nonVoid, resolveResult.getSubstitutor(), resolveResult.getSpreadState(), callExpression);
            LOG.assertTrue(normalized != null, "return type: " + returnType + "; substitutor: " + resolveResult.getSubstitutor());
            if (result == null || normalized.isAssignableFrom(result)) {
                result = normalized;
            } else if (!result.isAssignableFrom(normalized)) {
                result = TypesUtil.getLeastUpperBound(result, normalized, manager);
            }
        }
        return result;
    } else {
        return extractReturnTypeFromType(invoked.getType(), false, callExpression);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteralClassType(org.jetbrains.plugins.groovy.lang.psi.impl.GrLiteralClassType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11