Search in sources :

Example 21 with GrCodeReferenceElement

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

the class FieldAnnotationChecker method checkApplicability.

@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    final String qname = annotation.getQualifiedName();
    if (!GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD.equals(qname))
        return false;
    checkScriptField(holder, annotation);
    PsiElement annoParent = annotation.getParent();
    PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
    if (!(ownerToUse instanceof GrVariableDeclaration)) {
        return false;
    } else {
        GrVariableDeclaration declaration = (GrVariableDeclaration) ownerToUse;
        if (declaration.getVariables().length != 1 || !PsiUtil.isLocalVariable(declaration.getVariables()[0])) {
            return false;
        }
    }
    if (!GrAnnotationImpl.isAnnotationApplicableTo(annotation, PsiAnnotation.TargetType.LOCAL_VARIABLE)) {
        GrCodeReferenceElement ref = annotation.getClassReference();
        String target = JavaErrorMessages.message("annotation.target.LOCAL_VARIABLE");
        String description = JavaErrorMessages.message("annotation.not.applicable", ref.getText(), target);
        holder.createErrorAnnotation(ref, description);
    }
    return true;
}
Also used : GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PsiElement(com.intellij.psi.PsiElement) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 22 with GrCodeReferenceElement

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

the class TypeCheckedAnnotationChecker method checkArgumentList.

@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    final GrCodeReferenceElement classReference = annotation.getClassReference();
    PsiElement resolved = classReference.resolve();
    if (!(resolved instanceof PsiClass && GroovyCommonClassNames.GROOVY_TRANSFORM_TYPE_CHECKED.equals(((PsiClass) resolved).getQualifiedName()))) {
        return false;
    }
    String sdkVersion = GroovyConfigUtils.getInstance().getSDKVersion(annotation);
    if (!("2.1".equals(sdkVersion) || "2.1.0".equals(sdkVersion)))
        return false;
    GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    checkAnnotationArguments(holder, (PsiClass) resolved, classReference, attributes, false);
    return true;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement)

Example 23 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement 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 24 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement 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 25 with GrCodeReferenceElement

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

the class GroovyFileImpl method setPackageName.

@Override
public void setPackageName(String packageName) {
    final ASTNode fileNode = getNode();
    final GrPackageDefinition currentPackage = getPackageDefinition();
    if (packageName == null || packageName.isEmpty()) {
        if (currentPackage != null) {
            final ASTNode currNode = currentPackage.getNode();
            fileNode.removeChild(currNode);
        }
        return;
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    final GrPackageDefinition newPackage = (GrPackageDefinition) factory.createTopElementFromText("package " + packageName);
    if (currentPackage != null) {
        final GrCodeReferenceElement packageReference = currentPackage.getPackageReference();
        if (packageReference != null) {
            GrCodeReferenceElement ref = newPackage.getPackageReference();
            if (ref != null) {
                packageReference.replace(ref);
            }
            return;
        }
    }
    final ASTNode newNode = newPackage.getNode();
    if (currentPackage != null) {
        final ASTNode currNode = currentPackage.getNode();
        fileNode.replaceChild(currNode, newNode);
    } else {
        ASTNode anchor = fileNode.getFirstChildNode();
        if (anchor != null && anchor.getElementType() == GroovyTokenTypes.mSH_COMMENT) {
            anchor = anchor.getTreeNext();
            fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
        }
        fileNode.addChild(newNode, anchor);
        if (anchor != null && !anchor.getText().startsWith("\n\n")) {
            fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)

Aggregations

GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)71 NotNull (org.jetbrains.annotations.NotNull)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)12 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)5 PsiClass (com.intellij.psi.PsiClass)4 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)4 GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)4 Editor (com.intellij.openapi.editor.Editor)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3