Browse Source

Support properties on records in BindingReflectionHintsRegistrar

Closes gh-29571
pull/29676/head
Sébastien Deleuze 3 years ago
parent
commit
dc5a773b2b
  1. 24
      spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java
  2. 14
      spring-core/src/test/java/org/springframework/aot/hint/BindingReflectionHintsRegistrarTests.java

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

@ -91,19 +91,17 @@ public class BindingReflectionHintsRegistrar { @@ -91,19 +91,17 @@ public class BindingReflectionHintsRegistrar {
registerRecordHints(hints, seen, recordComponent.getAccessor());
}
}
else {
typeHint.withMembers(
MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
for (Method method : clazz.getMethods()) {
String methodName = method.getName();
if (methodName.startsWith("set") && method.getParameterCount() == 1) {
registerPropertyHints(hints, seen, method, 0);
}
else if ((methodName.startsWith("get") && method.getParameterCount() == 0 && method.getReturnType() != Void.TYPE) ||
(methodName.startsWith("is") && method.getParameterCount() == 0 && method.getReturnType() == boolean.class)) {
registerPropertyHints(hints, seen, method, -1);
}
typeHint.withMembers(
MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
for (Method method : clazz.getMethods()) {
String methodName = method.getName();
if (methodName.startsWith("set") && method.getParameterCount() == 1) {
registerPropertyHints(hints, seen, method, 0);
}
else if ((methodName.startsWith("get") && method.getParameterCount() == 0 && method.getReturnType() != Void.TYPE) ||
(methodName.startsWith("is") && method.getParameterCount() == 0 && method.getReturnType() == boolean.class)) {
registerPropertyHints(hints, seen, method, -1);
}
}
if (jacksonAnnotationPresent) {

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

@ -221,6 +221,13 @@ public class BindingReflectionHintsRegistrarTests { @@ -221,6 +221,13 @@ public class BindingReflectionHintsRegistrarTests {
});
}
@Test
void registerTypeForSerializationWithRecordWithProperty() {
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleRecordWithProperty.class);
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleRecordWithProperty.class, "getNameProperty"))
.accepts(this.hints);
}
@Test
void registerTypeForSerializationWithAnonymousClass() {
Runnable anonymousRunnable = () -> { };
@ -329,6 +336,13 @@ public class BindingReflectionHintsRegistrarTests { @@ -329,6 +336,13 @@ public class BindingReflectionHintsRegistrarTests {
record SampleRecord(String name) {}
record SampleRecordWithProperty(String name) {
public String getNameProperty() {
return "";
}
}
static class SampleClassWithJsonProperty {
@JsonProperty

Loading…
Cancel
Save