Search in sources :

Example 1 with Retention

use of java.lang.annotation.Retention in project spoon by INRIA.

the class AnnotationTest method testPersistenceProperty.

@Test
public void testPersistenceProperty() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PersistenceProperty.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.PersistenceProperty");
    assertEquals("PersistenceProperty", type.getSimpleName());
    assertEquals(2, type.getAnnotations().size());
    CtAnnotation<Target> a1 = type.getAnnotation(type.getFactory().Type().createReference(Target.class));
    assertNotNull(a1);
    CtAnnotation<Retention> a2 = type.getAnnotation(type.getFactory().Type().createReference(Retention.class));
    assertNotNull(a2);
    assertTrue(a1.getValues().containsKey("value"));
    assertTrue(a2.getValues().containsKey("value"));
}
Also used : Target(java.lang.annotation.Target) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Retention(java.lang.annotation.Retention) Test(org.junit.Test)

Example 2 with Retention

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

use of java.lang.annotation.Retention in project guava by google.

the class FeatureEnumTest method assertGoodTesterAnnotation.

private static void assertGoodTesterAnnotation(Class<? extends Annotation> annotationClass) {
    assertNotNull(rootLocaleFormat("%s must be annotated with @TesterAnnotation.", annotationClass), annotationClass.getAnnotation(TesterAnnotation.class));
    final Retention retentionPolicy = annotationClass.getAnnotation(Retention.class);
    assertNotNull(rootLocaleFormat("%s must have a @Retention annotation.", annotationClass), retentionPolicy);
    assertEquals(rootLocaleFormat("%s must have RUNTIME RetentionPolicy.", annotationClass), RetentionPolicy.RUNTIME, retentionPolicy.value());
    assertNotNull(rootLocaleFormat("%s must be inherited.", annotationClass), annotationClass.getAnnotation(Inherited.class));
    for (String propertyName : new String[] { "value", "absent" }) {
        Method method = null;
        try {
            method = annotationClass.getMethod(propertyName);
        } catch (NoSuchMethodException e) {
            fail(rootLocaleFormat("%s must have a property named '%s'.", annotationClass, propertyName));
        }
        final Class<?> returnType = method.getReturnType();
        assertTrue(rootLocaleFormat("%s.%s() must return an array.", annotationClass, propertyName), returnType.isArray());
        assertSame(rootLocaleFormat("%s.%s() must return an array of %s.", annotationClass, propertyName, annotationClass.getDeclaringClass()), annotationClass.getDeclaringClass(), returnType.getComponentType());
    }
}
Also used : Inherited(java.lang.annotation.Inherited) Method(java.lang.reflect.Method) Retention(java.lang.annotation.Retention)

Example 4 with Retention

use of java.lang.annotation.Retention in project roboguice by roboguice.

the class Matchers method checkForRuntimeRetention.

private static void checkForRuntimeRetention(Class<? extends Annotation> annotationType) {
    Retention retention = annotationType.getAnnotation(Retention.class);
    checkArgument(retention != null && retention.value() == RetentionPolicy.RUNTIME, "Annotation %s is missing RUNTIME retention", annotationType.getSimpleName());
}
Also used : Retention(java.lang.annotation.Retention)

Example 5 with Retention

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

the class Java8 method configureAnnotation.

protected void configureAnnotation(final AnnotationNode node, final 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 = EMPTY_METHOD_ARRAY;
        }
        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 | InvocationTargetException ignore) {
            }
        }
    }
}
Also used : Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) 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) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) GroovyObject(groovy.lang.GroovyObject) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) MetaMethod(groovy.lang.MetaMethod) Retention(java.lang.annotation.Retention) RetentionPolicy(java.lang.annotation.RetentionPolicy)

Aggregations

Retention (java.lang.annotation.Retention)17 Target (java.lang.annotation.Target)5 Method (java.lang.reflect.Method)5 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)5 Expression (org.codehaus.groovy.ast.expr.Expression)5 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)5 ElementType (java.lang.annotation.ElementType)4 RetentionPolicy (java.lang.annotation.RetentionPolicy)4 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)4 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)2 ClassNode (org.codehaus.groovy.ast.ClassNode)2 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)2 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)2 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)2 Test (org.junit.Test)2