Search in sources :

Example 16 with GrAnnotation

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

the class GrStubUtils method getAnnotationNames.

public static String[] getAnnotationNames(PsiModifierListOwner psi) {
    List<String> annoNames = ContainerUtil.newArrayList();
    final PsiModifierList modifierList = psi.getModifierList();
    if (modifierList instanceof GrModifierList) {
        for (GrAnnotation annotation : ((GrModifierList) modifierList).getRawAnnotations()) {
            final String name = annotation.getShortName();
            if (StringUtil.isNotEmpty(name)) {
                annoNames.add(name);
            }
        }
    }
    return ArrayUtil.toStringArray(annoNames);
}
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)

Example 17 with GrAnnotation

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

the class ResolveUtil method isScriptField.

public static boolean isScriptField(GrVariable var) {
    GrModifierList list = var.getModifierList();
    if (list == null)
        return false;
    PsiFile containingFile = var.getContainingFile();
    if (!(containingFile instanceof GroovyFile) || !((GroovyFile) containingFile).isScript())
        return false;
    GrMember member = PsiTreeUtil.getParentOfType(var, GrTypeDefinition.class, GrMethod.class);
    if (member != null)
        return false;
    for (GrAnnotation annotation : list.getAnnotations()) {
        String qualifiedName = annotation.getQualifiedName();
        if (qualifiedName == null)
            continue;
        if (qualifiedName.equals(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD))
            return true;
    }
    return false;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 18 with GrAnnotation

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

the class LoggingContributor method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    GrModifierList modifierList = context.getCodeClass().getModifierList();
    if (modifierList == null)
        return;
    for (GrAnnotation annotation : modifierList.getAnnotations()) {
        String qname = annotation.getQualifiedName();
        String logger = ourLoggers.get(qname);
        if (logger != null) {
            String fieldName = PsiUtil.getAnnoAttributeValue(annotation, "value", "log");
            GrLightField field = new GrLightField(fieldName, logger, context.getCodeClass());
            field.setNavigationElement(annotation);
            field.getModifierList().setModifiers(PsiModifier.PRIVATE, PsiModifier.FINAL, PsiModifier.STATIC);
            field.setOriginInfo("created by @" + annotation.getShortName());
            context.addField(field);
        }
    }
}
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) GrLightField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightField)

Example 19 with GrAnnotation

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

the class DelegatesToInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitAnnotation(@NotNull GrAnnotation annotation) {
            checkTarget(annotation);
            checkDelegatesTo(annotation);
        }

        private void checkTarget(GrAnnotation annotation) {
            if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET.equals(annotation.getQualifiedName()))
                return;
            final PsiElement owner = annotation.getParent().getParent();
            if (!(owner instanceof GrParameter))
                return;
            final boolean isTargetDeclared = annotation.findDeclaredAttributeValue("value") != null;
            String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "value");
            final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
            for (GrParameter parameter : parameterList.getParameters()) {
                final PsiAnnotation delegatesTo = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO);
                if (delegatesTo != null) {
                    if (isTargetDeclared) {
                        final String curTarget = GrAnnotationUtil.inferStringAttribute(delegatesTo, "target");
                        if (curTarget != null && curTarget.equals(targetName)) {
                            //target is used
                            return;
                        }
                    } else {
                        if (delegatesTo.findDeclaredAttributeValue("target") == null && delegatesTo.findDeclaredAttributeValue("value") == null) {
                            // target is used
                            return;
                        }
                    }
                }
            }
            registerError(annotation.getClassReference(), GroovyInspectionBundle.message("target.annotation.is.unused"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }

        private void checkDelegatesTo(GrAnnotation annotation) {
            if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO.equals(annotation.getQualifiedName()))
                return;
            final PsiElement owner = annotation.getParent().getParent();
            if (!(owner instanceof GrParameter))
                return;
            final PsiAnnotationMemberValue targetPair = annotation.findDeclaredAttributeValue("target");
            if (targetPair == null)
                return;
            String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "target");
            final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
            for (GrParameter parameter : parameterList.getParameters()) {
                final PsiAnnotation target = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET);
                if (target != null) {
                    final String curTarget = GrAnnotationUtil.inferStringAttribute(target, "value");
                    if (curTarget != null && curTarget.equals(targetName)) {
                        //target is used
                        return;
                    }
                }
            }
            registerError(targetPair, GroovyInspectionBundle.message("target.0.does.not.exist", targetName != null ? targetName : "?"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }
    };
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) PsiAnnotation(com.intellij.psi.PsiAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with GrAnnotation

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

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