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