Search in sources :

Example 26 with GrAssignmentExpression

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

the class GrCreateFieldForParameterIntention method performRefactoring.

@Override
protected void performRefactoring(Project project, PsiClass targetClass, PsiMethod method, PsiParameter myParameter, PsiType type, String fieldName, boolean methodStatic, boolean isFinal) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (targetClass.findFieldByName(fieldName, false) == null) {
        String[] modifiers = getModifiers(methodStatic, isFinal);
        GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
        GrVariableDeclaration inserted = (GrVariableDeclaration) targetClass.add(fieldDeclaration);
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
    }
    GrOpenBlock block = ((GrMethod) method).getBlock();
    if (block == null)
        return;
    GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
    GrStatement anchor = getAnchor(block);
    GrStatement statement = block.addStatementBefore(assignment, anchor);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 27 with GrAssignmentExpression

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

the class GrCreateFieldForParameterIntention method checkAssignmentToFieldExists.

private static boolean checkAssignmentToFieldExists(PsiParameter parameter) {
    for (PsiReference reference : ReferencesSearch.search(parameter).findAll()) {
        PsiElement element = reference.getElement();
        if (element instanceof GrReferenceExpression && element.getParent() instanceof GrAssignmentExpression && ((GrAssignmentExpression) element.getParent()).getRValue() == element) {
            GrAssignmentExpression parent = ((GrAssignmentExpression) element.getParent());
            GrExpression value = parent.getLValue();
            if (value instanceof GrReferenceExpression && ((GrReferenceExpression) value).resolve() instanceof PsiField)
                return true;
        }
    }
    return false;
}
Also used : GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 28 with GrAssignmentExpression

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

the class GrVariableInliner method getConflicts.

@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    GrExpression expr = (GrExpression) reference.getElement();
    if (expr.getParent() instanceof GrAssignmentExpression) {
        GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
        if (expr.equals(parent.getLValue())) {
            conflicts.putValue(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
        }
    }
    if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && expr instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) expr).advancedResolve();
        if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
            final PsiElement parent = expr.getParent();
            if (!(parent instanceof GrCall && parent instanceof GrExpression)) {
                conflicts.putValue(expr, GroovyRefactoringBundle.message("reference.to.accessor.0.is.used", CommonRefactoringUtil.htmlEmphasize(PsiFormatUtil.formatMethod((GrAccessorMethod) resolveResult.getElement(), PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE))));
            }
        }
    }
    return conflicts;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with GrAssignmentExpression

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

the class GroovyInlineLocalProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usages);
    final GrExpression initializer = mySettings.getInitializer();
    GrExpression initializerToUse = GrIntroduceHandlerBase.insertExplicitCastIfNeeded(myLocal, mySettings.getInitializer());
    for (UsageInfo usage : usages) {
        GrVariableInliner.inlineReference(usage, myLocal, initializerToUse);
    }
    final PsiElement initializerParent = initializer.getParent();
    if (initializerParent instanceof GrAssignmentExpression) {
        initializerParent.delete();
        return;
    }
    if (initializerParent instanceof GrVariable) {
        final Collection<PsiReference> all = ReferencesSearch.search(myLocal).findAll();
        if (!all.isEmpty()) {
            initializer.delete();
            return;
        }
    }
    final PsiElement owner = myLocal.getParent().getParent();
    if (owner instanceof GrVariableDeclarationOwner) {
        ((GrVariableDeclarationOwner) owner).removeVariable(myLocal);
    } else {
        myLocal.delete();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) PsiReference(com.intellij.psi.PsiReference) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 30 with GrAssignmentExpression

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

the class GrIntroduceFieldProcessor method generateAssignment.

private void generateAssignment(@NotNull GrVariable field, @Nullable GrStatement anchor, @Nullable GrStatementOwner defaultContainer, @Nullable PsiElement occurrenceToDelete) {
    if (myInitializer == null || defaultContainer == null)
        return;
    GrAssignmentExpression init = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(myContext.getProject()).createExpressionFromText(mySettings.getName() + " = " + myInitializer.getText());
    GrStatementOwner block;
    if (anchor != null) {
        anchor = GroovyRefactoringUtil.addBlockIntoParent(anchor);
        LOG.assertTrue(anchor.getParent() instanceof GrStatementOwner);
        block = (GrStatementOwner) anchor.getParent();
    } else {
        block = defaultContainer;
    }
    init = (GrAssignmentExpression) block.addStatementBefore(init, anchor);
    replaceOccurrence(field, init.getLValue(), (PsiClass) myContext.getScope());
    if (occurrenceToDelete != null) {
        occurrenceToDelete.delete();
    }
}
Also used : GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Aggregations

GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)34 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)12 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 IElementType (com.intellij.psi.tree.IElementType)5 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)5 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 PsiFile (com.intellij.psi.PsiFile)3 PsiType (com.intellij.psi.PsiType)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)3 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)3 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)3 ASTNode (com.intellij.lang.ASTNode)2