Check @RegisterReflectionForBinding specifies at least one class

Closes gh-29346
This commit is contained in:
Sébastien Deleuze
2022-10-18 18:57:43 +02:00
parent 03039fcc00
commit d89865ad67
2 changed files with 31 additions and 1 deletions

View File

@@ -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);
}
}

View File

@@ -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() {
}
}
}