Search in sources :

Example 11 with Retention

use of java.lang.annotation.Retention 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

Retention (java.lang.annotation.Retention)11 Method (java.lang.reflect.Method)4 RetentionPolicy (java.lang.annotation.RetentionPolicy)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ElementType (java.lang.annotation.ElementType)2 Target (java.lang.annotation.Target)2 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 Expression (org.codehaus.groovy.ast.expr.Expression)2 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)2 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)2 MemberSelectTree (com.sun.source.tree.MemberSelectTree)1 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)1 Annotation (java.lang.annotation.Annotation)1 Inherited (java.lang.annotation.Inherited)1