Skip to content

Commit

Permalink
Support properties on records in BindingReflectionHintsRegistrar
Browse files Browse the repository at this point in the history
Closes gh-29571
  • Loading branch information
sdeleuze committed Dec 10, 2022
1 parent 2b6f3ca commit dc5a773
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Expand Up @@ -91,19 +91,17 @@ private void registerReflectionHints(ReflectionHints hints, Set<Type> 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) {
Expand Down
Expand Up @@ -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 = () -> { };
Expand Down Expand Up @@ -329,6 +336,13 @@ enum SampleEnum {

record SampleRecord(String name) {}

record SampleRecordWithProperty(String name) {

public String getNameProperty() {
return "";
}
}

static class SampleClassWithJsonProperty {

@JsonProperty
Expand Down

0 comments on commit dc5a773

Please sign in to comment.