Skip to content

Commit

Permalink
Add support for annotation access from EnumValueInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Sep 29, 2023
1 parent 9f3b939 commit bdcbc3f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
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;
import javax.lang.model.element.VariableElement;
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;
Expand Down Expand Up @@ -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<? extends AnnotationMirror> annotationMirrors = elt.getAnnotationMirrors();
List<AnnotationValueInfo> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,12 +20,14 @@ public class EnumValueInfo {
private final Doc doc;
private final boolean deprecated;
private final Text deprecatedDesc;
private final Map<String, AnnotationValueInfo> annotations;

public EnumValueInfo(String identifier, Doc doc, boolean deprecated, Text deprecatedDesc) {
public EnumValueInfo(String identifier, Doc doc, boolean deprecated, Text deprecatedDesc, List<AnnotationValueInfo> 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);
}

/**
Expand Down Expand Up @@ -48,4 +56,20 @@ public boolean isDeprecated() {
public Text getDeprecatedDesc() {
return deprecatedDesc;
}

/**
* @return the list of {@link AnnotationValueInfo} for this enum value
*/
public List<AnnotationValueInfo> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.vertx.test.codegen.testenum;

public @interface SomeAnnotation {
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
public enum ValidEnum {

/**RED doc*/
@SomeAnnotation("red")
RED,

/**GREEN doc*/
@SomeAnnotation("green")
GREEN,

/**BLUE doc*/
@SomeAnnotation("blue")
BLUE

}

0 comments on commit bdcbc3f

Please sign in to comment.