diff --git a/spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessor.java b/spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessor.java index 6fb94cc3d4..a4e7184f8b 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessor.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessor.java @@ -21,6 +21,7 @@ import java.lang.reflect.AnnotatedElement; import org.springframework.aot.hint.BindingReflectionHintsRegistrar; import org.springframework.aot.hint.ReflectionHints; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.util.Assert; /** * A {@link ReflectiveProcessor} implementation that registers reflection hints @@ -38,7 +39,10 @@ public class RegisterReflectionForBindingProcessor implements ReflectiveProcesso public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) { RegisterReflectionForBinding registerReflection = AnnotationUtils.getAnnotation(element, RegisterReflectionForBinding.class); if (registerReflection != null) { - for (Class type : registerReflection.classes()) { + Class[] classes = registerReflection.classes(); + Assert.state(classes.length != 0, "A least one class should be specified in" + + " @RegisterReflectionForBinding attributes and none was provided on " + element); + for (Class type : classes) { this.bindingRegistrar.registerReflectionHints(hints, type); } } diff --git a/spring-core/src/test/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessorTests.java b/spring-core/src/test/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessorTests.java index 59c5434fa2..37e65ce6ac 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessorTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/annotation/RegisterReflectionForBindingProcessorTests.java @@ -22,6 +22,7 @@ import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Tests for {@link RegisterReflectionForBindingProcessor}. @@ -50,6 +51,21 @@ public class RegisterReflectionForBindingProcessorTests { assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleClassWithGetter.class, "getName")).accepts(hints); } + @Test + void throwExceptionWithoutAnnotationAttributeOnClass() { + assertThatThrownBy(() -> processor.registerReflectionHints(hints.reflection(), + SampleClassWithoutAnnotationAttribute.class)) + .isInstanceOf(IllegalStateException.class); + } + + @Test + void throwExceptionWithoutAnnotationAttributeOnMethod() throws NoSuchMethodException { + assertThatThrownBy(() -> processor.registerReflectionHints(hints.reflection(), + SampleClassWithoutMethodLevelAnnotationAttribute.class.getMethod("method"))) + .isInstanceOf(IllegalStateException.class); + } + + @RegisterReflectionForBinding(SampleClassWithGetter.class) static class ClassLevelAnnotatedBean { } @@ -66,7 +82,17 @@ public class RegisterReflectionForBindingProcessorTests { public String getName() { return null; } + } + @RegisterReflectionForBinding + static class SampleClassWithoutAnnotationAttribute { + } + + static class SampleClassWithoutMethodLevelAnnotationAttribute { + + @RegisterReflectionForBinding + public void method() { + } } }