Search in sources :

Example 16 with GroovyResolveResult

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

the class ExpressionGenerator method visitUnaryExpression.

@Override
public void visitUnaryExpression(@NotNull GrUnaryExpression expression) {
    final boolean postfix = expression.isPostfix();
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
    final PsiElement resolved = resolveResult.getElement();
    final GrExpression operand = expression.getOperand();
    IElementType opType = expression.getOperationTokenType();
    if (resolved instanceof PsiMethod) {
        if (opType == GroovyTokenTypes.mINC || opType == GroovyTokenTypes.mDEC) {
            if (!postfix || expression.getParent() instanceof GrStatementOwner || expression.getParent() instanceof GrControlStatement) {
                if (generatePrefixIncDec((PsiMethod) resolved, operand, expression))
                    return;
            }
        }
        if (operand != null && shouldNotReplaceOperatorWithMethod(operand.getType(), null, expression.getOperationTokenType())) {
            writeSimpleUnary(operand, expression, this);
        } else {
            if (opType == GroovyTokenTypes.mLNOT) {
                builder.append('!');
            }
            invokeMethodOn(((PsiMethod) resolved), operand, GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
        }
    } else if (operand != null) {
        if (postfix) {
            operand.accept(this);
            builder.append(expression.getOperationToken().getText());
        } else {
            builder.append(expression.getOperationToken().getText());
            operand.accept(this);
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 17 with GroovyResolveResult

use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult 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 18 with GroovyResolveResult

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

the class ExpressionGenerator method visitSafeCastExpression.

@Override
public void visitSafeCastExpression(@NotNull GrSafeCastExpression typeCastExpression) {
    final GrExpression operand = (GrExpression) PsiUtil.skipParenthesesIfSensibly(typeCastExpression.getOperand(), false);
    final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
    if (operand instanceof GrListOrMap && ((GrListOrMap) operand).isMap() && typeElement != null) {
        AnonymousFromMapGenerator.writeAnonymousMap((GrListOrMap) operand, typeElement, builder, context);
        return;
    }
    final PsiType type = typeElement.getType();
    if (operand instanceof GrListOrMap && !((GrListOrMap) operand).isMap() && type instanceof PsiArrayType) {
        builder.append("new ");
        final GrExpression[] initializers = ((GrListOrMap) operand).getInitializers();
        if (initializers.length == 0) {
            TypeWriter.writeTypeForNew(builder, ((PsiArrayType) type).getComponentType(), typeCastExpression);
            builder.append("[0]");
        } else {
            TypeWriter.writeTypeForNew(builder, type, typeCastExpression);
            builder.append('{');
            for (GrExpression initializer : initializers) {
                initializer.accept(this);
                builder.append(", ");
            }
            if (initializers.length > 0) {
                builder.delete(builder.length() - 2, builder.length());
            //builder.removeFromTheEnd(2);
            }
            builder.append('}');
        }
        return;
    }
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(typeCastExpression.multiResolve(false));
    final PsiElement resolved = resolveResult.getElement();
    if (resolved instanceof PsiMethod) {
        final GrExpression typeParam;
        try {
            typeParam = factory.createExpressionFromText(typeElement.getText(), typeCastExpression);
        } catch (IncorrectOperationException e) {
            generateCast(typeElement, operand);
            return;
        }
        invokeMethodOn(((PsiMethod) resolved), operand, new GrExpression[] { typeParam }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), typeCastExpression);
    } else {
        generateCast(typeElement, operand);
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 19 with GroovyResolveResult

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

the class ExpressionGenerator method hasFieldInitialization.

private static boolean hasFieldInitialization(GrNewExpression newExpression) {
    final GrArgumentList argumentList = newExpression.getArgumentList();
    if (argumentList == null)
        return false;
    if (argumentList.getNamedArguments().length == 0)
        return false;
    final GrCodeReferenceElement refElement = newExpression.getReferenceElement();
    if (refElement == null)
        return false;
    final GroovyResolveResult resolveResult = newExpression.advancedResolve();
    final PsiElement constructor = resolveResult.getElement();
    if (constructor instanceof PsiMethod) {
        return ((PsiMethod) constructor).getParameterList().getParametersCount() == 0;
    }
    final PsiElement resolved = refElement.resolve();
    return resolved instanceof PsiClass;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 20 with GroovyResolveResult

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

the class ExpressionGenerator method visitConditionalExpression.

@Override
public void visitConditionalExpression(@NotNull GrConditionalExpression expression) {
    final GrExpression condition = expression.getCondition();
    final GrExpression thenBranch = expression.getThenBranch();
    final GrExpression elseBranch = expression.getElseBranch();
    final boolean elvis = expression instanceof GrElvisExpression;
    final String var;
    if (elvis) {
        var = createVarByInitializer(condition);
    } else {
        var = null;
    }
    final PsiType type = condition.getType();
    if (type == null || PsiType.BOOLEAN.equals(TypesUtil.unboxPrimitiveTypeWrapper(type))) {
        if (elvis) {
            builder.append(var);
        } else {
            condition.accept(this);
        }
    } else {
        final GroovyResolveResult[] results = ResolveUtil.getMethodCandidates(type, "asBoolean", expression, PsiType.EMPTY_ARRAY);
        final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
        GenerationUtil.invokeMethodByResolveResult(elvis ? factory.createReferenceExpressionFromText(var, expression) : condition, result, "asBoolean", GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, this, expression);
    }
    builder.append('?');
    if (thenBranch != null) {
        if (elvis) {
            builder.append(var);
        } else {
            thenBranch.accept(this);
        }
    }
    builder.append(':');
    if (elseBranch != null) {
        elseBranch.accept(this);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)

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