FIX: typeGetAll did not work with repeated annotations on one class

This commit is contained in:
Jonas Pöhler authored and Noemi Szemenyei committed 2021-11-16 11:24:22 +01:00
1 parent 5807874c9e
commit 2d26740286
2 files changed
+31 -4

No files matched your search

@@ -2,6 +2,7 @@ package io.ebean.util;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@@ -57,10 +58,8 @@ public class AnnotationUtil {
private static <A extends Annotation> void typeGetAllCollect(Class<?> clazz, Class<A> annotationType, Set<A> result) { private static <A extends Annotation> void typeGetAllCollect(Class<?> clazz, Class<A> annotationType, Set<A> result) {
while (clazz != null && clazz != Object.class) { while (clazz != null && clazz != Object.class) {
final A val = clazz.getAnnotation(annotationType); final A[] annotations = clazz.getAnnotationsByType(annotationType);
if (val != null) { Collections.addAll(result, annotations);
result.add(val);
}
clazz = clazz.getSuperclass(); clazz = clazz.getSuperclass();
} }
} }
@@ -0,0 +1,28 @@
package io.ebean.util;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.util.Set;
import org.junit.jupiter.api.Test;
import io.ebean.annotation.Formula;
public class TestAnnotationUtil {
@Formula(select = "x")
@Formula(select = "y")
private static class TestObject {
}
@Test
public void testRepeatableAnnotation() {
Set<Formula> list = AnnotationUtil.typeGetAll(TestObject.class, Formula.class);
assertThat(list).hasSize(2);
}
}