Skip to content

Commit 620f558

Browse files
committed
Register hints for superclass in BindingReflectionHintsRegistrar
Closes gh-31552
1 parent 1e78cc3 commit 620f558

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ private void collectReferencedTypes(Set<Class<?>> types, ResolvableType resolvab
159159
for (ResolvableType genericResolvableType : resolvableType.getGenerics()) {
160160
collectReferencedTypes(types, genericResolvableType);
161161
}
162+
Class<?> superClass = clazz.getSuperclass();
163+
if (superClass != null && superClass != Object.class && superClass != Record.class && superClass != Enum.class) {
164+
types.add(superClass);
165+
}
162166
}
163167
}
164168

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ void registerTypeForSerializationWithEmptyClass() {
6363
});
6464
}
6565

66+
@Test
67+
void registerTypeForSerializationWithExtendingClass() {
68+
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleExtendingClass.class);
69+
assertThat(this.hints.reflection().typeHints()).satisfiesExactlyInAnyOrder(
70+
typeHint -> {
71+
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleEmptyClass.class));
72+
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
73+
MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
74+
assertThat(typeHint.constructors()).isEmpty();
75+
assertThat(typeHint.fields()).isEmpty();
76+
assertThat(typeHint.methods()).isEmpty();
77+
},
78+
typeHint -> {
79+
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleExtendingClass.class));
80+
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
81+
MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
82+
assertThat(typeHint.constructors()).isEmpty();
83+
assertThat(typeHint.fields()).isEmpty();
84+
assertThat(typeHint.methods()).isEmpty();
85+
});
86+
}
87+
6688
@Test
6789
void registerTypeForSerializationWithNoProperty() {
6890
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleClassWithNoProperty.class);
@@ -284,6 +306,9 @@ void registerTypeForAnnotationOnMethodAndField() {
284306
static class SampleEmptyClass {
285307
}
286308

309+
static class SampleExtendingClass extends SampleEmptyClass {
310+
}
311+
287312
static class SampleClassWithNoProperty {
288313

289314
String name() {

0 commit comments

Comments
 (0)