Allow ReflectionHints to register hints on interface hierarchies

This commit promotes a previously private method in
`BeanRegistrationsAotContribution` to a top-level method in
`ReflectionHints`.

This helps to register hints on all interfaces implemented in the class
hierarchy of the given type.

Closes gh-32824
This commit is contained in:
Brian Clozel
2024-05-14 17:52:08 +02:00
parent a86612a254
commit f7a6a7b814
3 changed files with 61 additions and 16 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.aot.hint;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -209,6 +210,18 @@ class ReflectionHintsTests {
});
}
@Test
void registerOnInterfaces() {
this.reflectionHints.registerForInterfaces(ChildType.class,
typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThat(this.reflectionHints.typeHints()).hasSize(2)
.noneMatch(typeHint -> typeHint.getType().getCanonicalName().equals(Serializable.class.getCanonicalName()))
.anyMatch(typeHint -> typeHint.getType().getCanonicalName().equals(SecondInterface.class.getCanonicalName())
&& typeHint.getMemberCategories().contains(MemberCategory.INTROSPECT_PUBLIC_METHODS))
.anyMatch(typeHint -> typeHint.getType().getCanonicalName().equals(FirstInterface.class.getCanonicalName())
&& typeHint.getMemberCategories().contains(MemberCategory.INTROSPECT_PUBLIC_METHODS));
}
private void assertTestTypeMethodHints(Consumer<ExecutableHint> methodHint) {
assertThat(this.reflectionHints.typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType().getCanonicalName()).isEqualTo(TestType.class.getCanonicalName());
@@ -241,4 +254,28 @@ class ReflectionHintsTests {
}
interface FirstInterface {
void first();
}
interface SecondInterface {
void second();
}
@SuppressWarnings("serial")
static class ParentType implements Serializable, FirstInterface {
@Override
public void first() {
}
}
@SuppressWarnings("serial")
static class ChildType extends ParentType implements SecondInterface {
@Override
public void second() {
}
}
}