use of java.lang.annotation.ElementType in project groovy by apache.
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.ElementType in project soot by Sable.
the class AnnotationGenerator method annotate.
/**
* Applies a Java 1.5-style annotation to a given Host. The Host must be of type {@link SootClass}, {@link SootMethod}
* or {@link SootField}.
*
* @param h a method, field, or class
* @param klass the class of the annotation to apply to <code>h</code>
* @param elems a (possibly empty) sequence of AnnotationElem objects corresponding to the elements that should be contained in this annotation
*/
public void annotate(Host h, Class<? extends Annotation> klass, List<AnnotationElem> elems) {
// error-checking -- is this annotation appropriate for the target Host?
Target t = klass.getAnnotation(Target.class);
Collection<ElementType> elementTypes = Arrays.asList(t.value());
final String ERR = "Annotation class " + klass + " not applicable to host of type " + h.getClass() + ".";
if (h instanceof SootClass) {
if (!elementTypes.contains(ElementType.TYPE)) {
throw new RuntimeException(ERR);
}
} else if (h instanceof SootMethod) {
if (!elementTypes.contains(ElementType.METHOD)) {
throw new RuntimeException(ERR);
}
} else if (h instanceof SootField) {
if (!elementTypes.contains(ElementType.FIELD)) {
throw new RuntimeException(ERR);
}
} else {
throw new RuntimeException("Tried to attach annotation to host of type " + h.getClass() + ".");
}
// get the retention type of the class
Retention r = klass.getAnnotation(Retention.class);
// CLASS (runtime invisible) retention is the default
int retPolicy = AnnotationConstants.RUNTIME_INVISIBLE;
if (r != null) {
// RetentionPolicy directly? (Eric Bodden 20/05/2008)
switch(r.value()) {
case CLASS:
retPolicy = AnnotationConstants.RUNTIME_INVISIBLE;
break;
case RUNTIME:
retPolicy = AnnotationConstants.RUNTIME_VISIBLE;
break;
default:
throw new RuntimeException("Unexpected retention policy: " + retPolicy);
}
}
annotate(h, klass.getCanonicalName(), retPolicy, elems);
}
use of java.lang.annotation.ElementType in project checker-framework by typetools.
the class TypeAnnotationMover method isDeclarationAnnotation.
/**
* Returns whether the annotation represented by {@code annotationDeclaration} might be a
* declaration annotation for {@code declarationType}. This holds if the TypeElement has no
* {@code @Target} meta-annotation, or if {@code declarationType} is a target of the annotation.
*
* @param annotationDeclaration declaration for an annotation
* @param declarationType the declaration type to check if the annotation might be a declaration
* annotation for
* @return true if {@code annotationDeclaration} contains {@code declarationType} as a target or
* doesn't contain {@code ElementType.TYPE_USE} as a target
*/
private boolean isDeclarationAnnotation(TypeElement annotationDeclaration, ElementType declarationType) {
Target target = annotationDeclaration.getAnnotation(Target.class);
if (target == null) {
return true;
}
boolean hasTypeUse = false;
for (ElementType elementType : target.value()) {
if (elementType == declarationType) {
return true;
}
if (elementType == ElementType.TYPE_USE) {
hasTypeUse = true;
}
}
if (!hasTypeUse) {
throw new Error(String.format("Annotation %s cannot be used on declaration with type %s", annotationDeclaration.getQualifiedName(), declarationType));
}
return false;
}
use of java.lang.annotation.ElementType in project checker-framework by typetools.
the class AnnotationUtils method isDeclarationAnnotation.
/**
* Returns true if anno is a declaration annotation. In other words, returns true if anno cannot
* be written on uses of types.
*
* @param anno the AnnotationMirror
* @return true if anno is a declaration annotation
*/
public static boolean isDeclarationAnnotation(AnnotationMirror anno) {
TypeElement elem = (TypeElement) anno.getAnnotationType().asElement();
Target t = elem.getAnnotation(Target.class);
if (t == null) {
return true;
}
for (ElementType elementType : t.value()) {
if (elementType == ElementType.TYPE_USE) {
return false;
}
}
return true;
}
use of java.lang.annotation.ElementType in project checker-framework by typetools.
the class AnnotationUtils method hasTypeQualifierElementTypes.
/**
* Returns true if the given array contains {@link ElementType#TYPE_USE}, false otherwise.
*
* @param elements an array of {@link ElementType} values
* @param cls the annotation class being tested; used for diagnostic messages only
* @return true iff the give array contains {@link ElementType#TYPE_USE}
* @throws RuntimeException if the array contains both {@link ElementType#TYPE_USE} and something
* besides {@link ElementType#TYPE_PARAMETER}
*/
public static boolean hasTypeQualifierElementTypes(ElementType[] elements, Class<?> cls) {
// True if the array contains TYPE_USE
boolean hasTypeUse = false;
// Non-null if the array contains an element other than TYPE_USE or TYPE_PARAMETER
ElementType otherElementType = null;
for (ElementType element : elements) {
if (element == ElementType.TYPE_USE) {
hasTypeUse = true;
} else if (element != ElementType.TYPE_PARAMETER) {
otherElementType = element;
}
if (hasTypeUse && otherElementType != null) {
throw new BugInCF("@Target meta-annotation should not contain both TYPE_USE and " + otherElementType + ", for annotation " + cls.getName());
}
}
return hasTypeUse;
}
Aggregations