Search in sources :

Example 11 with GrMethodCallExpression

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

the class JavaStylePropertiesInvocationInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCallExpression) {
            super.visitMethodCallExpression(methodCallExpression);
            visitMethodCall(methodCallExpression);
        }

        @Override
        public void visitApplicationStatement(@NotNull GrApplicationStatement applicationStatement) {
            super.visitApplicationStatement(applicationStatement);
            visitMethodCall(applicationStatement);
        }

        private void visitMethodCall(GrMethodCall methodCall) {
            if (JavaStylePropertiesUtil.isPropertyAccessor(methodCall)) {
                final String message = GroovyInspectionBundle.message("java.style.property.access");
                final GrExpression expression = methodCall.getInvokedExpression();
                if (expression instanceof GrReferenceExpression) {
                    PsiElement referenceNameElement = ((GrReferenceExpression) expression).getReferenceNameElement();
                    registerError(referenceNameElement, message, myFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }
            }
        }
    };
}
Also used : BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) NotNull(org.jetbrains.annotations.NotNull) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with GrMethodCallExpression

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

the class GroovyDslDefaultMembers method enclosingCall.

/**
   * Returns enclosing method call of a given context's place
   */
@Nullable
public GrCall enclosingCall(String name, GdslMembersHolderConsumer consumer) {
    final PsiElement place = consumer.getPlace();
    if (place == null)
        return null;
    GrCall call = PsiTreeUtil.getParentOfType(place, GrCall.class, true);
    if (call == null)
        return null;
    while (call != null && !name.equals(getInvokedMethodName(call))) {
        call = PsiTreeUtil.getParentOfType(call, GrCall.class, true);
    }
    if (call == null)
        return null;
    final GrArgumentList argumentList = call.getArgumentList();
    if (argumentList != null) {
        for (GrExpression arg : argumentList.getExpressionArguments()) {
            if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
                return call;
            }
        }
    }
    if (call instanceof GrMethodCallExpression) {
        for (GrExpression arg : call.getClosureArguments()) {
            if (arg instanceof GrClosableBlock && PsiTreeUtil.findCommonParent(place, arg) == arg) {
                return call;
            }
        }
    }
    return null;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with GrMethodCallExpression

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

the class GroovyTypeCheckVisitorHelper method getExpressionArgumentsOfCall.

@Nullable
public static List<GrExpression> getExpressionArgumentsOfCall(@NotNull GrArgumentList argumentList) {
    final ArrayList<GrExpression> args = ContainerUtil.newArrayList();
    for (GroovyPsiElement arg : argumentList.getAllArguments()) {
        if (arg instanceof GrSpreadArgument) {
            GrExpression spreaded = ((GrSpreadArgument) arg).getArgument();
            if (spreaded instanceof GrListOrMap && !((GrListOrMap) spreaded).isMap()) {
                Collections.addAll(args, ((GrListOrMap) spreaded).getInitializers());
            } else {
                return null;
            }
        } else if (arg instanceof GrExpression) {
            args.add((GrExpression) arg);
        } else if (arg instanceof GrNamedArgument) {
            args.add(((GrNamedArgument) arg).getExpression());
        }
    }
    final PsiElement parent = argumentList.getParent();
    if (parent instanceof GrIndexProperty && PsiUtil.isLValue((GroovyPsiElement) parent)) {
        args.add(TypeInferenceHelper.getInitializerFor((GrExpression) parent));
    } else if (parent instanceof GrMethodCallExpression) {
        ContainerUtil.addAll(args, ((GrMethodCallExpression) parent).getClosureArguments());
    }
    return args;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrSpreadArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrSpreadArgument) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with GrMethodCallExpression

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

the class GrNamedArgumentSearchVisitor method visitReferenceExpression.

@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
    if (myFirstArgumentName.equals(referenceExpression.getReferenceName()) && !referenceExpression.isQualified()) {
        PsiElement parent = referenceExpression.getParent();
        if (parent instanceof GrReferenceExpression) {
            GrReferenceExpression parentRef = (GrReferenceExpression) parent;
            PsiElement parentParent = parentRef.getParent();
            if (parentParent instanceof GrMethodCallExpression) {
                if (METHOD_NAMES.contains(parentRef.getReferenceName())) {
                    extractArguments(((GrMethodCallExpression) parentParent).getArgumentList());
                }
            } else {
                add(parentRef.getReferenceName());
            }
        } else if (parent instanceof GrIndexProperty) {
            GrIndexProperty indexProperty = (GrIndexProperty) parent;
            extractArguments(indexProperty.getArgumentList());
        }
    }
    super.visitReferenceExpression(referenceExpression);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 15 with GrMethodCallExpression

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

the class PsiImplUtil method replaceExpression.

@Nullable
public static GrExpression replaceExpression(GrExpression oldExpr, GrExpression newExpr, boolean removeUnnecessaryParentheses) {
    PsiElement oldParent = oldExpr.getParent();
    if (oldParent == null)
        throw new PsiInvalidElementAccessException(oldExpr);
    if (!(oldExpr instanceof GrApplicationStatement)) {
        newExpr = ApplicationStatementUtil.convertToMethodCallExpression(newExpr);
    }
    // Remove unnecessary parentheses
    if (removeUnnecessaryParentheses && oldParent instanceof GrParenthesizedExpression && !(oldParent.getParent() instanceof GrArgumentLabel)) {
        return ((GrExpression) oldParent).replaceWithExpression(newExpr, true);
    }
    //regexes cannot be after identifier , try to replace it with simple string
    if (getRegexAtTheBeginning(newExpr) != null && isAfterIdentifier(oldExpr)) {
        final PsiElement copy = newExpr.copy();
        final GrLiteral regex = getRegexAtTheBeginning(copy);
        LOG.assertTrue(regex != null);
        final GrLiteral stringLiteral = GrStringUtil.createStringFromRegex(regex);
        if (regex == copy) {
            return oldExpr.replaceWithExpression(stringLiteral, removeUnnecessaryParentheses);
        } else {
            regex.replace(stringLiteral);
            return oldExpr.replaceWithExpression((GrExpression) copy, removeUnnecessaryParentheses);
        }
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(oldExpr.getProject());
    if (oldParent instanceof GrStringInjection) {
        if (newExpr instanceof GrString || newExpr instanceof GrLiteral && ((GrLiteral) newExpr).getValue() instanceof String) {
            return GrStringUtil.replaceStringInjectionByLiteral((GrStringInjection) oldParent, (GrLiteral) newExpr);
        } else {
            GrClosableBlock block = factory.createClosureFromText("{foo}");
            oldParent.getNode().replaceChild(oldExpr.getNode(), block.getNode());
            GrStatement[] statements = block.getStatements();
            return ((GrExpression) statements[0]).replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        }
    }
    if (PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class, false, GrCodeBlock.class) != null) {
        final GrStringInjection stringInjection = PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class);
        GrStringUtil.wrapInjection(stringInjection);
        assert stringInjection != null;
        final PsiElement replaced = oldExpr.replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        return (GrExpression) replaced;
    }
    //check priorities    
    if (oldParent instanceof GrExpression && !(oldParent instanceof GrParenthesizedExpression)) {
        GrExpression addedParenth = checkPrecedence(newExpr, oldExpr) ? parenthesize(newExpr) : newExpr;
        if (newExpr != addedParenth) {
            return oldExpr.replaceWithExpression(addedParenth, removeUnnecessaryParentheses);
        }
    }
    //we should add the expression in arg list
    if (oldExpr instanceof GrClosableBlock && !(newExpr instanceof GrClosableBlock) && oldParent instanceof GrMethodCallExpression && ArrayUtil.contains(oldExpr, ((GrMethodCallExpression) oldParent).getClosureArguments())) {
        return ((GrMethodCallExpression) oldParent).replaceClosureArgument((GrClosableBlock) oldExpr, newExpr);
    }
    newExpr = (GrExpression) oldExpr.replace(newExpr);
    // to find target parenthesised expression.
    if (newExpr instanceof GrParenthesizedExpression && isFirstChild(newExpr)) {
        int parentCount = 0;
        PsiElement element = oldParent;
        while (element != null && !(element instanceof GrCommandArgumentList)) {
            if (element instanceof GrCodeBlock || element instanceof GrParenthesizedExpression)
                break;
            if (element instanceof PsiFile)
                break;
            final PsiElement parent = element.getParent();
            if (parent == null)
                break;
            if (!isFirstChild(element))
                break;
            element = parent;
            parentCount++;
        }
        if (element instanceof GrCommandArgumentList) {
            final GrCommandArgumentList commandArgList = (GrCommandArgumentList) element;
            final PsiElement parent = commandArgList.getParent();
            LOG.assertTrue(parent instanceof GrApplicationStatement);
            final GrMethodCall methodCall = factory.createMethodCallByAppCall((GrApplicationStatement) parent);
            final GrMethodCall newCall = (GrMethodCall) parent.replace(methodCall);
            PsiElement result = newCall.getArgumentList().getAllArguments()[0];
            for (int i = 0; i < parentCount; i++) {
                result = PsiUtil.skipWhitespacesAndComments(result.getFirstChild(), true);
            }
            LOG.assertTrue(result instanceof GrParenthesizedExpression);
            return (GrExpression) result;
        }
    }
    return newExpr;
}
Also used : GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)48 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 PsiElement (com.intellij.psi.PsiElement)16 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)11 Nullable (org.jetbrains.annotations.Nullable)8 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)8 NotNull (org.jetbrains.annotations.NotNull)5 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)5 TextRange (com.intellij.openapi.util.TextRange)4 PsiType (com.intellij.psi.PsiType)4 IElementType (com.intellij.psi.tree.IElementType)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NonNls (org.jetbrains.annotations.NonNls)4 ASTNode (com.intellij.lang.ASTNode)3 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)3