Search in sources :

Example 16 with GroovyRecursiveElementVisitor

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

the class GroovyDuplicateSwitchBranchInspection method collectCaseLabels.

private static GrCaseLabel[] collectCaseLabels(final GrSwitchStatement containingStatelent) {
    final Set<GrCaseLabel> labels = new HashSet<>();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitCaseLabel(@NotNull GrCaseLabel grCaseLabel) {
            super.visitCaseLabel(grCaseLabel);
            labels.add(grCaseLabel);
        }

        @Override
        public void visitSwitchStatement(@NotNull GrSwitchStatement grSwitchStatement) {
            if (containingStatelent.equals(grSwitchStatement)) {
                super.visitSwitchStatement(grSwitchStatement);
            }
        }
    };
    containingStatelent.accept(visitor);
    return labels.toArray(new GrCaseLabel[labels.size()]);
}
Also used : GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrSwitchStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement) NotNull(org.jetbrains.annotations.NotNull) HashSet(java.util.HashSet)

Example 17 with GroovyRecursiveElementVisitor

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

the class GrReassignedLocalVarsChecker method isReassignedVarImpl.

private static boolean isReassignedVarImpl(@NotNull final GrVariable resolved) {
    final GrControlFlowOwner variableScope = PsiTreeUtil.getParentOfType(resolved, GrCodeBlock.class, GroovyFile.class);
    if (variableScope == null)
        return false;
    final String name = resolved.getName();
    final Ref<Boolean> isReassigned = Ref.create(false);
    for (PsiElement scope = resolved.getParent().getNextSibling(); scope != null; scope = scope.getNextSibling()) {
        if (scope instanceof GroovyPsiElement) {
            ((GroovyPsiElement) scope).accept(new GroovyRecursiveElementVisitor() {

                @Override
                public void visitClosure(@NotNull GrClosableBlock closure) {
                    if (getUsedVarsInsideBlock(closure).contains(name)) {
                        isReassigned.set(true);
                    }
                }

                @Override
                public void visitElement(@NotNull GroovyPsiElement element) {
                    if (isReassigned.get())
                        return;
                    super.visitElement(element);
                }
            });
            if (isReassigned.get())
                break;
        }
    }
    return isReassigned.get();
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 18 with GroovyRecursiveElementVisitor

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

the class ImportStaticIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, final Editor editor) throws IncorrectOperationException {
    final PsiElement resolved = resolve(element);
    if (!(resolved instanceof PsiMember))
        return;
    final PsiClass containingClass = ((PsiMember) resolved).getContainingClass();
    if (containingClass == null)
        return;
    String originalName = ((PsiMember) resolved).getName();
    final String name = resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod) resolved) ? GroovyPropertyUtils.getPropertyName((PsiMethod) resolved) : originalName;
    final String qname = containingClass.getQualifiedName();
    if (name == null)
        return;
    final PsiFile containingFile = element.getContainingFile();
    if (!(containingFile instanceof GroovyFile))
        return;
    final GroovyFile file = (GroovyFile) containingFile;
    file.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            if (name.equals(expression.getReferenceName())) {
                PsiElement resolved = expression.resolve();
                if (resolved != null) {
                    expression.putUserData(TEMP_REFERENT_USER_DATA, resolved);
                }
            }
        }
    });
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final GrImportStatement tempImport = factory.createImportStatementFromText(qname + "." + name, true, false, null);
    final GrImportStatement importStatement = file.addImport(tempImport);
    boolean isAnythingShortened = shortenUsages(resolved, containingFile);
    if (!isAnythingShortened) {
        importStatement.delete();
        return;
    }
    file.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            GrTypeArgumentList typeArgumentList = expression.getTypeArgumentList();
            if (typeArgumentList != null && typeArgumentList.getFirstChild() != null) {
                expression.putUserData(TEMP_REFERENT_USER_DATA, null);
                return;
            }
            if (name.equals(expression.getReferenceName())) {
                if (expression.isQualified()) {
                    GrExpression qualifier = expression.getQualifierExpression();
                    if (qualifier instanceof GrReferenceExpression) {
                        PsiElement aClass = ((GrReferenceExpression) qualifier).resolve();
                        if (aClass == ((PsiMember) resolved).getContainingClass()) {
                            GrReferenceAdjuster.shortenReference(expression);
                        }
                    }
                } else {
                    PsiElement referent = expression.getUserData(TEMP_REFERENT_USER_DATA);
                    if (referent instanceof PsiMember && ((PsiMember) referent).hasModifierProperty(PsiModifier.STATIC) && referent != expression.resolve()) {
                        expression.bindToElement(referent);
                    }
                }
            }
            expression.putUserData(TEMP_REFERENT_USER_DATA, null);
        }
    });
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 19 with GroovyRecursiveElementVisitor

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

the class ConvertJunitAssertionToAssertStatementIntention method getReplacementElement.

@Nullable
private static GrStatement getReplacementElement(@NotNull PsiMethod method, @NotNull GrMethodCall methodCall) {
    String replacementStatement = getReplacementStatement(method, methodCall);
    if (replacementStatement == null)
        return null;
    GrExpression[] arguments = methodCall.getArgumentList().getExpressionArguments();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    GrAssertStatement statement = (GrAssertStatement) factory.createStatementFromText(replacementStatement);
    final Map<GrExpression, GrExpression> replaceMap = new HashMap<>();
    statement.acceptChildren(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitExpression(@NotNull GrExpression expression) {
            Matcher matcher = PATTERN.matcher(expression.getText());
            if (matcher.matches()) {
                int index = Integer.parseInt(matcher.group(1));
                replaceMap.put(expression, arguments[index]);
            } else {
                super.visitExpression(expression);
            }
        }
    });
    for (Map.Entry<GrExpression, GrExpression> entry : replaceMap.entrySet()) {
        entry.getKey().replaceWithExpression(entry.getValue(), true);
    }
    return statement;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrAssertStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GroovyRecursiveElementVisitor

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

the class GroovyExtractMethodHandler method findConflicts.

private static boolean findConflicts(InitialInfo info) {
    //new ConflictsDialog()
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final PsiElement declarationOwner = info.getContext().getParent();
    GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
            PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
            if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
                conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
            }
        }

        //skip 'print' and 'println'
        private boolean skipResult(GroovyResolveResult result) {
            PsiElement element = result.getElement();
            if (element instanceof PsiMethod) {
                String name = ((PsiMethod) element).getName();
                if (!name.startsWith("print"))
                    return false;
                if (element instanceof GrGdkMethod)
                    element = ((GrGdkMethod) element).getStaticMethod();
                PsiClass aClass = ((PsiMethod) element).getContainingClass();
                if (aClass != null) {
                    String qname = aClass.getQualifiedName();
                    return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
                }
            }
            return false;
        }
    };
    GrStatement[] statements = info.getStatements();
    for (GrStatement statement : statements) {
        statement.accept(visitor);
    }
    if (conflicts.isEmpty())
        return false;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
    }
    ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
    dialog.show();
    return !dialog.isOK();
}
Also used : GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog)

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