Search in sources :

Example 21 with GroovyResolveResult

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

the class GenerationUtil method resolveMethod.

@NotNull
public static GroovyResolveResult resolveMethod(@Nullable GrExpression caller, @NotNull String methodName, @NotNull GrExpression[] exprs, @NotNull GrNamedArgument[] namedArgs, @NotNull GrClosableBlock[] closureArgs, @NotNull GroovyPsiElement psiContext) {
    GroovyResolveResult call = GroovyResolveResult.EMPTY_RESULT;
    final PsiType type;
    if (caller == null) {
        type = GroovyPsiElementFactory.getInstance(psiContext.getProject()).createExpressionFromText("this", psiContext).getType();
    } else {
        type = caller.getType();
    }
    if (type != null) {
        final PsiType[] argumentTypes = PsiUtil.getArgumentTypes(namedArgs, exprs, closureArgs, false, null);
        final GroovyResolveResult[] candidates = ResolveUtil.getMethodCandidates(type, methodName, psiContext, argumentTypes);
        call = PsiImplUtil.extractUniqueResult(candidates);
    }
    return call;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with GroovyResolveResult

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

the class CodeBlockGenerator method writeTupleDeclaration.

private void writeTupleDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
    GrVariable[] variables = variableDeclaration.getVariables();
    final GrExpression tupleInitializer = variableDeclaration.getTupleInitializer();
    if (tupleInitializer instanceof GrListOrMap) {
        for (GrVariable variable : variables) {
            GenerationUtil.writeVariableSeparately(variable, builder, expressionContext);
            builder.append(";\n");
        }
    } else if (tupleInitializer != null) {
        GroovyResolveResult iteratorMethodResult = GenerationUtil.resolveMethod(tupleInitializer, "iterator", GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, variableDeclaration);
        final PsiType iteratorType = inferIteratorType(iteratorMethodResult, tupleInitializer);
        final String iteratorName = genIteratorVar(variableDeclaration, builder, expressionContext, tupleInitializer, iteratorType, iteratorMethodResult);
        final GrModifierList modifierList = variableDeclaration.getModifierList();
        PsiType iterableTypeParameter = PsiUtil.extractIterableTypeParameter(iteratorType, false);
        for (final GrVariable v : variables) {
            ModifierListGenerator.writeModifiers(builder, modifierList);
            final PsiType type = context.typeProvider.getVarType(v);
            TypeWriter.writeType(builder, type, variableDeclaration);
            builder.append(' ').append(v.getName());
            builder.append(" = ");
            GenerationUtil.wrapInCastIfNeeded(builder, type, iterableTypeParameter, tupleInitializer, expressionContext, new StatementWriter() {

                @Override
                public void writeStatement(StringBuilder builder, ExpressionContext context) {
                    builder.append(iteratorName).append(".hasNext() ? ").append(iteratorName).append(".next() : null");
                }
            });
            builder.append(";\n");
        }
    } else {
        GenerationUtil.writeSimpleVarDeclaration(variableDeclaration, builder, expressionContext);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)

Example 23 with GroovyResolveResult

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

the class ExpressionGenerator method generateMethodCall.

private void generateMethodCall(GrMethodCall methodCallExpression) {
    final GrExpression invoked = methodCallExpression.getInvokedExpression();
    final GrExpression[] exprs = methodCallExpression.getExpressionArguments();
    final GrNamedArgument[] namedArgs = methodCallExpression.getNamedArguments();
    final GrClosableBlock[] clArgs = methodCallExpression.getClosureArguments();
    if (invoked instanceof GrReferenceExpression) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) invoked).advancedResolve();
        final PsiElement resolved = resolveResult.getElement();
        if (resolved instanceof PsiMethod) {
            //todo replace null-qualifier with this-reference
            final GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
            invokeMethodOn(((PsiMethod) resolved), qualifier, exprs, namedArgs, clArgs, resolveResult.getSubstitutor(), methodCallExpression);
            return;
        } else if (resolved == null) {
            final GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
            final GrExpression[] args = generateArgsForInvokeMethod(((GrReferenceExpression) invoked).getReferenceName(), exprs, namedArgs, clArgs, methodCallExpression);
            GenerationUtil.invokeMethodByName(qualifier, "invokeMethod", args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, this, methodCallExpression);
            return;
        }
    }
    GenerationUtil.invokeMethodByName(invoked, "call", exprs, namedArgs, clArgs, this, methodCallExpression);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 24 with GroovyResolveResult

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

