Search in sources :

Example 51 with GrExpression

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

the class GrIntroduceFieldProcessor method run.

@Nullable
public GrVariable run() {
    PsiElement scope = myContext.getScope();
    final PsiClass targetClass = scope instanceof GroovyFileBase ? ((GroovyFileBase) scope).getScriptClass() : (PsiClass) scope;
    if (targetClass == null)
        return null;
    final GrVariableDeclaration declaration = insertField(targetClass);
    final GrVariable field = declaration.getVariables()[0];
    if (mySettings.removeLocalVar()) {
        myLocalVariable = GrIntroduceHandlerBase.resolveLocalVar(myContext);
        assert myLocalVariable != null : myContext.getExpression() + ", " + myContext.getVar() + ", " + myContext.getStringPart();
    }
    GrExpression originalInitializer = getInitializer();
    myInitializer = originalInitializer == null ? null : (GrExpression) originalInitializer.copy();
    List<PsiElement> replaced = processOccurrences(targetClass, field);
    switch(mySettings.initializeIn()) {
        case CUR_METHOD:
            initializeInMethod(field, replaced);
            break;
        case FIELD_DECLARATION:
            field.setInitializerGroovy(myInitializer);
            break;
        case CONSTRUCTOR:
            initializeInConstructor(field, replaced);
            break;
        case SETUP_METHOD:
            initializeInSetup(field, replaced);
            break;
    }
    JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
    if (mySettings.removeLocalVar()) {
        GrIntroduceHandlerBase.deleteLocalVar(myLocalVariable);
    }
    return field;
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with GrExpression

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

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

the class GrInplaceParameterIntroducer method createExpressionWrapper.

@NotNull
private static GrExpressionWrapper createExpressionWrapper(@NotNull GrIntroduceContext context) {
    GrExpression expression = context.getExpression();
    GrVariable var = context.getVar();
    assert expression != null || var != null;
    GrExpression initializer = expression != null ? expression : var.getInitializerGroovy();
    return new GrExpressionWrapper(initializer);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with GrExpression

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

the class GrIntroduceConstantProcessor method run.

@Nullable
public GrField run() {
    final PsiClass targetClass = settings.getTargetClass();
    if (targetClass == null)
        return null;
    if (checkErrors(targetClass)) {
        return null;
    }
    final GrVariableDeclaration declaration = addDeclaration(targetClass);
    final GrField field = (GrField) declaration.getVariables()[0];
    GrVariable localVar = GrIntroduceHandlerBase.resolveLocalVar(context);
    if (localVar != null) {
        assert localVar.getInitializerGroovy() != null : "initializer should exist: " + localVar.getText();
        GrIntroduceHandlerBase.deleteLocalVar(localVar);
        if (settings.replaceAllOccurrences()) {
            processOccurrences(field);
        } else {
            replaceOccurrence(field, localVar.getInitializerGroovy(), isEscalateVisibility());
        }
    } else if (context.getStringPart() != null) {
        final GrExpression ref = context.getStringPart().replaceLiteralWithConcatenation(field.getName());
        final PsiElement element = replaceOccurrence(field, ref, isEscalateVisibility());
        updateCaretPosition(element);
    } else if (context.getExpression() != null) {
        if (settings.replaceAllOccurrences()) {
            processOccurrences(field);
        } else {
            replaceOccurrence(field, context.getExpression(), isEscalateVisibility());
        }
    }
    return field;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with GrExpression

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

the class GrIntroduceConstantProcessor method getInitializer.

@NotNull
protected GrExpression getInitializer() {
    GrVariable var = GrIntroduceHandlerBase.resolveLocalVar(context);
    GrExpression expression = context.getExpression();
    if (var != null) {
        return var.getInitializerGroovy();
    } else if (expression != null) {
        return expression;
    } else {
        return context.getStringPart().createLiteralFromSelected();
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16