Search in sources :

Example 81 with GrExpression

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

the class CustomAnnotationChecker method checkAnnotationValueByType.

public static void checkAnnotationValueByType(@NotNull AnnotationHolder holder, @NotNull GrAnnotationMemberValue value, @Nullable PsiType ltype, boolean skipArrays) {
    final GlobalSearchScope resolveScope = value.getResolveScope();
    final PsiManager manager = value.getManager();
    if (value instanceof GrExpression) {
        final PsiType rtype;
        if (value instanceof GrClosableBlock) {
            rtype = PsiType.getJavaLangClass(manager, resolveScope);
        } else {
            rtype = ((GrExpression) value).getType();
        }
        if (rtype != null && !checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
            holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
        }
    } else if (value instanceof GrAnnotation) {
        final PsiElement resolved = ((GrAnnotation) value).getClassReference().resolve();
        if (resolved instanceof PsiClass) {
            final PsiClassType rtype = JavaPsiFacade.getElementFactory(value.getProject()).createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
            if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
                holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
            }
        }
    } else if (value instanceof GrAnnotationArrayInitializer) {
        if (ltype instanceof PsiArrayType) {
            final PsiType componentType = ((PsiArrayType) ltype).getComponentType();
            final GrAnnotationMemberValue[] initializers = ((GrAnnotationArrayInitializer) value).getInitializers();
            for (GrAnnotationMemberValue initializer : initializers) {
                checkAnnotationValueByType(holder, initializer, componentType, false);
            }
        } else {
            final PsiType rtype = TypesUtil.getTupleByAnnotationArrayInitializer((GrAnnotationArrayInitializer) value);
            if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
                holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
            }
        }
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 82 with GrExpression

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

the class GrInspectionUtil method replaceExpression.

public static void replaceExpression(GrExpression expression, String newExpression) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
    final GrExpression newCall = factory.createExpressionFromText(newExpression);
    expression.replaceWithExpression(newCall, true);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 83 with GrExpression

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

the class GrHighlightUtil method isScriptPropertyAccess.

private static boolean isScriptPropertyAccess(GrReferenceExpression refExpr) {
    final GrExpression qualifier = refExpr.getQualifierExpression();
    if (qualifier == null) {
        final PsiClass clazz = PsiTreeUtil.getParentOfType(refExpr, PsiClass.class);
        if (clazz == null) {
            //script
            return true;
        }
        //in class, a property should normally be defined, so it's not a declaration
        return false;
    }
    final PsiType type = qualifier.getType();
    if (type instanceof PsiClassType && !(qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof GroovyScriptClass)) {
        final PsiClassType classType = (PsiClassType) type;
        final PsiClass psiClass = classType.resolve();
        if (psiClass instanceof GroovyScriptClass) {
            return true;
        }
    }
    return false;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 84 with GrExpression

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

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

the class GroovyPointlessArithmeticInspection method calculateReplacementExpression.

private static String calculateReplacementExpression(GrExpression expression) {
    final GrBinaryExpression exp = (GrBinaryExpression) expression;
    final IElementType sign = exp.getOperationTokenType();
    final GrExpression lhs = exp.getLeftOperand();
    final GrExpression rhs = exp.getRightOperand();
    assert rhs != null;
    if (GroovyTokenTypes.mPLUS == sign) {
        if (isZero(lhs)) {
            return rhs.getText();
        } else {
            return lhs.getText();
        }
    }
    if (GroovyTokenTypes.mMINUS == sign) {
        return lhs.getText();
    }
    if (GroovyTokenTypes.mSTAR == sign) {
        if (isOne(lhs)) {
            return rhs.getText();
        } else if (isOne(rhs)) {
            return lhs.getText();
        } else {
            return "0";
        }
    }
    if (GroovyTokenTypes.mDIV == sign) {
        return lhs.getText();
    }
    return "";
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16