Search in sources :

Example 6 with GrOpenBlock

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

the class ConvertSimpleGetterToPropertyIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrMethod method = (GrMethod) element.getParent();
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement statement = block.getStatements()[0];
    GrExpression value;
    if (statement instanceof GrReturnStatement) {
        value = ((GrReturnStatement) statement).getReturnValue();
    } else {
        value = (GrExpression) statement;
    }
    String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
    if (fieldName == null)
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    List<String> modifiers = ContainerUtil.newArrayList();
    for (String modifier : MODIFIERS_TO_CHECK) {
        if (method.hasModifierProperty(modifier))
            modifiers.add(modifier);
    }
    modifiers.add(PsiModifier.FINAL);
    GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
    PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
    GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
    PsiElement replaced = method.replace(declaration);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType)

Example 7 with GrOpenBlock

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

the class MergeElseIfIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrIfStatement parentStatement = (GrIfStatement) element;
    GrBlockStatement elseBlockStatement = (GrBlockStatement) parentStatement.getElseBranch();
    assert elseBlockStatement != null;
    final GrOpenBlock elseBranch = elseBlockStatement.getBlock();
    final GrStatement elseBranchContents = elseBranch.getStatements()[0];
    PsiImplUtil.replaceStatement("if(" + parentStatement.getCondition().getText() + ")" + parentStatement.getThenBranch().getText() + "else " + elseBranchContents.getText(), parentStatement);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 8 with GrOpenBlock

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

the class GroovyExtractMethodHandler method renameParameterOccurrences.

private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement[] statements = block.getStatements();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    for (ParameterInfo info : helper.getParameterInfos()) {
        final String oldName = info.getOriginalName();
        final String newName = info.getName();
        final ArrayList<GrExpression> result = new ArrayList<>();
        if (!oldName.equals(newName)) {
            for (final GrStatement statement : statements) {
                statement.accept(new PsiRecursiveElementVisitor() {

                    @Override
                    public void visitElement(final PsiElement element) {
                        super.visitElement(element);
                        if (element instanceof GrReferenceExpression) {
                            GrReferenceExpression expr = (GrReferenceExpression) element;
                            if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
                                result.add(expr);
                            }
                        }
                    }
                });
                for (GrExpression expr : result) {
                    expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) ParameterInfo(org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 9 with GrOpenBlock

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

the class GroovyMethodInliner method isOnExpressionOrReturnPlace.

/*
  Method call is used as expression in some enclosing expression or
  is method return result
  */
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
    PsiElement parent = call.getParent();
    if (!(parent instanceof GrVariableDeclarationOwner)) {
        return true;
    }
    // tail calls in methods and closures
    GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
    if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
        GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
        assert statements.length > 0;
        GrStatement last = statements[statements.length - 1];
        if (last == call)
            return true;
        if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
            return true;
        }
    }
    return false;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 10 with GrOpenBlock

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

the class GroovyInlineMethodUtil method isTailMethodCall.

/**
   * Checks whether given method call is tail call of other method or closure
   *
   * @param call [tail?] Method call
   * @return
   */
static boolean isTailMethodCall(GrCallExpression call) {
    GrStatement stmt = call;
    PsiElement parent = call.getParent();
    // return statement
    if (parent instanceof GrReturnStatement) {
        stmt = ((GrReturnStatement) parent);
        parent = parent.getParent();
    }
    // method body result
    if (parent instanceof GrOpenBlock) {
        if (parent.getParent() instanceof GrMethod) {
            GrStatement[] statements = ((GrOpenBlock) parent).getStatements();
            return statements.length > 0 && stmt == statements[statements.length - 1];
        }
    }
    // closure result
    if (parent instanceof GrClosableBlock) {
        GrStatement[] statements = ((GrClosableBlock) parent).getStatements();
        return statements.length > 0 && stmt == statements[statements.length - 1];
    }
    // todo test me!
    if (stmt instanceof GrReturnStatement) {
        GrMethod method = PsiTreeUtil.getParentOfType(stmt, GrMethod.class);
        if (method != null) {
            Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(method.getBlock());
            return returnStatements.contains(stmt) && !hasBadReturns(method);
        }
    }
    return false;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)68 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)24 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)21 PsiElement (com.intellij.psi.PsiElement)13 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)7 Nullable (org.jetbrains.annotations.Nullable)6 NotNull (org.jetbrains.annotations.NotNull)5 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)5 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)5 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)5 TextRange (com.intellij.openapi.util.TextRange)4 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)4 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)4 UsageInfo (com.intellij.usageView.UsageInfo)3