Search in sources :

Example 11 with GrReferenceElement

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

the class GrDeprecatedAPIUsageInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
            super.visitReferenceExpression(ref);
            checkRef(ref);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement ref) {
            super.visitCodeReferenceElement(ref);
            checkRef(ref);
        }

        private void checkRef(GrReferenceElement ref) {
            PsiElement resolved = ref.resolve();
            if (isDeprecated(resolved)) {
                PsiElement toHighlight = getElementToHighlight(ref);
                registerError(toHighlight, GroovyBundle.message("0.is.deprecated", ref.getReferenceName()), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.LIKE_DEPRECATED);
            }
        }

        @NotNull
        public PsiElement getElementToHighlight(@NotNull GrReferenceElement refElement) {
            final PsiElement refNameElement = refElement.getReferenceNameElement();
            return refNameElement != null ? refNameElement : refElement;
        }

        private boolean isDeprecated(PsiElement resolved) {
            if (resolved instanceof PsiDocCommentOwner) {
                return ((PsiDocCommentOwner) resolved).isDeprecated();
            }
            if (resolved instanceof PsiModifierListOwner && PsiImplUtil.isDeprecatedByAnnotation((PsiModifierListOwner) resolved)) {
                return true;
            }
            return false;
        }
    };
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) PsiDocCommentOwner(com.intellij.psi.PsiDocCommentOwner) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) NotNull(org.jetbrains.annotations.NotNull) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with GrReferenceElement

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

the class UnnecessaryQualifiedReferenceInspection method canBeSimplified.

private static boolean canBeSimplified(PsiElement element) {
    if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null)
        return false;
    if (element instanceof GrCodeReferenceElement) {
        if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null)
            return false;
    } else if (element instanceof GrReferenceExpression) {
        if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element))
            return false;
    } else {
        return false;
    }
    final GrReferenceElement ref = (GrReferenceElement) element;
    if (ref.getQualifier() == null)
        return false;
    if (!(ref.getContainingFile() instanceof GroovyFileBase))
        return false;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiClass))
        return false;
    final String name = ((PsiClass) resolved).getName();
    if (name == null)
        return false;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
    final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
    final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
    if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
        return true;
    }
    final PsiClass containingClass = ((PsiClass) resolved).getContainingClass();
    if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()).insertInnerClassImports()) {
        return false;
    }
    return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 13 with GrReferenceElement

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

the class GroovyChangeUtilSupport method decodeInformation.

@Override
public TreeElement decodeInformation(TreeElement element, final Map<Object, Object> decodingState) {
    if (element instanceof CompositeElement) {
        if (element.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || element.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
            GrReferenceElement ref = (GrReferenceElement) SourceTreeToPsiMap.treeElementToPsi(element);
            final PsiMember refMember = element.getCopyableUserData(REFERENCED_MEMBER_KEY);
            if (refMember != null) {
                element.putCopyableUserData(REFERENCED_MEMBER_KEY, null);
                PsiElement refElement1 = ref.resolve();
                if (!refMember.getManager().areElementsEquivalent(refMember, refElement1)) {
                    try {
                        if (!(refMember instanceof PsiClass) || ref.getQualifier() == null) {
                            // can restore only if short (otherwise qualifier should be already restored)
                            ref = (GrReferenceElement) ref.bindToElement(refMember);
                        }
                    } catch (IncorrectOperationException ignored) {
                    }
                    return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
                }
            }
            return element;
        }
    }
    return null;
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 14 with GrReferenceElement

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

the class GroovyChangeUtilSupport method encodeInformation.

@Override
public void encodeInformation(final TreeElement element, final ASTNode original, final Map<Object, Object> encodingState) {
    if (original instanceof CompositeElement) {
        if (original.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || original.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
            PsiElement psi = original.getPsi();
            Project project = psi.getProject();
            if (!PsiUtil.isThisOrSuperRef(psi) && project.isInitialized() && !DumbService.isDumb(project)) {
                final GroovyResolveResult result = ((GrReferenceElement) psi).advancedResolve();
                if (result != null) {
                    final PsiElement target = result.getElement();
                    if (target instanceof PsiClass || (target instanceof PsiMethod || target instanceof PsiField) && ((PsiMember) target).hasModifierProperty(PsiModifier.STATIC) && result.getCurrentFileResolveContext() instanceof GrImportStatement) {
                        element.putCopyableUserData(REFERENCED_MEMBER_KEY, (PsiMember) target);
                    }
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 15 with GrReferenceElement

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

the class GrClassReferenceTypePointer method calcType.

@Nullable
@Override
protected GrClassReferenceType calcType() {
    final GrReferenceElement reference = mySmartPsiElementPointer.getElement();
    if (reference != null) {
        return new GrClassReferenceType(reference);
    }
    try {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
        GrTypeElement typeElement = factory.createTypeElement(myReferenceText, null);
        return (GrClassReferenceType) typeElement.getType();
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrClassReferenceType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)25 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)5 PsiElement (com.intellij.psi.PsiElement)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NotNull (org.jetbrains.annotations.NotNull)4 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)4 Nullable (org.jetbrains.annotations.Nullable)3 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)3 ReferenceData (com.intellij.codeInsight.editorActions.ReferenceData)2 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2