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]);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations