Skip to content

Commit

Permalink
improve AnnotationInstanceBuilder methods that add class-valued annot…
Browse files Browse the repository at this point in the history
…ation members

- use `Type.create(Class)` for correct handling of non-class types
- fix validation of types, multi-dimensional arrays are permitted
  • Loading branch information
Ladicek committed Jan 24, 2024
1 parent 6b77a46 commit 14591a4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,7 @@ public AnnotationInstanceBuilder add(String name, Class<?> value) {
throw new IllegalArgumentException("Annotation member '" + name + "' already added");
}

DotName className = DotName.createSimple(value.getName());
Type clazz = Type.create(className, Type.Kind.CLASS);
Type clazz = Type.create(value);
this.values.add(AnnotationValue.createClassValue(name, clazz));
return this;
}
Expand All @@ -614,8 +613,7 @@ public AnnotationInstanceBuilder add(String name, Class<?>[] values) {

AnnotationValue[] array = new AnnotationValue[values.length];
for (int i = 0; i < values.length; i++) {
DotName className = DotName.createSimple(values[i].getName());
Type clazz = Type.create(className, Type.Kind.CLASS);
Type clazz = Type.create(values[i]);
array[i] = AnnotationValue.createClassValue(name, clazz);
}
this.values.add(AnnotationValue.createArrayValue(name, array));
Expand Down Expand Up @@ -673,9 +671,9 @@ private void validateType(Type type) {
if (kind == Type.Kind.VOID || kind == Type.Kind.PRIMITIVE || kind == Type.Kind.CLASS) {
return;
}
if (kind == Type.Kind.ARRAY && type.asArrayType().dimensions() == 1) {
Type.Kind constituent = type.asArrayType().constituent().kind();
if (constituent == Type.Kind.PRIMITIVE || constituent == Type.Kind.CLASS) {
if (kind == Type.Kind.ARRAY) {
Type.Kind element = type.asArrayType().elementType().kind();
if (element == Type.Kind.PRIMITIVE || element == Type.Kind.CLASS) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ArrayType;
import org.jboss.jandex.ClassType;
import org.jboss.jandex.DotName;
import org.jboss.jandex.PrimitiveType;
import org.jboss.jandex.Type;
import org.jboss.jandex.VoidType;
import org.junit.jupiter.api.Test;

public class AnnotationInstanceBuilderTest {
Expand Down Expand Up @@ -129,9 +133,11 @@ private static void verify(AnnotationInstance ann) {
assertEquals(SimpleEnum.class.getName(), ann.value("enArray").asEnumTypeArray()[0].toString());
assertEquals(SimpleEnum.BAZ.name(), ann.value("enArray").asEnumArray()[1]);
assertEquals(SimpleEnum.class.getName(), ann.value("enArray").asEnumTypeArray()[1].toString());
assertEquals(2, ann.value("clsArray").asClassArray().length);
assertEquals(4, ann.value("clsArray").asClassArray().length);
assertEquals(String.class.getName(), ann.value("clsArray").asClassArray()[0].name().toString());
assertEquals(Number.class.getName(), ann.value("clsArray").asClassArray()[1].name().toString());
assertEquals(void.class.getName(), ann.value("clsArray").asClassArray()[1].name().toString());
assertEquals(Number[].class.getName(), ann.value("clsArray").asClassArray()[2].name().toString());
assertEquals(byte[][].class.getName(), ann.value("clsArray").asClassArray()[3].name().toString());
assertEquals(2, ann.value("nestedArray").asNestedArray().length);
assertEquals("two", ann.value("nestedArray").asNestedArray()[0].value().asString());
assertEquals("three", ann.value("nestedArray").asNestedArray()[1].value().asString());
Expand Down Expand Up @@ -193,10 +199,10 @@ private static AnnotationInstance complexAnnotation_manual() {
AnnotationValue.createEnumValue("", DotName.createSimple(SimpleEnum.class.getName()), "BAZ"),
}),
AnnotationValue.createArrayValue("clsArray", new AnnotationValue[] {
AnnotationValue.createClassValue("",
Type.create(DotName.createSimple("java.lang.String"), Type.Kind.CLASS)),
AnnotationValue.createClassValue("",
Type.create(DotName.createSimple("java.lang.Number"), Type.Kind.CLASS)),
AnnotationValue.createClassValue("", ClassType.create(String.class)),
AnnotationValue.createClassValue("", VoidType.VOID),
AnnotationValue.createClassValue("", ArrayType.create(ClassType.create(Number.class), 1)),
AnnotationValue.createClassValue("", ArrayType.create(PrimitiveType.BYTE, 2)),
}),
AnnotationValue.createArrayValue("nestedArray", new AnnotationValue[] {
AnnotationValue.createNestedAnnotationValue("", simpleAnnotation_manual("two")),
Expand Down Expand Up @@ -233,7 +239,7 @@ private static AnnotationInstance complexAnnotation_builder() {
.add("chArray", new char[] { 'd', 'e' })
.add("strArray", new String[] { "fg", "hi" })
.add("enArray", new SimpleEnum[] { SimpleEnum.BAR, SimpleEnum.BAZ })
.add("clsArray", new Class[] { String.class, Number.class })
.add("clsArray", new Class[] { String.class, void.class, Number[].class, byte[][].class })
.add("nestedArray", new AnnotationInstance[] { simpleAnnotation_builder("two"),
simpleAnnotation_builder("three") })
.build();
Expand Down

0 comments on commit 14591a4

Please sign in to comment.