Search in sources :

Example 36 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-community by JetBrains.

the class NewInstanceOfSingletonInspection method buildVisitor.

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

        @Override
        public void visitNewExpression(@NotNull GrNewExpression newExpression) {
            if (newExpression.getArrayDeclaration() != null)
                return;
            GrCodeReferenceElement refElement = newExpression.getReferenceElement();
            if (refElement == null)
                return;
            PsiElement resolved = refElement.resolve();
            if (!(resolved instanceof GrTypeDefinition))
                return;
            PsiAnnotation annotation = AnnotationUtil.findAnnotation((GrTypeDefinition) resolved, GROOVY_LANG_SINGLETON);
            if (annotation == null)
                return;
            registerError(newExpression, GroovyInspectionBundle.message("new.instance.of.singleton"), ContainerUtil.ar(new ReplaceWithInstanceAccessFix()), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }
    };
}
Also used : GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiAnnotation(com.intellij.psi.PsiAnnotation) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-community by JetBrains.

the class GrAliasAnnotationChecker method getAliasedAnnotations.

@Nullable
private static ArrayList<GrAnnotation> getAliasedAnnotations(GrAnnotation annotation) {
    final PsiAnnotation annotationCollector = GrAnnotationCollector.findAnnotationCollector(annotation);
    if (annotationCollector == null)
        return null;
    final ArrayList<GrAnnotation> aliasedAnnotations = ContainerUtil.newArrayList();
    GrAnnotationCollector.collectAnnotations(aliasedAnnotations, annotation, annotationCollector);
    return aliasedAnnotations;
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) PsiAnnotation(com.intellij.psi.PsiAnnotation) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-community by JetBrains.

the class GrAnnotationArgumentListImpl method addInternal.

@Override
public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) {
    if (first.getElementType() == GroovyElementTypes.ANNOTATION_MEMBER_VALUE_PAIR && last.getElementType() == GroovyElementTypes.ANNOTATION_MEMBER_VALUE_PAIR) {
        ASTNode lparenth = getNode().getFirstChildNode();
        ASTNode rparenth = getNode().getLastChildNode();
        if (lparenth == null) {
            getNode().addLeaf(GroovyTokenTypes.mLPAREN, "(", null);
        }
        if (rparenth == null) {
            getNode().addLeaf(GroovyTokenTypes.mRPAREN, ")", null);
        }
        final PsiNameValuePair[] nodes = getAttributes();
        if (nodes.length == 1) {
            final PsiNameValuePair pair = nodes[0];
            if (pair.getName() == null) {
                final String text = pair.getValue().getText();
                try {
                    final PsiAnnotation annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@AAA(value = " + text + ")");
                    getNode().replaceChild(pair.getNode(), annotation.getParameterList().getAttributes()[0].getNode());
                } catch (IncorrectOperationException e) {
                    LOG.error(e);
                }
            }
        }
        if (anchor == null && before != null) {
            anchor = before.booleanValue() ? getNode().getLastChildNode() : getNode().getFirstChildNode();
        }
    }
    return super.addInternal(first, last, anchor, before);
}
Also used : PsiNameValuePair(com.intellij.psi.PsiNameValuePair) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 39 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-community by JetBrains.

the class AnnotationAttributeCompletionResultProcessor method process.

public void process(@NotNull Consumer<LookupElement> consumer, @NotNull PrefixMatcher matcher) {
    GrCodeReferenceElement ref = myAnnotation.getClassReference();
    PsiElement resolved = ref.resolve();
    if (resolved instanceof PsiClass) {
        final PsiAnnotation annotationCollector = GrAnnotationCollector.findAnnotationCollector((PsiClass) resolved);
        if (annotationCollector != null) {
            final ArrayList<GrAnnotation> annotations = ContainerUtil.newArrayList();
            GrAnnotationCollector.collectAnnotations(annotations, myAnnotation, annotationCollector);
            Set<String> usedNames = ContainerUtil.newHashSet();
            for (GrAnnotation annotation : annotations) {
                final PsiElement resolvedAliased = annotation.getClassReference().resolve();
                if (resolvedAliased instanceof PsiClass && ((PsiClass) resolvedAliased).isAnnotationType()) {
                    for (PsiMethod method : ((PsiClass) resolvedAliased).getMethods()) {
                        if (usedNames.add(method.getName())) {
                            for (LookupElement element : GroovyCompletionUtil.createLookupElements(new GroovyResolveResultImpl(method, true), false, matcher, null)) {
                                consumer.consume(element);
                            }
                        }
                    }
                }
            }
        } else if (((PsiClass) resolved).isAnnotationType()) {
            for (PsiMethod method : ((PsiClass) resolved).getMethods()) {
                for (LookupElement element : GroovyCompletionUtil.createLookupElements(new GroovyResolveResultImpl(method, true), false, matcher, null)) {
                    consumer.consume(element);
                }
            }
        }
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiAnnotation(com.intellij.psi.PsiAnnotation) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl)

Example 40 with PsiAnnotation

use of com.intellij.psi.PsiAnnotation in project intellij-community by JetBrains.

the class ExternalBuilderStrategySupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    PsiAnnotation annotation = PsiImplUtil.getAnnotation(context.getCodeClass(), BUILDER_FQN);
    if (!isApplicable(annotation, EXTERNAL_STRATEGY_NAME))
        return;
    final PsiClass constructedClass = GrAnnotationUtil.inferClassAttribute(annotation, "forClass");
    if (constructedClass == null || "groovy.transform.Undefined.CLASS".equals(constructedClass.getQualifiedName()))
        return;
    if (constructedClass instanceof GrTypeDefinition) {
        for (GrField field : ((GrTypeDefinition) constructedClass).getCodeFields()) {
            context.addMethod(DefaultBuilderStrategySupport.createFieldSetter(context.getCodeClass(), field, annotation));
        }
    } else {
        for (PsiMethod setter : PropertyUtil.getAllProperties(constructedClass, true, false).values()) {
            final PsiMethod builderSetter = createFieldSetter(context.getCodeClass(), setter, annotation);
            if (builderSetter != null)
                context.addMethod(builderSetter);
        }
    }
    context.addMethod(createBuildMethod(annotation, createType(constructedClass)));
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Aggregations

PsiAnnotation (com.intellij.psi.PsiAnnotation)61 PsiModifierList (com.intellij.psi.PsiModifierList)18 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)14 PsiMethod (com.intellij.psi.PsiMethod)14 NotNull (org.jetbrains.annotations.NotNull)13 Project (com.intellij.openapi.project.Project)12 PsiClass (com.intellij.psi.PsiClass)11 PsiElement (com.intellij.psi.PsiElement)9 MockProblemDescriptor (com.intellij.testFramework.MockProblemDescriptor)6 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)5 PsiParameter (com.intellij.psi.PsiParameter)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5 Nullable (org.jetbrains.annotations.Nullable)5 PsiParameterList (com.intellij.psi.PsiParameterList)4 TestSize (com.google.idea.blaze.base.dependencies.TestSize)3 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)3 PsiNameValuePair (com.intellij.psi.PsiNameValuePair)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Nullable (javax.annotation.Nullable)3