Search in sources :

Example 36 with GrVariable

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

the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.

private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
    PsiElement parent = element.getParent();
    if (parent instanceof GrAnonymousClassDefinition) {
        parent = parent.getParent();
    }
    if (parent instanceof GrNewExpression) {
        return newExpressionProcessor.process((GrNewExpression) parent);
    }
    if (parent instanceof GrTypeElement) {
        final GrTypeElement typeElement = (GrTypeElement) parent;
        final PsiElement grandpa = typeElement.getParent();
        if (grandpa instanceof GrVariableDeclaration) {
            final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
            if (vars.length == 1) {
                final GrVariable variable = vars[0];
                if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
                    return false;
                }
            }
        } else if (grandpa instanceof GrMethod) {
            final GrMethod method = (GrMethod) grandpa;
            if (typeElement == method.getReturnTypeElementGroovy()) {
                ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {

                    @Override
                    public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
                        if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
                            return false;
                        }
                        return true;
                    }
                });
            }
        } else if (grandpa instanceof GrTypeCastExpression) {
            final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        } else if (grandpa instanceof GrSafeCastExpression) {
            final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with GrVariable

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

the class UnusedDefInspection method isUsedInTopLevelFlowOnly.

private static boolean isUsedInTopLevelFlowOnly(PsiElement element) {
    GrVariable var = null;
    if (element instanceof GrVariable) {
        var = (GrVariable) element;
    } else if (element instanceof GrReferenceExpression) {
        final PsiElement resolved = ((GrReferenceExpression) element).resolve();
        if (resolved instanceof GrVariable)
            var = (GrVariable) resolved;
    }
    if (var != null) {
        final GroovyPsiElement scope = ControlFlowUtils.findControlFlowOwner(var);
        if (scope == null) {
            PsiFile file = var.getContainingFile();
            if (file == null) {
                LOG.error("no file??? var of type" + var.getClass().getCanonicalName());
                return false;
            } else {
                TextRange range = var.getTextRange();
                LOG.error("var: " + var.getName() + ", offset:" + (range != null ? range.getStartOffset() : -1));
                return false;
            }
        }
        return ReferencesSearch.search(var, var.getUseScope()).forEach(ref -> ControlFlowUtils.findControlFlowOwner(ref.getElement()) == scope);
    }
    return true;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 38 with GrVariable

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

the class GrDocCommentUtil method findDocComment.

@Nullable
public static GrDocComment findDocComment(GrDocCommentOwner owner) {
    if (owner.getFirstChild() instanceof GrDocComment) {
        return ((GrDocComment) owner.getFirstChild());
    }
    PsiElement element = owner instanceof GrVariable && owner.getParent() instanceof GrVariableDeclaration ? owner.getParent() : owner;
    element = skipWhiteSpacesAndStopOnDoc(element, false);
    if (element instanceof GrDocComment)
        return (GrDocComment) element;
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElement(com.intellij.psi.PsiElement) GroovyDocPsiElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GroovyDocPsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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 40 with GrVariable

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

the class GrReferenceExpressionImpl method calculateType.

@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
    final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
    final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
    final PsiElement resolved = result.getElement();
    for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
        PsiType type = calculator.calculateType(refExpr, resolved);
        if (type != null)
            return type;
    }
    if (ResolveUtil.isClassReference(refExpr)) {
        GrExpression qualifier = refExpr.getQualifier();
        LOG.assertTrue(qualifier != null);
        return qualifier.getType();
    }
    if (PsiUtil.isCompileStatic(refExpr)) {
        final PsiType type;
        if (resolved instanceof GrField) {
            type = ((GrField) resolved).getType();
        } else if (resolved instanceof GrVariable) {
            type = ((GrVariable) resolved).getDeclaredType();
        } else if (resolved instanceof GrAccessorMethod) {
            type = ((GrAccessorMethod) resolved).getProperty().getType();
        } else {
            type = null;
        }
        if (type != null) {
            return result.getSubstitutor().substitute(type);
        }
    }
    final PsiType nominal = refExpr.getNominalType(forceRValue);
    Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
    if (reassigned != null && reassigned.booleanValue()) {
        return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
    }
    final PsiType inferred = getInferredTypes(refExpr, resolved);
    if (inferred == null) {
        if (nominal == null) {
            //inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
            if (resolved instanceof GrVariable) {
                LOG.assertTrue(resolved.isValid());
                return ((GrVariable) resolved).getTypeGroovy();
            }
        }
        return nominal;
    }
    if (nominal == null)
        return inferred;
    if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
        if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
            return nominal;
        }
    }
    return inferred;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpressionTypeCalculator(org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrExpressionTypeCalculator) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5