Search in sources :

Example 16 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class GrIntroduceParameterProcessor method generateDelegate.

private void generateDelegate(GrMethod toReplaceIn, PsiMethod toSearchFor, boolean methodsToProcessAreDifferent) {
    GroovyIntroduceParameterUtil.generateDelegate(toReplaceIn, myParameterInitializer, myProject);
    if (methodsToProcessAreDifferent) {
        final GrMethod method = GroovyIntroduceParameterUtil.generateDelegate(toSearchFor, myParameterInitializer, myProject);
        final PsiClass containingClass = method.getContainingClass();
        if (containingClass != null && containingClass.isInterface()) {
            final GrOpenBlock block = method.getBlock();
            if (block != null) {
                block.delete();
            }
        }
    }
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 17 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method insertDeclaration.

private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
    if (original instanceof GrField) {
        final PsiClass containingClass = ((GrField) original).getContainingClass();
        LOG.assertTrue(containingClass != null);
        return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
    }
    final GrStatementOwner block;
    if (original instanceof PsiParameter) {
        final PsiElement container = original.getParent().getParent();
        if (container instanceof GrMethod) {
            block = ((GrMethod) container).getBlock();
        } else if (container instanceof GrClosableBlock) {
            block = (GrCodeBlock) container;
        } else if (container instanceof GrForStatement) {
            final GrStatement body = ((GrForStatement) container).getBody();
            if (body instanceof GrBlockStatement) {
                block = ((GrBlockStatement) body).getBlock();
            } else {
                GrBlockStatement blockStatement = myFactory.createBlockStatement();
                LOG.assertTrue(blockStatement != null);
                if (body != null) {
                    blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
                    blockStatement = (GrBlockStatement) body.replace(blockStatement);
                } else {
                    blockStatement = (GrBlockStatement) container.add(blockStatement);
                }
                block = blockStatement.getBlock();
            }
        } else {
            throw new IncorrectOperationException();
        }
        LOG.assertTrue(block != null);
        return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
    }
    PsiElement parent = original.getParent();
    LOG.assertTrue(parent instanceof GrVariableDeclaration);
    final PsiElement pparent = parent.getParent();
    if (pparent instanceof GrIfStatement) {
        if (((GrIfStatement) pparent).getThenBranch() == parent) {
            block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
        } else {
            block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
        }
        parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
    } else if (pparent instanceof GrLoopStatement) {
        block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
        parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
    } else {
        LOG.assertTrue(pparent instanceof GrStatementOwner);
        block = (GrStatementOwner) pparent;
    }
    return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 18 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class MoveGroovyMemberHandler method doMove.

@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
    GroovyChangeContextUtil.encodeContextInfo(member);
    final PsiDocComment docComment;
    if (member instanceof PsiDocCommentOwner) {
        docComment = ((PsiDocCommentOwner) member).getDocComment();
    } else {
        docComment = null;
    }
    PsiMember moved;
    if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
        final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
        moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
        member.delete();
    } else if (member instanceof GrEnumConstant) {
        moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
    } else if (member instanceof GrField) {
        if (anchor != null)
            anchor = anchor.getParent();
        final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
        GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
        int number = ArrayUtil.find(parent.getMembers(), member);
        final GrMember[] members = movedDeclaration.getMembers();
        for (int i = 0; i < number; i++) {
            members[i].delete();
        }
        for (int i = number + 1; i < members.length; i++) {
            members[i].delete();
        }
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = movedDeclaration.getModifierList();
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
        }
        moved = movedDeclaration.getMembers()[0];
    } else if (member instanceof GrMethod) {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = moved.getModifierList();
            assert list != null;
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
        }
    } else {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
    }
    if (docComment != null) {
        PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
        PsiElement prevSibling = insertedDocComment.getPrevSibling();
        addLineFeedIfNeeded(prevSibling);
        docComment.delete();
    }
    member.delete();
    return moved;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class GroovyIntroduceParameterUtil method generateDelegate.

public static GrMethod generateDelegate(PsiMethod prototype, IntroduceParameterData.ExpressionWrapper initializer, Project project) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrMethod result;
    if (prototype instanceof GrMethod) {
        result = (GrMethod) prototype.copy();
    } else {
        StringBuilder builder = new StringBuilder();
        builder.append(prototype.getModifierList().getText()).append(' ');
        if (prototype.getReturnTypeElement() != null) {
            builder.append(prototype.getReturnTypeElement().getText());
        }
        builder.append(' ').append(prototype.getName());
        builder.append(prototype.getParameterList().getText());
        builder.append("{}");
        result = factory.createMethodFromText(builder.toString());
    }
    StringBuilder call = new StringBuilder();
    call.append("def foo(){\n");
    final GrParameter[] parameters = result.getParameters();
    call.append(prototype.getName());
    if (initializer.getExpression() instanceof GrClosableBlock) {
        if (parameters.length > 0) {
            call.append('(');
            for (GrParameter parameter : parameters) {
                call.append(parameter.getName()).append(", ");
            }
            call.replace(call.length() - 2, call.length(), ")");
        }
        call.append(initializer.getText());
    } else {
        call.append('(');
        for (GrParameter parameter : parameters) {
            call.append(parameter.getName()).append(", ");
        }
        call.append(initializer.getText());
        call.append(")");
    }
    call.append("\n}");
    final GrOpenBlock block = factory.createMethodFromText(call.toString()).getBlock();
    result.getBlock().replace(block);
    final PsiElement parent = prototype.getParent();
    final GrMethod method = (GrMethod) parent.addBefore(result, prototype);
    JavaCodeStyleManager.getInstance(method.getProject()).shortenClassReferences(method);
    return method;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) 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) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 20 with GrMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.

the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodSignature.

@Override
public boolean processChangeMethodSignature(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    if (!(usage.getElement() instanceof GrMethod) || !isGroovyUsage(usage))
        return true;
    GrMethod method = (GrMethod) usage.getElement();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(data.getParameterName(), method.getBlock());
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(method);
    final PsiParameter[] parameters = method.getParameterList().getParameters();
    data.getParametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                PsiDocTag tag = javaDocHelper.getTagForParameter(param);
                if (tag != null) {
                    tag.delete();
                }
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    addParameter(method, javaDocHelper, data.getForcedType(), data.getParameterName(), data.isDeclareFinal(), data.getProject());
    fieldConflictsResolver.fix();
    return false;
}
Also used : MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) TIntProcedure(gnu.trove.TIntProcedure) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)134 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)24 PsiElement (com.intellij.psi.PsiElement)22 NotNull (org.jetbrains.annotations.NotNull)21 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)17 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)16 ArrayList (java.util.ArrayList)15 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)15 Nullable (org.jetbrains.annotations.Nullable)12 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)12 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)10 Project (com.intellij.openapi.project.Project)9 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)8