use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class CustomAnnotationChecker method checkAnnotationValueByType.
public static void checkAnnotationValueByType(@NotNull AnnotationHolder holder, @NotNull GrAnnotationMemberValue value, @Nullable PsiType ltype, boolean skipArrays) {
final GlobalSearchScope resolveScope = value.getResolveScope();
final PsiManager manager = value.getManager();
if (value instanceof GrExpression) {
final PsiType rtype;
if (value instanceof GrClosableBlock) {
rtype = PsiType.getJavaLangClass(manager, resolveScope);
} else {
rtype = ((GrExpression) value).getType();
}
if (rtype != null && !checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
}
} else if (value instanceof GrAnnotation) {
final PsiElement resolved = ((GrAnnotation) value).getClassReference().resolve();
if (resolved instanceof PsiClass) {
final PsiClassType rtype = JavaPsiFacade.getElementFactory(value.getProject()).createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
}
}
} else if (value instanceof GrAnnotationArrayInitializer) {
if (ltype instanceof PsiArrayType) {
final PsiType componentType = ((PsiArrayType) ltype).getComponentType();
final GrAnnotationMemberValue[] initializers = ((GrAnnotationArrayInitializer) value).getInitializers();
for (GrAnnotationMemberValue initializer : initializers) {
checkAnnotationValueByType(holder, initializer, componentType, false);
}
} else {
final PsiType rtype = TypesUtil.getTupleByAnnotationArrayInitializer((GrAnnotationArrayInitializer) value);
if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class GrAliasAnnotationChecker method checkArgumentList.
@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
final PsiAnnotation annotationCollector = GrAnnotationCollector.findAnnotationCollector(annotation);
if (annotationCollector == null) {
return false;
}
final ArrayList<GrAnnotation> annotations = ContainerUtil.newArrayList();
final Set<String> usedAttributes = GrAnnotationCollector.collectAnnotations(annotations, annotation, annotationCollector);
AliasedAnnotationHolder aliasedHolder = new AliasedAnnotationHolder(holder, annotation);
AnnotationChecker checker = new AnnotationChecker(aliasedHolder);
for (GrAnnotation aliased : annotations) {
checker.checkAnnotationArgumentList(aliased);
}
final GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
final String aliasQName = annotation.getQualifiedName();
if (attributes.length == 1 && attributes[0].getNameIdentifierGroovy() == null && !usedAttributes.contains("value")) {
holder.createErrorAnnotation(attributes[0], GroovyBundle.message("at.interface.0.does.not.contain.attribute", aliasQName, "value"));
}
for (GrAnnotationNameValuePair pair : attributes) {
final PsiElement nameIdentifier = pair.getNameIdentifierGroovy();
if (nameIdentifier != null && !usedAttributes.contains(pair.getName())) {
holder.createErrorAnnotation(nameIdentifier, GroovyBundle.message("at.interface.0.does.not.contain.attribute", aliasQName, pair.getName()));
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class SpockUnrollReferenceProvider method getReferencesByElement.
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
GrAnnotationNameValuePair nvp = (GrAnnotationNameValuePair) element.getParent();
String name = nvp.getName();
if (name != null && !name.equals("value"))
return PsiReference.EMPTY_ARRAY;
PsiElement argumentList = nvp.getParent();
if (!(argumentList instanceof GrAnnotationArgumentList))
return PsiReference.EMPTY_ARRAY;
PsiElement eAnnotation = argumentList.getParent();
if (!(eAnnotation instanceof GrAnnotation))
return PsiReference.EMPTY_ARRAY;
GrAnnotation annotation = (GrAnnotation) eAnnotation;
String shortName = annotation.getShortName();
if (!shortName.equals("Unroll") && !shortName.equals("spock.lang.Unroll"))
return PsiReference.EMPTY_ARRAY;
PsiElement modifierList = annotation.getParent();
if (!(modifierList instanceof GrModifierList))
return PsiReference.EMPTY_ARRAY;
PsiElement eMethod = modifierList.getParent();
if (!(eMethod instanceof GrMethod))
return PsiReference.EMPTY_ARRAY;
final GrMethod method = (GrMethod) eMethod;
ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
TextRange rangeInElement = manipulator.getRangeInElement(element);
String text = rangeInElement.substring(element.getText());
final List<SpockVariableReference> references = new ArrayList<>();
Matcher matcher = PATTERN.matcher(text);
while (matcher.find()) {
TextRange range = new TextRange(rangeInElement.getStartOffset() + matcher.start(1), rangeInElement.getStartOffset() + matcher.end(1));
references.add(new SpockVariableReference(element, range, references, method));
}
return references.toArray(new PsiReference[references.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class GrAnnotationNameValuePairImpl method multiResolve.
@NotNull
@Override
public GroovyResolveResult[] multiResolve(boolean incompleteCode) {
GrAnnotation annotation = PsiImplUtil.getAnnotation(this);
if (annotation != null) {
GrCodeReferenceElement ref = annotation.getClassReference();
PsiElement resolved = ref.resolve();
String declaredName = getName();
String name = declaredName == null ? PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME : declaredName;
if (resolved instanceof PsiClass) {
final PsiAnnotation collector = GrAnnotationCollector.findAnnotationCollector((PsiClass) resolved);
if (collector != null) {
return multiResolveFromAlias(annotation, name, collector);
}
if (((PsiClass) resolved).isAnnotationType()) {
return multiResolveFromAnnotationType((PsiClass) resolved, name);
}
}
}
return GroovyResolveResult.EMPTY_ARRAY;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class GrAnnotationNameValuePairImpl method multiResolveFromAlias.
private static GroovyResolveResult[] multiResolveFromAlias(@NotNull GrAnnotation alias, @NotNull String name, @NotNull PsiAnnotation annotationCollector) {
List<GroovyResolveResult> result = ContainerUtilRt.newArrayList();
List<GrAnnotation> annotations = ContainerUtilRt.newArrayList();
GrAnnotationCollector.collectAnnotations(annotations, alias, annotationCollector);
for (GrAnnotation annotation : annotations) {
final PsiElement clazz = annotation.getClassReference().resolve();
if (clazz instanceof PsiClass && ((PsiClass) clazz).isAnnotationType()) {
if (GroovyCommonClassNames.GROOVY_TRANSFORM_ANNOTATION_COLLECTOR.equals(((PsiClass) clazz).getQualifiedName()))
continue;
for (PsiMethod method : ((PsiClass) clazz).findMethodsByName(name, false)) {
result.add(new GroovyResolveResultImpl(method, true));
}
}
}
return result.toArray(new GroovyResolveResult[result.size()]);
}
Aggregations