From dc5a773b2b9fd3bf7278d8a48438d224711d95e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Sat, 10 Dec 2022 16:12:23 +0100 Subject: [PATCH] Support properties on records in BindingReflectionHintsRegistrar Closes gh-29571 --- .../hint/BindingReflectionHintsRegistrar.java | 24 +++++++++---------- .../BindingReflectionHintsRegistrarTests.java | 14 +++++++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java b/spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java index 099d2829000f..4860d1f059c8 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java @@ -91,19 +91,17 @@ private void registerReflectionHints(ReflectionHints hints, Set seen, Type 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) { diff --git a/spring-core/src/test/java/org/springframework/aot/hint/BindingReflectionHintsRegistrarTests.java b/spring-core/src/test/java/org/springframework/aot/hint/BindingReflectionHintsRegistrarTests.java index 760b235f5c82..8341c91713f9 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/BindingReflectionHintsRegistrarTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/BindingReflectionHintsRegistrarTests.java @@ -221,6 +221,13 @@ void registerTypeForSerializationWithRecord() { }); } + @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 @@ enum SampleEnum { record SampleRecord(String name) {} + record SampleRecordWithProperty(String name) { + + public String getNameProperty() { + return ""; + } + } + static class SampleClassWithJsonProperty { @JsonProperty