Search in sources :

Example 6 with GroovyRecursiveElementVisitor

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

the class FieldConflictsResolver method fixInitializer.

public GrExpression fixInitializer(GrExpression initializer) {
    if (myField == null)
        return initializer;
    final GrReferenceExpression[] replacedRef = { null };
    initializer.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
            final GrExpression qualifierExpression = expression.getQualifier();
            if (qualifierExpression != null) {
                qualifierExpression.accept(this);
            } else {
                final PsiElement result = expression.resolve();
                if (expression.getManager().areElementsEquivalent(result, myField)) {
                    try {
                        replacedRef[0] = qualifyReference(expression, myField, myQualifyingClass);
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                    }
                }
            }
        }
    });
    if (!initializer.isValid())
        return replacedRef[0];
    return initializer;
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 7 with GroovyRecursiveElementVisitor

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

the class AnnotatedMembersSearcher method execute.

@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
    final PsiClass annClass = p.getAnnotationClass();
    assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";
    final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Override
        public String compute() {
            return annClass.getQualifiedName();
        }
    });
    assert annotationFQN != null;
    final SearchScope scope = p.getScope();
    final List<PsiModifierListOwner> candidates;
    if (scope instanceof GlobalSearchScope) {
        candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope) scope));
    } else {
        candidates = new ArrayList<>();
        for (final PsiElement element : ((LocalSearchScope) scope).getScope()) {
            ApplicationManager.getApplication().runReadAction(() -> {
                if (element instanceof GroovyPsiElement) {
                    ((GroovyPsiElement) element).accept(new GroovyRecursiveElementVisitor() {

                        @Override
                        public void visitMethod(@NotNull GrMethod method) {
                            candidates.add(method);
                        }

                        @Override
                        public void visitField(@NotNull GrField field) {
                            candidates.add(field);
                        }
                    });
                }
            });
        }
    }
    for (final PsiModifierListOwner candidate : candidates) {
        boolean accepted = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

            @Override
            public Boolean compute() {
                if (AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
                    PsiModifierList list = candidate.getModifierList();
                    if (list != null) {
                        for (PsiAnnotation annotation : list.getAnnotations()) {
                            if ((p.isApproximate() || annotationFQN.equals(annotation.getQualifiedName())) && !consumer.process(candidate)) {
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
        });
        if (!accepted)
            return false;
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 8 with GroovyRecursiveElementVisitor

use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor 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 9 with GroovyRecursiveElementVisitor

use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor 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 10 with GroovyRecursiveElementVisitor

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

the class ControlFlowBuilder method collectUsedVariableWithoutInitialization.

private static Set<ReadWriteVariableInstruction> collectUsedVariableWithoutInitialization(GrTypeDefinition typeDefinition) {
    final Set<ReadWriteVariableInstruction> vars = ContainerUtil.newLinkedHashSet();
    typeDefinition.acceptChildren(new GroovyRecursiveElementVisitor() {

        private void collectVars(Instruction[] flow) {
            ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, false);
            Collections.addAll(vars, reads);
        }

        @Override
        public void visitField(@NotNull GrField field) {
            GrExpression initializer = field.getInitializerGroovy();
            if (initializer != null) {
                Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
                collectVars(flow);
            }
        }

        @Override
        public void visitMethod(@NotNull GrMethod method) {
            GrOpenBlock block = method.getBlock();
            if (block != null) {
                collectVars(block.getControlFlow());
            }
        }

        @Override
        public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
            GrOpenBlock block = initializer.getBlock();
            collectVars(block.getControlFlow());
        }
    });
    return vars;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Aggregations

GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)8 PsiElement (com.intellij.psi.PsiElement)7 NotNull (org.jetbrains.annotations.NotNull)7 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)3 TextRange (com.intellij.openapi.util.TextRange)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 UsageInfo (com.intellij.usageView.UsageInfo)2 ArrayList (java.util.ArrayList)2