Skip to content

Commit d89865a

Browse files
committed
Check @RegisterReflectionForBinding specifies at least one class
Closes gh-29346
1 parent 03039fc commit d89865a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
2222
import org.springframework.aot.hint.ReflectionHints;
2323
import org.springframework.core.annotation.AnnotationUtils;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* A {@link ReflectiveProcessor} implementation that registers reflection hints
@@ -38,7 +39,10 @@ public class RegisterReflectionForBindingProcessor implements ReflectiveProcesso
3839
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
3940
RegisterReflectionForBinding registerReflection = AnnotationUtils.getAnnotation(element, RegisterReflectionForBinding.class);
4041
if (registerReflection != null) {
41-
for (Class<?> type : registerReflection.classes()) {
42+
Class<?>[] classes = registerReflection.classes();
43+
Assert.state(classes.length != 0, "A least one class should be specified in" +
44+
" @RegisterReflectionForBinding attributes and none was provided on " + element);
45+
for (Class<?> type : classes) {
4246
this.bindingRegistrar.registerReflectionHints(hints, type);
4347
}
4448
}

spring-core/src/test/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessorTests.java

+26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
2323

2424
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2526

2627
/**
2728
* Tests for {@link RegisterReflectionForBindingProcessor}.
@@ -50,6 +51,21 @@ void registerReflectionForBindingOnMethod() throws NoSuchMethodException {
5051
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleClassWithGetter.class, "getName")).accepts(hints);
5152
}
5253

54+
@Test
55+
void throwExceptionWithoutAnnotationAttributeOnClass() {
56+
assertThatThrownBy(() -> processor.registerReflectionHints(hints.reflection(),
57+
SampleClassWithoutAnnotationAttribute.class))
58+
.isInstanceOf(IllegalStateException.class);
59+
}
60+
61+
@Test
62+
void throwExceptionWithoutAnnotationAttributeOnMethod() throws NoSuchMethodException {
63+
assertThatThrownBy(() -> processor.registerReflectionHints(hints.reflection(),
64+
SampleClassWithoutMethodLevelAnnotationAttribute.class.getMethod("method")))
65+
.isInstanceOf(IllegalStateException.class);
66+
}
67+
68+
5369
@RegisterReflectionForBinding(SampleClassWithGetter.class)
5470
static class ClassLevelAnnotatedBean {
5571
}
@@ -66,7 +82,17 @@ static class SampleClassWithGetter {
6682
public String getName() {
6783
return null;
6884
}
85+
}
86+
87+
@RegisterReflectionForBinding
88+
static class SampleClassWithoutAnnotationAttribute {
89+
}
6990

91+
static class SampleClassWithoutMethodLevelAnnotationAttribute {
92+
93+
@RegisterReflectionForBinding
94+
public void method() {
95+
}
7096
}
7197

7298
}

0 commit comments

Comments
 (0)