the class ExpressionGenerator method visitAssignmentExpression.

/**
   * x= expr ->
   * x = expr
   * x.set(expr)[: x.get()]
   * x+= expr ->
   * x+=expr
   * x= plus(x, expr)
   * x.set(plus(x, expr))[expr]
   * x[a] = 4 ->
   * x[a] = 4
   * x.putAt(a, 4) [4]
   */
@Override
public void visitAssignmentExpression(@NotNull final GrAssignmentExpression expression) {
    final GrExpression lValue = expression.getLValue();
    final GrExpression rValue = expression.getRValue();
    final IElementType token = expression.getOperationTokenType();
    PsiElement realLValue = PsiUtil.skipParentheses(lValue, false);
    if (realLValue instanceof GrReferenceExpression && rValue != null) {
        GroovyResolveResult resolveResult = ((GrReferenceExpression) realLValue).advancedResolve();
        PsiElement resolved = resolveResult.getElement();
        if (resolved instanceof GrVariable && context.analyzedVars.toWrap((GrVariable) resolved)) {
            //write assignment to wrapped local var
            writeAssignmentWithRefSetter((GrExpression) realLValue, expression);
            return;
        } else if (resolved instanceof PsiMethod && resolveResult.isInvokedOnProperty()) {
            //write assignment via setter
            writeAssignmentWithSetter(((GrReferenceExpression) realLValue).getQualifier(), (PsiMethod) resolved, expression);
            return;
        } else if (resolved == null || resolved instanceof GrBindingVariable) {
            //write unresolved reference assignment via setter GroovyObject.setProperty(String name, Object value)
            final GrExpression qualifier = ((GrReferenceExpression) realLValue).getQualifier();
            final PsiType type = PsiImplUtil.getQualifierType((GrReferenceExpression) realLValue);
            final GrExpression[] args = { factory.createExpressionFromText("\"" + ((GrReferenceExpression) realLValue).getReferenceName() + "\""), getRValue(expression) };
            GroovyResolveResult[] candidates = type != null ? ResolveUtil.getMethodCandidates(type, "setProperty", expression, args[0].getType(), args[1].getType()) : GroovyResolveResult.EMPTY_ARRAY;
            final PsiMethod method = PsiImplUtil.extractUniqueElement(candidates);
            if (method != null) {
                writeAssignmentWithSetter(qualifier, method, args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, PsiSubstitutor.EMPTY, expression);
                return;
            }
        }
    } else if (realLValue instanceof GrIndexProperty) {
        //qualifier[args] = rValue
        //write assignment via qualifier.putAt(Args, Value) method
        final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(((GrIndexProperty) realLValue).multiResolve(false));
        final PsiElement resolved = result.getElement();
        if (resolved instanceof PsiMethod) {
            final GrExpression[] args = ((GrIndexProperty) realLValue).getArgumentList().getExpressionArguments();
            writeAssignmentWithSetter(((GrIndexProperty) realLValue).getInvokedExpression(), (PsiMethod) resolved, ArrayUtil.append(args, getRValue(expression)), GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, result.getSubstitutor(), expression);
            return;
        }
    }
    final PsiType lType = GenerationUtil.getDeclaredType(lValue, context);
    if (token == GroovyTokenTypes.mASSIGN) {
        //write simple assignment
        lValue.accept(this);
        builder.append(" = ");
        if (rValue != null) {
            final PsiType rType = GenerationUtil.getDeclaredType(rValue, context);
            GenerationUtil.wrapInCastIfNeeded(builder, GenerationUtil.getNotNullType(expression, lType), rType, expression, context, new StatementWriter() {

                @Override
                public void writeStatement(StringBuilder builder, ExpressionContext context) {
                    rValue.accept(ExpressionGenerator.this);
                }
            });
        }
    } else {
        //write assignment such as +=, -=, etc
        final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
        final PsiElement resolved = resolveResult.getElement();
        if (resolved instanceof PsiMethod && !shouldNotReplaceOperatorWithMethod(lValue.getType(), rValue, expression.getOperationTokenType())) {
            lValue.accept(this);
            builder.append(" = ");
            final PsiType rType = GenerationUtil.getDeclaredType((PsiMethod) resolved, resolveResult.getSubstitutor(), context);
            GenerationUtil.wrapInCastIfNeeded(builder, GenerationUtil.getNotNullType(expression, lType), rType, expression, context, new StatementWriter() {

                @Override
                public void writeStatement(StringBuilder builder, ExpressionContext context) {
                    invokeMethodOn(((PsiMethod) resolved), (GrExpression) lValue.copy(), rValue == null ? GrExpression.EMPTY_ARRAY : new GrExpression[] { rValue }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
                }
            });
        } else {
            writeSimpleBinaryExpression(expression.getOperationToken(), lValue, rValue);
        }
    }
}
Also used : GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) IElementType(com.intellij.psi.tree.IElementType) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 25 with GroovyResolveResult

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

