Search in sources :

Example 26 with GrReferenceExpression

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

the class GroovyInlineLocalHandler method createSettings.

/**
   * Returns Settings object for referenced definition in case of local variable
   */
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
    final String localName = variable.getName();
    final Project project = variable.getProject();
    GrExpression initializer = null;
    Instruction writeInstr = null;
    Instruction[] flow = null;
    //search for initializer to inline
    if (invokedOnReference) {
        LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
        final PsiReference ref = TargetElementUtil.findReference(editor);
        LOG.assertTrue(ref != null);
        PsiElement cur = ref.getElement();
        if (cur instanceof GrReferenceExpression) {
            GrControlFlowOwner controlFlowOwner;
            do {
                controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
                if (controlFlowOwner == null)
                    break;
                flow = controlFlowOwner.getControlFlow();
                final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
                final PsiElement finalCur = cur;
                Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
                LOG.assertTrue(instruction != null);
                final BitSet prev = writes.get(instruction.num());
                if (prev.cardinality() == 1) {
                    writeInstr = flow[prev.nextSetBit(0)];
                    final PsiElement element = writeInstr.getElement();
                    if (element instanceof GrVariable) {
                        initializer = ((GrVariable) element).getInitializerGroovy();
                    } else if (element instanceof GrReferenceExpression) {
                        initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
                    }
                }
                PsiElement old_cur = cur;
                if (controlFlowOwner instanceof GrClosableBlock) {
                    cur = controlFlowOwner;
                } else {
                    PsiElement parent = controlFlowOwner.getParent();
                    if (parent instanceof GrMember)
                        cur = ((GrMember) parent).getContainingClass();
                }
                if (cur == old_cur)
                    break;
            } while (initializer == null);
        }
    } else {
        flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
        initializer = variable.getInitializerGroovy();
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
    }
    if (initializer == null || writeInstr == null) {
        String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
        CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
        return null;
    }
    int writeInstructionNumber = writeInstr.num();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
    RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
    if (dialog.showAndGet()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Logger(com.intellij.openapi.diagnostic.Logger) HelpID(com.intellij.refactoring.HelpID) InlineActionHandler(com.intellij.lang.refactoring.InlineActionHandler) PsiReference(com.intellij.psi.PsiReference) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) BitSet(java.util.BitSet) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) BitSet(java.util.BitSet) PsiReference(com.intellij.psi.PsiReference) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with GrReferenceExpression

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

the class GroovyMethodInliner method prepareNewMethod.

/**
   * Prepare temporary method with non-conflicting local names
   */
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    if (method instanceof GrReflectedMethod) {
        method = ((GrReflectedMethod) method).getBaseMethod();
    }
    GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
    if (qualifier != null) {
        Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
        GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
    }
    ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
    collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
    // there are only local variables and parameters (possible of inner closures)
    for (PsiNamedElement namedElement : innerDefinitions) {
        String name = namedElement.getName();
        if (name != null) {
            String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
            if (!newName.equals(namedElement.getName())) {
                final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
                for (PsiReference ref : refs) {
                    PsiElement element = ref.getElement();
                    if (element instanceof GrReferenceExpression) {
                        GrExpression newExpr = factory.createExpressionFromText(newName);
                        ((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
                    }
                }
                namedElement.setName(newName);
            }
        }
    }
    GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
    return newMethod;
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with GrReferenceExpression

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

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

the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.

private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
    final StringPartInfo part = context.getStringPart();
    GrExpression expression = context.getExpression();
    if (expression != null) {
        expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
        if (expression == null)
            return false;
        if (expression instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
            if (PsiUtil.isLocalVariable(resolved)) {
                expression = ((GrVariable) resolved).getInitializerGroovy();
                if (expression == null)
                    return false;
            }
        }
        ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
        expression.accept(visitor);
        return visitor.isResult();
    }
    if (part != null) {
        for (GrStringInjection injection : part.getInjections()) {
            GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
            assert scope != null;
            ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
            scope.accept(visitor);
            if (!visitor.isResult()) {
                return visitor.isResult();
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 30 with GrReferenceExpression

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

the class SwitchStatementGenerator method generateIfs.

private static void generateIfs(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull GrExpression condition, @NotNull GrCaseSection[] caseSections) {
    final GrExpression ref;
    if (condition instanceof GrReferenceExpression) {
        ref = condition;
    } else {
        final String varName = generateConditionVar(builder, context, condition);
        ref = GroovyPsiElementFactory.getInstance(context.project).createExpressionFromText(varName);
    }
    final GrExpression[] args = { ref };
    generateIfFromCaseSection(builder, context, caseSections, 0, args);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) 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