All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.slimgears.apt.data.HasAnnotations Maven / Gradle / Ivy

There is a newer version: 0.7.58
Show newest version
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 aclass) {
        return getAnnotation(TypeInfo.of(aclass));
    }

    default boolean hasAnnotation(TypeInfo atype) {
        return annotations().stream().anyMatch(a -> a.type().equals(atype));
    }

    default boolean hasAnnotation(Class 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