the class ExpressionGenerator method generatePrefixIncDec.

private boolean generatePrefixIncDec(PsiMethod method, GrExpression operand, GrUnaryExpression unary) {
    if (!(operand instanceof GrReferenceExpression))
        return false;
    final GrExpression qualifier = ((GrReferenceExpression) operand).getQualifier();
    final GroovyResolveResult resolveResult = ((GrReferenceExpression) operand).advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) resolved)) {
        final PsiMethod getter = (PsiMethod) resolved;
        final String propertyName = GroovyPropertyUtils.getPropertyNameByGetter(getter);
        final PsiType type;
        if (qualifier == null) {
            type = null;
        } else {
            type = qualifier.getType();
            if (type == null)
                return false;
        }
        final PsiMethod setter = GroovyPropertyUtils.findPropertySetter(type, propertyName, unary);
        if (setter == null)
            return false;
        final ExpressionGenerator generator = new ExpressionGenerator(new StringBuilder(), context);
        if (shouldNotReplaceOperatorWithMethod(operand.getType(), null, unary.getOperationTokenType())) {
            writeSimpleUnary(operand, unary, generator);
        } else {
            generator.invokeMethodOn(method, operand, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), unary);
        }
        final GrExpression fromText = factory.createExpressionFromText(generator.toString(), unary);
        invokeMethodOn(setter, qualifier, new GrExpression[] { fromText }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), unary);
    } else if (resolved instanceof PsiVariable) {
        boolean wrap = context.analyzedVars.toWrap((PsiVariable) resolved);
        boolean doNeedExpression = PsiUtil.isExpressionUsed(unary);
        StringBuilder curBuilder;
        ExpressionGenerator curGenerator;
        if (doNeedExpression && wrap) {
            curBuilder = new StringBuilder();
            curGenerator = new ExpressionGenerator(curBuilder, context);
        } else {
            curBuilder = builder;
            curGenerator = this;
        }
        boolean shouldInsertParentheses = !wrap && doNeedExpression;
        if (shouldInsertParentheses) {
            curBuilder.append('(');
        }
        operand.accept(curGenerator);
        if (wrap) {
            curBuilder.append(".set(");
        } else {
            curBuilder.append(" = ");
        }
        if (shouldNotReplaceOperatorWithMethod(operand.getType(), null, unary.getOperationTokenType())) {
            writeSimpleUnary((GrExpression) operand.copy(), unary, curGenerator);
        } else {
            curGenerator.invokeMethodOn(method, (GrExpression) operand.copy(), GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), unary);
        }
        if (shouldInsertParentheses) {
            curBuilder.append(')');
        }
        if (wrap) {
            curBuilder.append(')');
            if (doNeedExpression) {
                curBuilder.append(';');
                context.myStatements.add(curBuilder.toString());
                operand.accept(this);
                builder.append(".get()");
            }
        }
    }
    return true;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)132 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)33 Nullable (org.jetbrains.annotations.Nullable)29 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)29 NotNull (org.jetbrains.annotations.NotNull)25 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)23 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)14 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)13 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)8 IElementType (com.intellij.psi.tree.IElementType)7 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)7 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)7 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)7 Project (com.intellij.openapi.project.Project)6