Search in sources :

Example 1 with GrAnnotationArrayInitializer

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

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

the class GroovySuppressableInspectionTool method getInspectionIdsSuppressedInAnnotation.

@NotNull
private static Collection<String> getInspectionIdsSuppressedInAnnotation(final GrModifierList modifierList) {
    if (modifierList == null) {
        return Collections.emptyList();
    }
    PsiAnnotation annotation = modifierList.findAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME);
    if (annotation == null) {
        return Collections.emptyList();
    }
    final GrAnnotationMemberValue attributeValue = (GrAnnotationMemberValue) annotation.findAttributeValue(null);
    Collection<String> result = new ArrayList<>();
    if (attributeValue instanceof GrAnnotationArrayInitializer) {
        for (GrAnnotationMemberValue annotationMemberValue : ((GrAnnotationArrayInitializer) attributeValue).getInitializers()) {
            final String id = getInspectionIdSuppressedInAnnotationAttribute(annotationMemberValue);
            if (id != null) {
                result.add(id);
            }
        }
    } else {
        final String id = getInspectionIdSuppressedInAnnotationAttribute(attributeValue);
        if (id != null) {
            result.add(id);
        }
    }
    return result;
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) ArrayList(java.util.ArrayList) PsiAnnotation(com.intellij.psi.PsiAnnotation) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrAnnotationArrayInitializer

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

the class GroovyLanguageInjectionSupport method getTopLevelInjectionTarget.

@NotNull
public static PsiElement getTopLevelInjectionTarget(@NotNull final PsiElement host) {
    PsiElement target = host;
    PsiElement parent = target.getParent();
    for (; parent != null; target = parent, parent = target.getParent()) {
        if (parent instanceof GrBinaryExpression)
            continue;
        if (parent instanceof GrString)
            continue;
        if (parent instanceof GrParenthesizedExpression)
            continue;
        if (parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != target)
            continue;
        if (parent instanceof GrAnnotationArrayInitializer)
            continue;
        if (parent instanceof GrListOrMap) {
            parent = parent.getParent();
            continue;
        }
        break;
    }
    return target;
}
Also used : GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GrAnnotationArrayInitializer

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

the class SuppressForMemberFix method addSuppressAnnotation.

private static void addSuppressAnnotation(final Project project, final GrModifierList modifierList, final String id) throws IncorrectOperationException {
    PsiAnnotation annotation = modifierList.findAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME);
    final GrExpression toAdd = GroovyPsiElementFactory.getInstance(project).createExpressionFromText("\"" + id + "\"");
    if (annotation != null) {
        final PsiAnnotationMemberValue value = annotation.findDeclaredAttributeValue(null);
        if (value instanceof GrAnnotationArrayInitializer) {
            value.add(toAdd);
        } else if (value != null) {
            GrAnnotation anno = GroovyPsiElementFactory.getInstance(project).createAnnotationFromText("@A([])");
            final GrAnnotationArrayInitializer list = (GrAnnotationArrayInitializer) anno.findDeclaredAttributeValue(null);
            list.add(value);
            list.add(toAdd);
            annotation.setDeclaredAttributeValue(null, list);
        }
    } else {
        modifierList.addAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME).setDeclaredAttributeValue(null, toAdd);
    }
}
Also used : GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer)

Example 5 with GrAnnotationArrayInitializer

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

the class MixinMemberContributor method processDynamicElements.

@Override
public void processDynamicElements(@NotNull final PsiType qualifierType, @NotNull PsiScopeProcessor processor, @NotNull final PsiElement place, @NotNull ResolveState state) {
    if (isInAnnotation(place))
        return;
    final PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(qualifierType);
    if (aClass == null)
        return;
    final PsiModifierList modifierList = aClass.getModifierList();
    if (modifierList == null)
        return;
    List<PsiClass> mixins = new ArrayList<>();
    for (PsiAnnotation annotation : getAllMixins(modifierList)) {
        final PsiAnnotationMemberValue value = annotation.findAttributeValue("value");
        if (value instanceof GrAnnotationArrayInitializer) {
            final GrAnnotationMemberValue[] initializers = ((GrAnnotationArrayInitializer) value).getInitializers();
            for (GrAnnotationMemberValue initializer : initializers) {
                addMixin(initializer, mixins);
            }
        } else if (value instanceof GrExpression) {
            addMixin((GrExpression) value, mixins);
        }
    }
    final MixinProcessor delegate = new MixinProcessor(processor, qualifierType, place);
    for (PsiClass mixin : mixins) {
        if (!mixin.processDeclarations(delegate, state, null, place)) {
            return;
        }
    }
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer)

Aggregations

GrAnnotationArrayInitializer (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer)5 GrAnnotationMemberValue (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)2 PsiAnnotation (com.intellij.psi.PsiAnnotation)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)1