io.jooby.internal.apt.AnnotationSupport Maven / Gradle / Ivy
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal.apt;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.VariableElement;
public interface AnnotationSupport {
Predicate VALUE = "value"::equals;
Predicate NAME = "name"::equals;
static List findAnnotationValue(
AnnotationMirror annotation, Predicate predicate) {
return annotation == null
? List.of()
: annotation.getElementValues().entrySet().stream()
.filter(it -> predicate.test(it.getKey().getSimpleName().toString()))
.flatMap(
it ->
Stream.of(it.getValue().getValue())
.filter(Objects::nonNull)
.flatMap(value -> annotationValue(value).stream())
.map(Objects::toString))
.toList();
}
static List findAnnotationValue(
AnnotationMirror annotation,
Predicate predicate,
Function mapper) {
return annotation == null
? List.of()
: annotation.getElementValues().entrySet().stream()
.filter(it -> predicate.test(it.getKey().getSimpleName().toString()))
.flatMap(
it ->
Stream.of(it.getValue().getValue())
.filter(Objects::nonNull)
.flatMap(
e -> {
var list = e instanceof List ? (List) e : List.of(e);
return list.stream().map(mapper);
}))
.toList();
}
static List