com.slimgears.apt.data.HasAnnotations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apt-utils Show documentation
Show all versions of apt-utils Show documentation
General purpose utils / module: apt-utils
package com.slimgears.apt.data;
import com.google.common.collect.ImmutableList;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import java.lang.annotation.Annotation;
public interface HasAnnotations {
ImmutableList annotations();
default AnnotationInfo getAnnotation(TypeInfo atype) {
return annotations()
.stream()
.filter(a -> a.type().equals(atype))
.findFirst()
.orElse(null);
}
default AnnotationInfo getAnnotation(Class extends Annotation> aclass) {
return getAnnotation(TypeInfo.of(aclass));
}
default boolean hasAnnotation(TypeInfo atype) {
return annotations().stream().anyMatch(a -> a.type().equals(atype));
}
default boolean hasAnnotation(Class extends Annotation> aclass) {
return hasAnnotation(TypeInfo.of(aclass));
}
@SuppressWarnings("unchecked")
interface Builder> {
B annotations(ImmutableList annotations);
ImmutableList.Builder annotationsBuilder();
default B annotationsFromElement(Element element) {
element.getAnnotationMirrors().forEach(this::annotation);
return (B)this;
}
default B annotation(AnnotationMirror annotationMirror) {
return annotation(AnnotationInfo.of(annotationMirror));
}
default B annotation(AnnotationInfo annotation) {
annotationsBuilder().add(annotation);
return (B)this;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy