Search in sources :

Example 1 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project groovy-core by groovy.

the class Java5 method configureAnnotation.

private void configureAnnotation(AnnotationNode node, Annotation annotation) {
    Class type = annotation.annotationType();
    if (type == Retention.class) {
        Retention r = (Retention) annotation;
        RetentionPolicy value = r.value();
        setRetentionPolicy(value, node);
        node.setMember("value", new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)), value.toString()));
    } else if (type == Target.class) {
        Target t = (Target) annotation;
        ElementType[] elements = t.value();
        ListExpression elementExprs = new ListExpression();
        for (ElementType element : elements) {
            elementExprs.addExpression(new PropertyExpression(new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
        }
        node.setMember("value", elementExprs);
    } else {
        Method[] declaredMethods;
        try {
            declaredMethods = type.getDeclaredMethods();
        } catch (SecurityException se) {
            declaredMethods = new Method[0];
        }
        for (Method declaredMethod : declaredMethods) {
            try {
                Object value = declaredMethod.invoke(annotation);
                Expression valueExpression = annotationValueToExpression(value);
                if (valueExpression == null)
                    continue;
                node.setMember(declaredMethod.getName(), valueExpression);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
    }
}
Also used : ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Target(java.lang.annotation.Target) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 2 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project intellij-community by JetBrains.

the class AnnotationsHighlightUtil method doCheckRepeatableAnnotation.

@Nullable
private static String doCheckRepeatableAnnotation(@NotNull PsiAnnotation annotation) {
    PsiAnnotationOwner owner = annotation.getOwner();
    if (!(owner instanceof PsiModifierList))
        return null;
    PsiElement target = ((PsiModifierList) owner).getParent();
    if (!(target instanceof PsiClass) || !((PsiClass) target).isAnnotationType())
        return null;
    PsiClass container = getRepeatableContainer(annotation);
    if (container == null)
        return null;
    PsiMethod[] methods = container.findMethodsByName("value", false);
    if (methods.length == 0) {
        return JavaErrorMessages.message("annotation.container.no.value", container.getQualifiedName());
    }
    if (methods.length == 1) {
        PsiType expected = new PsiImmediateClassType((PsiClass) target, PsiSubstitutor.EMPTY).createArrayType();
        if (!expected.equals(methods[0].getReturnType())) {
            return JavaErrorMessages.message("annotation.container.bad.type", container.getQualifiedName(), JavaHighlightUtil.formatType(expected));
        }
    }
    RetentionPolicy targetPolicy = getRetentionPolicy((PsiClass) target);
    if (targetPolicy != null) {
        RetentionPolicy containerPolicy = getRetentionPolicy(container);
        if (containerPolicy != null && targetPolicy.compareTo(containerPolicy) > 0) {
            return JavaErrorMessages.message("annotation.container.low.retention", container.getQualifiedName(), containerPolicy);
        }
    }
    Set<PsiAnnotation.TargetType> repeatableTargets = AnnotationTargetUtil.getAnnotationTargets((PsiClass) target);
    if (repeatableTargets != null) {
        Set<PsiAnnotation.TargetType> containerTargets = AnnotationTargetUtil.getAnnotationTargets(container);
        if (containerTargets != null && !repeatableTargets.containsAll(containerTargets)) {
            return JavaErrorMessages.message("annotation.container.wide.target", container.getQualifiedName());
        }
    }
    return null;
}
Also used : PsiImmediateClassType(com.intellij.psi.impl.source.PsiImmediateClassType) RetentionPolicy(java.lang.annotation.RetentionPolicy) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project error-prone by google.

the class ElementPredicates method effectiveRetentionPolicy.

private static RetentionPolicy effectiveRetentionPolicy(Element element) {
    RetentionPolicy retentionPolicy = RetentionPolicy.CLASS;
    Retention retentionAnnotation = element.getAnnotation(Retention.class);
    if (retentionAnnotation != null) {
        retentionPolicy = retentionAnnotation.value();
    }
    return retentionPolicy;
}
Also used : Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Example 4 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project groovy-core by groovy.

the class Java5 method configureAnnotationFromDefinition.

public static void configureAnnotationFromDefinition(AnnotationNode definition, AnnotationNode root) {
    ClassNode type = definition.getClassNode();
    if ("java.lang.annotation.Retention".equals(type.getName())) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof PropertyExpression))
            return;
        PropertyExpression pe = (PropertyExpression) exp;
        String name = pe.getPropertyAsString();
        RetentionPolicy policy = RetentionPolicy.valueOf(name);
        setRetentionPolicy(policy, root);
    } else if ("java.lang.annotation.Target".equals(type.getName())) {
        Expression exp = definition.getMember("value");
        if (!(exp instanceof ListExpression))
            return;
        ListExpression le = (ListExpression) exp;
        int bitmap = 0;
        for (Expression e : le.getExpressions()) {
            if (!(e instanceof PropertyExpression))
                return;
            PropertyExpression element = (PropertyExpression) e;
            String name = element.getPropertyAsString();
            ElementType value = ElementType.valueOf(name);
            bitmap |= getElementCode(value);
        }
        root.setAllowedTargets(bitmap);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Example 5 with RetentionPolicy

use of java.lang.annotation.RetentionPolicy in project groovy by apache.

the class Java5 method configureAnnotation.

private void configureAnnotation(AnnotationNode node, Annotation annotation) {
    Class type = annotation.annotationType();
    if (type == Retention.class) {
        Retention r = (Retention) annotation;
        RetentionPolicy value = r.value();
        setRetentionPolicy(value, node);
        node.setMember("value", new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)), value.toString()));
    } else if (type == Target.class) {
        Target t = (Target) annotation;
        ElementType[] elements = t.value();
        ListExpression elementExprs = new ListExpression();
        for (ElementType element : elements) {
            elementExprs.addExpression(new PropertyExpression(new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
        }
        node.setMember("value", elementExprs);
    } else {
        Method[] declaredMethods;
        try {
            declaredMethods = type.getDeclaredMethods();
        } catch (SecurityException se) {
            declaredMethods = new Method[0];
        }
        for (Method declaredMethod : declaredMethods) {
            try {
                Object value = declaredMethod.invoke(annotation);
                Expression valueExpression = annotationValueToExpression(value);
                if (valueExpression == null)
                    continue;
                node.setMember(declaredMethod.getName(), valueExpression);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
    }
}
Also used : ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Target(java.lang.annotation.Target) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Aggregations

RetentionPolicy (java.lang.annotation.RetentionPolicy)7 ElementType (java.lang.annotation.ElementType)4 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)4 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)4 Expression (org.codehaus.groovy.ast.expr.Expression)4 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)4 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)4 Retention (java.lang.annotation.Retention)3 Target (java.lang.annotation.Target)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 Module (com.intellij.openapi.module.Module)1 PsiClassOwner (com.intellij.psi.PsiClassOwner)1 PsiFile (com.intellij.psi.PsiFile)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 Nullable (org.jetbrains.annotations.Nullable)1 BundleManifest (org.jetbrains.osgi.project.BundleManifest)1