use of javax.lang.model.element.AnnotationMirror in project buck by facebook.
the class MoreElements method findAnnotation.
@Nullable
private static AnnotationMirror findAnnotation(CharSequence name, Element element) {
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
DeclaredType annotationType = annotationMirror.getAnnotationType();
TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
if (annotationTypeElement.getQualifiedName().contentEquals(name)) {
return annotationMirror;
}
}
return null;
}
use of javax.lang.model.element.AnnotationMirror in project buck by facebook.
the class MoreElements method isSourceRetention.
public static boolean isSourceRetention(AnnotationMirror annotation) {
DeclaredType annotationType = annotation.getAnnotationType();
TypeElement annotationTypeElement = (TypeElement) annotationType.asElement();
AnnotationMirror retentionAnnotation = findAnnotation("java.lang.annotation.Retention", annotationTypeElement);
if (retentionAnnotation == null) {
return false;
}
VariableElement retentionPolicy = (VariableElement) Preconditions.checkNotNull(findAnnotationValue(retentionAnnotation, "value"));
return retentionPolicy.getSimpleName().contentEquals("SOURCE");
}
use of javax.lang.model.element.AnnotationMirror in project immutables by immutables.
the class Annotations method getAnnotationLines.
static List<CharSequence> getAnnotationLines(Element element, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, ElementType elementType, Function<String, String> importsResolver) {
List<CharSequence> lines = Lists.newArrayList();
Set<String> seenAnnotations = new HashSet<>();
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
TypeElement annotationElement = (TypeElement) annotation.getAnnotationType().asElement();
if (annotationTypeMatches(element, annotationElement, includeAnnotations, includeAllAnnotations, includeJacksonAnnotations, seenAnnotations) && annotationMatchesTarget(annotationElement, elementType)) {
lines.add(AnnotationMirrors.toCharSequence(annotation, importsResolver));
}
}
return lines;
}
use of javax.lang.model.element.AnnotationMirror in project immutables by immutables.
the class Annotations method annotationTypeMatches.
private static boolean annotationTypeMatches(Element element, TypeElement annotationElement, Set<String> includeAnnotations, boolean includeAllAnnotations, boolean includeJacksonAnnotations, Set<String> seenAnnotations) {
String qualifiedName = annotationElement.getQualifiedName().toString();
if (seenAnnotations.contains(qualifiedName) || qualifiedName.startsWith(PREFIX_IMMUTABLES) || qualifiedName.startsWith(PREFIX_JAVA_LANG)) {
// also skip any we've already seen, since we're recursing.
return false;
}
seenAnnotations.add(qualifiedName);
if (annotationElement.getSimpleName().contentEquals(NULLABLE_SIMPLE_NAME)) {
// we expect to propagate nullability separately
return false;
}
if (includeAllAnnotations) {
return true;
}
if (qualifiedName.equals(PREFIX_JACKSON_IGNORE_PROPERTIES)) {
// but preferred way is to use additionalJsonAnnotations style attribute.
return true;
}
if (includeJacksonAnnotations) {
if (element.getKind() != ElementKind.METHOD) {
if (qualifiedName.equals(Proto.JACKSON_DESERIALIZE) || qualifiedName.equals(Proto.JACKSON_SERIALIZE)) {
return false;
}
}
if (qualifiedName.startsWith(PREFIX_JACKSON) || qualifiedName.startsWith(PREFIX_JACKSON_DATABIND)) {
return true;
}
}
if (includeAnnotations.contains(qualifiedName)) {
return true;
}
// This block of code can include annotation if it's parent annotation is included
if (includeJacksonAnnotations || !includeAnnotations.isEmpty()) {
for (AnnotationMirror parentAnnotation : annotationElement.getAnnotationMirrors()) {
TypeElement parentElement = (TypeElement) parentAnnotation.getAnnotationType().asElement();
if (annotationTypeMatches(element, parentElement, includeAnnotations, false, includeJacksonAnnotations, seenAnnotations)) {
return true;
}
}
}
return false;
}
use of javax.lang.model.element.AnnotationMirror in project immutables by immutables.
the class TypeStringProvider method typeAnnotationsToBuffer.
private StringBuilder typeAnnotationsToBuffer(List<? extends AnnotationMirror> annotations) {
StringBuilder annotationBuffer = new StringBuilder(100);
for (AnnotationMirror annotationMirror : annotations) {
CharSequence sequence = AnnotationMirrors.toCharSequence(annotationMirror, importsResolver);
if (!nullableTypeAnnotation && sequence.toString().endsWith(EPHEMERAL_ANNOTATION_NULLABLE)) {
this.nullableTypeAnnotation = true;
}
annotationBuffer.append(sequence).append(' ');
}
return annotationBuffer;
}
Aggregations