Search in sources :

Example 51 with GrReferenceExpression

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

the class GrRefactoringConflictsUtil method checkUsedElements.

public static void checkUsedElements(PsiMember member, PsiElement scope, @NotNull Set<GrMember> membersToMove, @Nullable Set<PsiMethod> abstractMethods, @Nullable PsiClass targetClass, @NotNull PsiElement context, MultiMap<PsiElement, String> conflicts) {
    final Set<PsiMember> moving = new HashSet<>(membersToMove);
    if (abstractMethods != null) {
        moving.addAll(abstractMethods);
    }
    if (scope instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                GrExpression qualifier = refExpr.getQualifierExpression();
                PsiClass accessClass = (PsiClass) (qualifier != null ? PsiUtil.getAccessObjectClass(qualifier).getElement() : null);
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, accessClass, member, conflicts);
            }
        }
    } else if (scope instanceof GrNewExpression) {
        final GrNewExpression newExpression = (GrNewExpression) scope;
        final GrAnonymousClassDefinition anonymousClass = newExpression.getAnonymousClassDefinition();
        if (anonymousClass != null) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(anonymousClass, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility(anonymousClass, context, anonymousClass, member, conflicts);
            }
        } else {
            final PsiMethod refElement = newExpression.resolveMethod();
            if (refElement != null) {
                if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                    RefactoringConflictsUtil.checkAccessibility(refElement, context, null, member, conflicts);
                }
            }
        }
    } else if (scope instanceof GrCodeReferenceElement) {
        GrCodeReferenceElement refExpr = (GrCodeReferenceElement) scope;
        PsiElement refElement = refExpr.resolve();
        if (refElement instanceof PsiMember) {
            if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
                RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, null, member, conflicts);
            }
        }
    }
    for (PsiElement child : scope.getChildren()) {
        if (child instanceof PsiWhiteSpace || child instanceof PsiComment)
            continue;
        checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts);
    }
}
Also used : GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 52 with GrReferenceExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project android by JetBrains.

the class AndroidGradleSpellcheckingStrategy method isPrint.

private static boolean isPrint(PsiElement element) {
    PsiElement parent0 = element.getParent();
    if (parent0 == null) {
        return false;
    }
    PsiElement parent1 = parent0.getParent();
    if (parent1 == null) {
        return false;
    }
    PsiElement parent2 = parent1.getParent();
    if (parent2 == null) {
        return false;
    }
    if (parent2 instanceof GrCommandArgumentList) {
        parent2 = parent2.getParent();
    }
    if (parent2 instanceof GrApplicationStatement) {
        GrApplicationStatement call = (GrApplicationStatement) parent2;
        GrExpression propertyExpression = call.getInvokedExpression();
        if (propertyExpression instanceof GrReferenceExpression) {
            GrReferenceExpression propertyRef = (GrReferenceExpression) propertyExpression;
            String property = propertyRef.getReferenceName();
            if ("print".equals(property) || "println".equals(property)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) 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)

Example 53 with GrReferenceExpression

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

the class GradleUnresolvedReferenceFilter method isReject.

@Override
public boolean isReject(@NotNull GrReferenceExpression expression) {
    final PsiType psiType = GradleResolverUtil.getTypeOf(expression);
    if (psiType == null) {
        PsiElement child = expression.getFirstChild();
        if (child == null)
            return false;
        PsiReference reference = child.getReference();
        if (reference instanceof GrReferenceExpression) {
            PsiType type = ((GrReferenceExpression) reference).getType();
            if (type != null) {
                PsiClassType extType = createGenericType(GRADLE_API_EXTRA_PROPERTIES_EXTENSION, expression, null);
                return TypeConversionUtil.areTypesConvertible(type, extType);
            }
        }
        return false;
    }
    Set<String> toIgnore = new HashSet<>(IGNORE_SET);
    GradleExtensionsSettings.GradleExtensionsData extensionsData = GradleExtensionsContributor.Companion.getExtensionsFor(expression);
    if (extensionsData != null) {
        for (GradleExtensionsSettings.GradleExtension extension : extensionsData.extensions) {
            if (StringUtil.isNotEmpty(extension.namedObjectTypeFqn)) {
                toIgnore.add(extension.namedObjectTypeFqn);
            }
        }
    }
    return toIgnore.contains(TypesUtil.getQualifiedName(psiType));
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) PsiReference(com.intellij.psi.PsiReference) GradleExtensionsSettings(org.jetbrains.plugins.gradle.settings.GradleExtensionsSettings) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) HashSet(java.util.HashSet) ContainerUtil.newHashSet(com.intellij.util.containers.ContainerUtil.newHashSet)

Example 54 with GrReferenceExpression

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

the class SecondUnsafeCallQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (!(element instanceof GrReferenceExpression))
        return;
    final PsiElement newDot = GroovyPsiElementFactory.getInstance(project).createDotToken(GroovyTokenTypes.mOPTIONAL_DOT.toString());
    ((GrReferenceExpression) element).replaceDotToken(newDot);
}
Also used : PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 55 with GrReferenceExpression

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

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11