use of java.lang.annotation.Target in project epoxy by airbnb.
the class AttributeInfo method buildAnnotationLists.
/**
* Keeps track of annotations on the attribute so that they can be used in the generated setter
* and getter method. Setter and getter annotations are stored separately since the annotation may
* not target both method and parameter types.
*/
private void buildAnnotationLists(List<? extends AnnotationMirror> annotationMirrors) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (!annotationMirror.getElementValues().isEmpty()) {
// Not supporting annotations with values for now
continue;
}
ClassName annotationClass = ClassName.bestGuess(annotationMirror.getAnnotationType().toString());
if (annotationClass.equals(ClassName.get(EpoxyAttribute.class))) {
// Don't include our own annotation
continue;
}
DeclaredType annotationType = annotationMirror.getAnnotationType();
// A target may exist on an annotation type to specify where the annotation can
// be used, for example fields, methods, or parameters.
Target targetAnnotation = annotationType.asElement().getAnnotation(Target.class);
// Allow all target types if no target was specified on the annotation
List<ElementType> elementTypes = Arrays.asList(targetAnnotation == null ? ElementType.values() : targetAnnotation.value());
AnnotationSpec annotationSpec = AnnotationSpec.builder(annotationClass).build();
if (elementTypes.contains(ElementType.PARAMETER)) {
setterAnnotations.add(annotationSpec);
}
if (elementTypes.contains(ElementType.METHOD)) {
getterAnnotations.add(annotationSpec);
}
}
}
use of java.lang.annotation.Target 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) {
}
}
}
}
use of java.lang.annotation.Target in project checker-framework by typetools.
the class StubParser method annotateDecl.
private void annotateDecl(Map<String, Set<AnnotationMirror>> declAnnos, Element elt, List<AnnotationExpr> annotations) {
if (annotations == null) {
return;
}
Set<AnnotationMirror> annos = AnnotationUtils.createAnnotationSet();
for (AnnotationExpr annotation : annotations) {
AnnotationMirror annoMirror = getAnnotation(annotation, allStubAnnotations);
if (annoMirror != null) {
Target target = annoMirror.getAnnotationType().asElement().getAnnotation(Target.class);
// Only add the declaration annotation if the annotation applies to the element.
if (AnnotationUtils.getElementKindsForTarget(target).contains(elt.getKind())) {
annos.add(annoMirror);
}
}
}
String key = ElementUtils.getVerboseName(elt);
putOrAddToMap(declAnnos, key, annos);
}
use of java.lang.annotation.Target 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.Target in project checker-framework by typetools.
the class WholeProgramInferenceScenesStorage method shouldIgnore.
/**
* Returns true if {@code am} should not be inserted in source code, for example {@link
* org.checkerframework.common.value.qual.BottomVal}. This happens when {@code am} cannot be
* inserted in source code or is the default for the location passed as argument.
*
* <p>Invisible qualifiers, which are annotations that contain the {@link
* org.checkerframework.framework.qual.InvisibleQualifier} meta-annotation, also return true.
*
* <p>TODO: Merge functionality somewhere else with {@link
* org.checkerframework.framework.util.defaults.QualifierDefaults}. Look into the
* createQualifierDefaults method in {@link GenericAnnotatedTypeFactory} (which uses the
* QualifierDefaults class linked above) before changing anything here. See
* https://github.com/typetools/checker-framework/issues/683 .
*
* @param am an annotation to test for whether it should be inserted into source code
* @param location where the location would be inserted; used to determine if {@code am} is the
* default for that location
* @param atm its kind is used to determine if {@code am} is the default for that kind
* @return true if am should not be inserted into source code, or if am is invisible
*/
private boolean shouldIgnore(AnnotationMirror am, TypeUseLocation location, AnnotatedTypeMirror atm) {
Element elt = am.getAnnotationType().asElement();
// Checks if am is an implementation detail (a type qualifier used
// internally by the type system and not meant to be seen by the user).
Target target = elt.getAnnotation(Target.class);
if (target != null && target.value().length == 0) {
return true;
}
if (elt.getAnnotation(InvisibleQualifier.class) != null) {
return true;
}
// Checks if am is default
if (elt.getAnnotation(DefaultQualifierInHierarchy.class) != null) {
return true;
}
DefaultQualifier defaultQual = elt.getAnnotation(DefaultQualifier.class);
if (defaultQual != null) {
for (TypeUseLocation loc : defaultQual.locations()) {
if (loc == TypeUseLocation.ALL || loc == location) {
return true;
}
}
}
DefaultFor defaultQualForLocation = elt.getAnnotation(DefaultFor.class);
if (defaultQualForLocation != null) {
for (TypeUseLocation loc : defaultQualForLocation.value()) {
if (loc == TypeUseLocation.ALL || loc == location) {
return true;
}
}
}
// Checks if am is a default annotation.
// This case checks if it is meta-annotated with @DefaultFor.
// TODO: Handle cases of annotations added via an
// org.checkerframework.framework.type.treeannotator.LiteralTreeAnnotator.
DefaultFor defaultFor = elt.getAnnotation(DefaultFor.class);
if (defaultFor != null) {
org.checkerframework.framework.qual.TypeKind[] types = defaultFor.typeKinds();
TypeKind atmKind = atm.getUnderlyingType().getKind();
if (hasMatchingTypeKind(atmKind, types)) {
return true;
}
}
return false;
}
Aggregations