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"));
}
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) {
}
}
}
}
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());
}
}
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());
}
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) {
}
}
}
}
Aggregations