Search in sources :

Example 26 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrAnnotationNameValuePairImpl method getDetachedValue.

@Override
@Nullable
public PsiAnnotationMemberValue getDetachedValue() {
    GrNameValuePairStub stub = getStub();
    if (stub != null) {
        String text = stub.getValue();
        PsiAnnotationMemberValue result = SoftReference.dereference(myDetachedValue);
        if (result == null) {
            GrAnnotation annotation = GroovyPsiElementFactory.getInstance(getProject()).createAnnotationFromText("@F(" + text + ")", this);
            ((LightVirtualFile) annotation.getContainingFile().getViewProvider().getVirtualFile()).setWritable(false);
            PsiAnnotationMemberValue value = annotation.findAttributeValue(null);
            myDetachedValue = new SoftReference<>(result = value);
        }
        return result;
    }
    return getValue();
}
Also used : GrNameValuePairStub(org.jetbrains.plugins.groovy.lang.psi.stubs.GrNameValuePairStub) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrAnnotationCollector method getResolvedAnnotations.

@NotNull
public static GrAnnotation[] getResolvedAnnotations(@NotNull GrModifierList modifierList) {
    final GrAnnotation[] rawAnnotations = modifierList.getRawAnnotations();
    if (!hasAliases(rawAnnotations))
        return rawAnnotations;
    List<GrAnnotation> result = ContainerUtil.newArrayList();
    for (GrAnnotation annotation : rawAnnotations) {
        final PsiAnnotation annotationCollector = findAnnotationCollector(annotation);
        if (annotationCollector != null) {
            collectAnnotations(result, annotation, annotationCollector);
        } else {
            result.add(annotation);
        }
    }
    return result.toArray(new GrAnnotation[result.size()]);
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class GrAnnotationCollector method collectAliasedAnnotationsFromAnnotationCollectorAnnotations.

private static void collectAliasedAnnotationsFromAnnotationCollectorAnnotations(@NotNull PsiModifierList modifierList, @NotNull Map<String, Map<String, PsiNameValuePair>> annotations) {
    PsiElement parent = modifierList.getParent();
    if (parent instanceof PsiClass && GroovyCommonClassNames.GROOVY_TRANSFORM_COMPILE_DYNAMIC.equals(((PsiClass) parent).getQualifiedName())) {
        Map<String, PsiNameValuePair> params = ContainerUtil.newLinkedHashMap();
        annotations.put(GroovyCommonClassNames.GROOVY_TRANSFORM_COMPILE_STATIC, params);
        GrAnnotation annotation = GroovyPsiElementFactory.getInstance(modifierList.getProject()).createAnnotationFromText("@CompileStatic(TypeCheckingMode.SKIP)");
        params.put("value", annotation.getParameterList().getAttributes()[0]);
        return;
    }
    PsiAnnotation[] rawAnnotations = modifierList instanceof GrModifierList ? ((GrModifierList) modifierList).getRawAnnotations() : modifierList.getAnnotations();
    for (PsiAnnotation annotation : rawAnnotations) {
        final String qname = annotation.getQualifiedName();
        if (qname == null || qname.equals(GroovyCommonClassNames.GROOVY_TRANSFORM_ANNOTATION_COLLECTOR))
            continue;
        final PsiNameValuePair[] attributes = annotation.getParameterList().getAttributes();
        for (PsiNameValuePair pair : attributes) {
            Map<String, PsiNameValuePair> map = annotations.get(qname);
            if (map == null) {
                map = ContainerUtil.newLinkedHashMap();
                annotations.put(qname, map);
            }
            map.put(pair.getName() != null ? pair.getName() : "value", pair);
        }
        if (attributes.length == 0 && !annotations.containsKey(qname)) {
            annotations.put(qname, ContainerUtil.<String, PsiNameValuePair>newLinkedHashMap());
        }
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)

Example 29 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation 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 30 with GrAnnotation

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.

the class ModifierListGenerator method writeModifiers.

public static boolean writeModifiers(StringBuilder builder, PsiModifierList modifierList, String[] modifiers, boolean writeAnnotations) {
    boolean wasAddedModifiers = false;
    if (writeAnnotations && modifierList instanceof GrModifierList) {
        GrAnnotation[] annotations = ((GrModifierList) modifierList).getAnnotations();
        AnnotationGenerator annotationGenerator = new AnnotationGenerator(builder, new ExpressionContext(modifierList.getProject(), GroovyFile.EMPTY_ARRAY));
        wasAddedModifiers = annotations.length > 0;
        for (GrAnnotation annotation : annotations) {
            annotation.accept(annotationGenerator);
            builder.append(' ');
        }
    }
    for (String modifierType : modifiers) {
        if (modifierList.hasModifierProperty(modifierType)) {
            builder.append(modifierType);
            builder.append(' ');
            wasAddedModifiers = true;
        }
    }
    return wasAddedModifiers;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)

Aggregations

GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)30 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)9 NotNull (org.jetbrains.annotations.NotNull)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)5 PsiElement (com.intellij.psi.PsiElement)4 GrAnnotationNameValuePair (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair)4 PsiAnnotation (com.intellij.psi.PsiAnnotation)3 Nullable (org.jetbrains.annotations.Nullable)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GrAnnotationMemberValue (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)1 CantRunException (com.intellij.execution.CantRunException)1 ExecutionException (com.intellij.execution.ExecutionException)1 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1