diff --git a/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumModel.java b/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumModel.java index ac7bb1c04..37e90db93 100644 --- a/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumModel.java +++ b/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumModel.java @@ -10,6 +10,7 @@ import io.vertx.codegen.type.TypeMirrorFactory; import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; @@ -17,6 +18,7 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -85,7 +87,12 @@ public boolean process() { enumItemDeprecatedDesc = new Text(Helper.normalizeWhitespaces(methodDeprecatedTag.get().getValue())).map(Token.tagMapper(elementUtils, typeUtils, modelElt)); } } - return new EnumValueInfo(elt.getSimpleName().toString(), doc, elt.getAnnotation(Deprecated.class) != null, enumItemDeprecatedDesc); + List annotationMirrors = elt.getAnnotationMirrors(); + List annotationValueInfos = new ArrayList<>(); + if (annotationMirrors != null) { + annotationMirrors.stream().map(annotationValueInfoFactory::processAnnotation).forEach(annotationValueInfos::add); + } + return new EnumValueInfo(elt.getSimpleName().toString(), doc, elt.getAnnotation(Deprecated.class) != null, enumItemDeprecatedDesc, annotationValueInfos); }). collect(Collectors.toList()); if (values.isEmpty()) { diff --git a/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumValueInfo.java b/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumValueInfo.java index 823b7ab37..8f758fc24 100644 --- a/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumValueInfo.java +++ b/vertx-codegen-processor/src/main/java/io/vertx/codegen/EnumValueInfo.java @@ -2,6 +2,12 @@ import io.vertx.codegen.doc.Doc; import io.vertx.codegen.doc.Text; +import io.vertx.codegen.type.AnnotationValueInfo; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * The value (member) of an enumeration model. @@ -14,12 +20,14 @@ public class EnumValueInfo { private final Doc doc; private final boolean deprecated; private final Text deprecatedDesc; + private final Map annotations; - public EnumValueInfo(String identifier, Doc doc, boolean deprecated, Text deprecatedDesc) { + public EnumValueInfo(String identifier, Doc doc, boolean deprecated, Text deprecatedDesc, List annotations) { this.identifier = identifier; this.doc = doc; this.deprecated = deprecated; this.deprecatedDesc = deprecatedDesc; + this.annotations = annotations.stream().collect(HashMap::new, (m, a) -> m.put(a.getName(), a), HashMap::putAll); } /** @@ -48,4 +56,20 @@ public boolean isDeprecated() { public Text getDeprecatedDesc() { return deprecatedDesc; } + + /** + * @return the list of {@link AnnotationValueInfo} for this enum value + */ + public List getAnnotations() { + return new ArrayList<>(annotations.values()); + } + + /** + * @param annotationName fully qualified name of an annotation type + * @return {@link AnnotationValueInfo} for an annotation of given type present on this enum value, + * or {@code null} if an annotation of given type is not present on this enum value + */ + public AnnotationValueInfo getAnnotation(String annotationName) { + return annotations.get(annotationName); + } } diff --git a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/EnumTest.java b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/EnumTest.java index 553dab6c8..ed398a52e 100644 --- a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/EnumTest.java +++ b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/EnumTest.java @@ -9,6 +9,7 @@ import io.vertx.test.codegen.testapi.jsonmapper.WithMyCustomEnumWithMapper; import io.vertx.test.codegen.testenum.EnumAsParam; import io.vertx.test.codegen.testenum.InvalidEmptyEnum; +import io.vertx.test.codegen.testenum.SomeAnnotation; import io.vertx.test.codegen.testenum.ValidEnum; import org.junit.Test; @@ -31,6 +32,9 @@ public void testEnum() throws Exception { assertEquals(Arrays.asList("RED doc", "GREEN doc", "BLUE doc"), model.getValues().stream(). map(e -> e.getDoc().toString()). collect(Collectors.toList())); + assertEquals(Arrays.asList("red", "green", "blue"), model.getValues().stream() + .map(e -> e.getAnnotation(SomeAnnotation.class.getName()).getMember("value")) + .collect(Collectors.toList())); assertEquals("enum", model.getKind()); assertEquals("ValidEnum doc", model.getDoc().toString()); assertEquals(ValidEnum.class.getName(), model.getFqn()); diff --git a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/SomeAnnotation.java b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/SomeAnnotation.java new file mode 100644 index 000000000..bfeb9da95 --- /dev/null +++ b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/SomeAnnotation.java @@ -0,0 +1,5 @@ +package io.vertx.test.codegen.testenum; + +public @interface SomeAnnotation { + String value(); +} diff --git a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/ValidEnum.java b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/ValidEnum.java index be1f97059..fa3108e75 100644 --- a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/ValidEnum.java +++ b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/testenum/ValidEnum.java @@ -7,12 +7,15 @@ public enum ValidEnum { /**RED doc*/ + @SomeAnnotation("red") RED, /**GREEN doc*/ + @SomeAnnotation("green") GREEN, /**BLUE doc*/ + @SomeAnnotation("blue") BLUE }