Search in sources :

Example 1 with GrIndexProperty

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

the class ExpressionGenerator method visitReferenceExpression.

@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
    final GrExpression qualifier = referenceExpression.getQualifier();
    final GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
    final PsiElement resolved = resolveResult.getElement();
    final String referenceName = referenceExpression.getReferenceName();
    if (PsiUtil.isThisOrSuperRef(referenceExpression)) {
        writeThisOrSuperRef(referenceExpression, qualifier, referenceName);
        return;
    }
    if (ResolveUtil.isClassReference(referenceExpression)) {
        // just delegate to qualifier
        LOG.assertTrue(qualifier != null);
        qualifier.accept(this);
        return;
    }
    if (resolved instanceof PsiClass) {
        builder.append(((PsiClass) resolved).getQualifiedName());
        if (PsiUtil.isExpressionUsed(referenceExpression)) {
            builder.append(".class");
        }
        return;
    }
    //don't try to resolve local vars that are provided my this generator (they are listed in myUsedVarNames)
    if (resolved == null && qualifier == null && context.myUsedVarNames.contains(referenceName)) {
        builder.append(referenceName);
        return;
    }
    //all refs in script that are not resolved are saved in 'binding' of the script
    if (qualifier == null && (resolved == null || resolved instanceof GrBindingVariable || resolved instanceof LightElement && !(resolved instanceof ClosureSyntheticParameter)) && (referenceExpression.getParent() instanceof GrIndexProperty || !(referenceExpression.getParent() instanceof GrCall)) && PsiUtil.getContextClass(referenceExpression) instanceof GroovyScriptClass) {
        final GrExpression thisExpr = factory.createExpressionFromText("this", referenceExpression);
        thisExpr.accept(this);
        builder.append(".getBinding().getProperty(\"").append(referenceExpression.getReferenceName()).append("\")");
        return;
    }
    final IElementType type = referenceExpression.getDotTokenType();
    GrExpression qualifierToUse = qualifier;
    if (type == GroovyTokenTypes.mMEMBER_POINTER) {
        LOG.assertTrue(qualifier != null);
        builder.append("new ").append(GroovyCommonClassNames.ORG_CODEHAUS_GROOVY_RUNTIME_METHOD_CLOSURE).append('(');
        qualifier.accept(this);
        builder.append(", \"").append(referenceName).append("\")");
        return;
    }
    if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
        LOG.assertTrue(qualifier != null);
        String qualifierName = createVarByInitializer(qualifier);
        builder.append('(').append(qualifierName).append(" == null ? null : ");
        qualifierToUse = factory.createReferenceExpressionFromText(qualifierName, referenceExpression);
    }
    if (resolveResult.isInvokedOnProperty()) {
        //property-style access to accessor (e.g. qual.prop should be translated to qual.getProp())
        LOG.assertTrue(resolved instanceof PsiMethod);
        LOG.assertTrue(GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) resolved));
        invokeMethodOn(((PsiMethod) resolved), qualifierToUse, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), referenceExpression);
    } else {
        if (qualifierToUse != null) {
            qualifierToUse.accept(this);
            builder.append('.');
        }
        if (resolved instanceof PsiNamedElement && !(resolved instanceof GrBindingVariable)) {
            final String refName = ((PsiNamedElement) resolved).getName();
            if (resolved instanceof GrVariable && context.analyzedVars.toWrap((GrVariable) resolved)) {
                //this var should be wrapped by groovy.lang.Reference. so we add .get() tail.
                builder.append(context.analyzedVars.toVarName((GrVariable) resolved));
                if (!PsiUtil.isAccessedForWriting(referenceExpression)) {
                    builder.append(".get()");
                }
            } else {
                builder.append(refName);
            }
        } else {
            //unresolved reference
            if (referenceName != null) {
                if (PsiUtil.isAccessedForWriting(referenceExpression)) {
                    builder.append(referenceName);
                } else {
                    PsiType stringType = PsiType.getJavaLangString(referenceExpression.getManager(), referenceExpression.getResolveScope());
                    PsiType qualifierType = PsiImplUtil.getQualifierType(referenceExpression);
                    GroovyResolveResult[] candidates = qualifierType != null ? ResolveUtil.getMethodCandidates(qualifierType, "getProperty", referenceExpression, stringType) : GroovyResolveResult.EMPTY_ARRAY;
                    final PsiElement method = PsiImplUtil.extractUniqueElement(candidates);
                    if (method != null) {
                        builder.append("getProperty(\"").append(referenceName).append("\")");
                    } else {
                        builder.append(referenceName);
                    }
                }
            } else {
                final PsiElement nameElement = referenceExpression.getReferenceNameElement();
                if (nameElement instanceof GrExpression) {
                    ((GrExpression) nameElement).accept(this);
                } else if (nameElement != null) {
                    builder.append(nameElement.toString());
                }
            }
        }
    }
    if (type == GroovyTokenTypes.mOPTIONAL_DOT) {
        builder.append(')');
    }
}
Also used : GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) LightElement(com.intellij.psi.impl.light.LightElement) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) 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) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 2 with GrIndexProperty

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

the class ExpressionGenerator method visitBuiltinTypeClassExpression.

@Override
public void visitBuiltinTypeClassExpression(@NotNull GrBuiltinTypeClassExpression expression) {
    PsiElement firstChild = expression.getFirstChild();
    LOG.assertTrue(firstChild != null);
    ASTNode node = firstChild.getNode();
    LOG.assertTrue(node != null);
    final IElementType type = node.getElementType();
    final String boxed = TypesUtil.getBoxedTypeName(type);
    builder.append(boxed);
    if (expression.getParent() instanceof GrIndexProperty) {
        builder.append("[]");
    }
    builder.append(".class");
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrIndexProperty(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty) ASTNode(com.intellij.lang.ASTNode) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 3 with GrIndexProperty

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty 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 4 with GrIndexProperty

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty 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 5 with GrIndexProperty

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty 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)

Aggregations

GrIndexProperty (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrIndexProperty)11 IElementType (com.intellij.psi.tree.IElementType)5 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 PsiElement (com.intellij.psi.PsiElement)4 Nullable (org.jetbrains.annotations.Nullable)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)2 GrBindingVariable (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable)2 ASTNode (com.intellij.lang.ASTNode)1 PsiType (com.intellij.psi.PsiType)1 LightElement (com.intellij.psi.impl.light.LightElement)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrSpreadArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrSpreadArgument)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1