Search in sources :

Example 81 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrIntroduceHandlerBase method insertExplicitCastIfNeeded.

public static GrExpression insertExplicitCastIfNeeded(GrVariable variable, GrExpression initializer) {
    PsiType ltype = findLValueType(initializer);
    PsiType rtype = initializer.getType();
    GrExpression rawExpr = (GrExpression) PsiUtil.skipParentheses(initializer, false);
    if (ltype == null || TypesUtil.isAssignableWithoutConversions(ltype, rtype, initializer) || !TypesUtil.isAssignable(ltype, rtype, initializer)) {
        return rawExpr;
    } else {
        // implicit coercion should be replaced with explicit cast
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(variable.getProject());
        GrSafeCastExpression cast = (GrSafeCastExpression) factory.createExpressionFromText("a as B");
        cast.getOperand().replaceWithExpression(rawExpr, false);
        cast.getCastTypeElement().replace(factory.createTypeElement(ltype));
        return cast;
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)

Example 82 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method processExternalUsage.

private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
    final PsiElement element = usage.getElement();
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
    if (callExpression == null) {
        final PsiElement parent = element.getParent();
        if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
            callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
        }
    }
    if (callExpression == null)
        return;
    //check for x.getFoo()(args)
    if (callExpression instanceof GrMethodCall) {
        final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
        if (invoked instanceof GrReferenceExpression) {
            final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
            final PsiElement resolved = result.getElement();
            if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
                PsiElement actualCallExpression = callExpression.getParent();
                if (actualCallExpression instanceof GrCall) {
                    callExpression = (GrCall) actualCallExpression;
                }
            }
        }
    }
    GrArgumentList argList = callExpression.getArgumentList();
    LOG.assertTrue(argList != null);
    GrExpression[] oldArgs = argList.getExpressionArguments();
    GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
    if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
        argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
    } else {
        PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newarg can be replaced by OldReferenceResolve
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
    } else {
        GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) OldReferencesResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver)

Example 83 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method changeSignature.

private static void changeSignature(GrClosableBlock block, GrIntroduceParameterSettings settings) {
    final String name = settings.getName();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(name, block);
    final GrParameter[] parameters = block.getParameters();
    settings.parametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    final PsiType type = settings.getSelectedType();
    final String typeText = type == null ? null : type.getCanonicalText();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    GrParameter parameter = factory.createParameter(name, typeText, block);
    parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, settings.declareFinal());
    final GrParameterList parameterList = block.getParameterList();
    final PsiParameter anchorParameter = GroovyIntroduceParameterUtil.getAnchorParameter(parameterList, block.isVarArgs());
    parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
    if (block.getArrow() == null) {
        final PsiElement arrow = block.addAfter(factory.createClosureFromText("{->}").getArrow().copy(), parameterList);
        final PsiElement child = block.getFirstChild().getNextSibling();
        if (PsiImplUtil.isWhiteSpaceOrNls(child)) {
            final String text = child.getText();
            child.delete();
            block.addAfter(factory.createLineTerminator(text), arrow);
        }
    }
    JavaCodeStyleManager.getInstance(parameter.getProject()).shortenClassReferences(parameter);
    fieldConflictsResolver.fix();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) TIntProcedure(gnu.trove.TIntProcedure) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 84 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method processChangedMethodCall.

private static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings) {
    if (element.getParent() instanceof GrMethodCallExpression) {
        GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
        GrExpression expression = factory.createExpressionFromText(settings.getName(), null);
        final GrArgumentList argList = methodCall.getArgumentList();
        final PsiElement[] exprs = argList.getAllArguments();
        if (exprs.length > 0) {
            argList.addAfter(expression, exprs[exprs.length - 1]);
        } else {
            argList.add(expression);
        }
        removeParametersFromCall(methodCall, settings);
    } else {
        LOG.error(element.getParent());
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Example 85 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrIntroduceParameterProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    //PsiType initializerType = mySettings.getSelectedType();
    // Changing external occurrences (the tricky part)
    IntroduceParameterUtil.processUsages(usages, this);
    final GrMethod toReplaceIn = (GrMethod) mySettings.getToReplaceIn();
    final PsiMethod toSearchFor = (PsiMethod) mySettings.getToSearchFor();
    final boolean methodsToProcessAreDifferent = toReplaceIn != toSearchFor;
    if (mySettings.generateDelegate()) {
        generateDelegate(toReplaceIn, toSearchFor, methodsToProcessAreDifferent);
    }
    // Changing signature of initial method
    // (signature of myMethodToReplaceIn will be either changed now or have already been changed)
    //LOG.assertTrue(initializerType == null || initializerType.isValid());
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(mySettings.getName(), toReplaceIn.getBlock());
    processMethodSignature(usages, toReplaceIn, toSearchFor, methodsToProcessAreDifferent);
    processUsages(usages, factory);
    processStringPart();
    processVar();
    fieldConflictsResolver.fix();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Aggregations

GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)159 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)42 PsiElement (com.intellij.psi.PsiElement)30 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)23 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)22 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)21 IncorrectOperationException (com.intellij.util.IncorrectOperationException)20 Nullable (org.jetbrains.annotations.Nullable)20 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)18 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)17 NotNull (org.jetbrains.annotations.NotNull)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)15 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 Project (com.intellij.openapi.project.Project)8 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 ASTNode (com.intellij.lang.ASTNode)7 ArrayList (java.util.ArrayList)7 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6