Skip to content

Commit

Permalink
Check @RegisterReflectionForBinding specifies at least one class
Browse files Browse the repository at this point in the history
Closes gh-29346
  • Loading branch information
sdeleuze committed Oct 18, 2022
1 parent 03039fc commit d89865a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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}.
Expand Down Expand Up @@ -50,6 +51,21 @@ void registerReflectionForBindingOnMethod() throws NoSuchMethodException {
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 {
}
Expand All @@ -66,7 +82,17 @@ static class SampleClassWithGetter {
public String getName() {
return null;
}
}

@RegisterReflectionForBinding
static class SampleClassWithoutAnnotationAttribute {
}

static class SampleClassWithoutMethodLevelAnnotationAttribute {

@RegisterReflectionForBinding
public void method() {
}
}

}

0 comments on commit d89865a

Please sign in to comment.