Search in sources :

Example 6 with GrAnnotationNameValuePair

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

the class GrLightAnnotation method addAttribute.

public void addAttribute(PsiNameValuePair pair) {
    if (pair instanceof GrAnnotationNameValuePair) {
        myAnnotationArgList.addAttribute((GrAnnotationNameValuePair) pair);
    } else {
        GrAnnotationMemberValue newValue = new AnnotationArgConverter().convert(pair.getValue());
        if (newValue == null)
            return;
        String name = pair.getName();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(pair.getProject());
        String annotationText;
        annotationText = name != null ? "@A(" + name + "=" + newValue.getText() + ")" : "@A(" + newValue.getText() + ")";
        GrAnnotation annotation = factory.createAnnotationFromText(annotationText);
        myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
    }
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) AnnotationArgConverter(org.jetbrains.plugins.groovy.lang.psi.impl.AnnotationArgConverter)

Example 7 with GrAnnotationNameValuePair

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

the class GroovyLanguageInjectionSupport method doInject.

private static boolean doInject(@NotNull String languageId, @NotNull PsiElement psiElement, @NotNull PsiLanguageInjectionHost host) {
    final PsiElement target = getTopLevelInjectionTarget(psiElement);
    final PsiElement parent = target.getParent();
    final Project project = psiElement.getProject();
    if (parent instanceof GrReturnStatement) {
        final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent);
        if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
            return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) owner.getParent(), -1, host, languageId);
        }
    } else if (parent instanceof GrMethod) {
        return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod) parent, -1, host, languageId);
    } else if (parent instanceof GrAnnotationNameValuePair) {
        final PsiReference ref = parent.getReference();
        if (ref != null) {
            final PsiElement resolved = ref.resolve();
            if (resolved instanceof PsiMethod) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) resolved, -1, host, languageId);
            }
        }
    } else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) {
        final PsiMethod method = ((GrMethodCall) parent.getParent()).resolveMethod();
        if (method != null) {
            final int index = GrInjectionUtil.findParameterIndex(target, ((GrMethodCall) parent.getParent()));
            if (index >= 0) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, host, languageId);
            }
        }
    } else if (parent instanceof GrAssignmentExpression) {
        final GrExpression expr = ((GrAssignmentExpression) parent).getLValue();
        if (expr instanceof GrReferenceExpression) {
            final PsiElement element = ((GrReferenceExpression) expr).resolve();
            if (element != null) {
                return doInject(languageId, element, host);
            }
        }
    } else {
        if (parent instanceof PsiVariable) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) parent, host, languageId, fixer))
                return true;
        } else if (target instanceof PsiVariable && !(target instanceof LightElement)) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) target, host, languageId, fixer))
                return true;
        }
    }
    return false;
}
Also used : Processor(com.intellij.util.Processor) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) LightElement(com.intellij.psi.impl.light.LightElement) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 8 with GrAnnotationNameValuePair

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

the class AnnotationCollectorChecker method checkArgumentList.

@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    if (!isInAliasDeclaration(annotation))
        return false;
    final PsiClass clazz = (PsiClass) annotation.getClassReference().resolve();
    if (clazz == null)
        return true;
    final GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    CustomAnnotationChecker.checkAnnotationArguments(holder, clazz, annotation.getClassReference(), attributes, false);
    return true;
}
Also used : GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiClass(com.intellij.psi.PsiClass)

Example 9 with GrAnnotationNameValuePair

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

the class GriffonPropertyListenerAnnotationChecker method checkArgumentList.

@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    if (!"griffon.transform.PropertyListener".equals(annotation.getQualifiedName()))
        return false;
    final GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    if (attributes.length != 1)
        return false;
    final GrAnnotationNameValuePair attribute = attributes[0];
    final GrAnnotationMemberValue value = attribute.getValue();
    final PsiAnnotationOwner owner = annotation.getOwner();
    if (owner instanceof GrField) {
        if (value instanceof GrClosableBlock) {
            return true;
        }
    } else if (owner instanceof GrTypeDefinition) {
        if (value instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) value).resolve();
            if (resolved instanceof GrField) {
                final PsiClass containingClass = ((GrField) resolved).getContainingClass();
                if (annotation.getManager().areElementsEquivalent((PsiElement) owner, containingClass)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiAnnotationOwner(com.intellij.psi.PsiAnnotationOwner) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 10 with GrAnnotationNameValuePair

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

the class GrAnnotationCollector method collectAnnotations.

/**
   *
   * @param list resulting collection of aliased annotations
   * @param alias alias annotation
   * @param annotationCollector @AnnotationCollector annotation used in alias declaration
   * @return set of used arguments of alias annotation
   */
@NotNull
public static Set<String> collectAnnotations(@NotNull List<GrAnnotation> list, @NotNull GrAnnotation alias, @NotNull PsiAnnotation annotationCollector) {
    final PsiModifierList modifierList = (PsiModifierList) annotationCollector.getParent();
    Map<String, Map<String, PsiNameValuePair>> annotations = ContainerUtil.newLinkedHashMap();
    collectAliasedAnnotationsFromAnnotationCollectorValueAttribute(annotationCollector, annotations);
    collectAliasedAnnotationsFromAnnotationCollectorAnnotations(modifierList, annotations);
    final PsiManager manager = alias.getManager();
    final GrAnnotationNameValuePair[] attributes = alias.getParameterList().getAttributes();
    Set<String> allUsedAttrs = ContainerUtil.newLinkedHashSet();
    for (Map.Entry<String, Map<String, PsiNameValuePair>> entry : annotations.entrySet()) {
        final String qname = entry.getKey();
        final PsiClass resolved = JavaPsiFacade.getInstance(alias.getProject()).findClass(qname, alias.getResolveScope());
        if (resolved == null)
            continue;
        final GrLightAnnotation annotation = new GrLightAnnotation(manager, alias.getLanguage(), qname, modifierList);
        Set<String> usedAttrs = ContainerUtil.newLinkedHashSet();
        for (GrAnnotationNameValuePair attr : attributes) {
            final String name = attr.getName() != null ? attr.getName() : "value";
            if (resolved.findMethodsByName(name, false).length > 0) {
                annotation.addAttribute(attr);
                allUsedAttrs.add(name);
                usedAttrs.add(name);
            }
        }
        final Map<String, PsiNameValuePair> defaults = entry.getValue();
        for (Map.Entry<String, PsiNameValuePair> defa : defaults.entrySet()) {
            if (!usedAttrs.contains(defa.getKey())) {
                annotation.addAttribute(defa.getValue());
            }
        }
        list.add(annotation);
    }
    return allUsedAttrs;
}
Also used : GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GrLightAnnotation(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightAnnotation) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrAnnotationNameValuePair (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair)10 PsiElement (com.intellij.psi.PsiElement)4 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)4 PsiClass (com.intellij.psi.PsiClass)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrAnnotationMemberValue (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 PsiAnnotationOwner (com.intellij.psi.PsiAnnotationOwner)1 LightElement (com.intellij.psi.impl.light.LightElement)1 Processor (com.intellij.util.Processor)1 HashSet (com.intellij.util.containers.HashSet)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1