Search in sources :

Example 6 with GrAnnotation

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()));
            }
        }
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 7 with GrAnnotation

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;
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiAnnotation(com.intellij.psi.PsiAnnotation) PsiElement(com.intellij.psi.PsiElement)

Example 8 with GrAnnotation

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()]);
}
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) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) Matcher(java.util.regex.Matcher) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) NotNull(org.jetbrains.annotations.NotNull)

Example 9 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 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;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) NotNull(org.jetbrains.annotations.NotNull)

Example 10 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 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()]);
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl)

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