Browse Source

Infer reflection hints for Jackson annotations `builder` attributes

This notably enables Jackson to reflectively call a user-provided
builder class and invoke its declared methods (setters and build) in
a native app.

See gh-32238
Closes gh-32257
pull/32357/head
Simon Baslé 2 years ago
parent
commit
70be04ba81
  1. 10
      spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java
  2. 3
      spring-core/src/test/java/org/springframework/aot/hint/BindingReflectionHintsRegistrarTests.java

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

@ -195,9 +195,15 @@ public class BindingReflectionHintsRegistrar { @@ -195,9 +195,15 @@ public class BindingReflectionHintsRegistrar {
}
private void registerHintsForClassAttributes(ReflectionHints hints, MergedAnnotation<Annotation> annotation) {
annotation.getRoot().asMap().values().forEach(value -> {
annotation.getRoot().asMap().forEach((key,value) -> {
if (value instanceof Class<?> classValue && value != Void.class) {
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
if (key.equals("builder")) {
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS);
}
else {
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}
});
}

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

@ -289,7 +289,8 @@ class BindingReflectionHintsRegistrarTests { @@ -289,7 +289,8 @@ class BindingReflectionHintsRegistrarTests {
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleRecordWithJacksonCustomStrategy.class);
assertThat(RuntimeHintsPredicates.reflection().onType(PropertyNamingStrategies.UpperSnakeCaseStrategy.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
.accepts(this.hints);
assertThat(RuntimeHintsPredicates.reflection().onType(SampleRecordWithJacksonCustomStrategy.Builder.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
assertThat(RuntimeHintsPredicates.reflection().onType(SampleRecordWithJacksonCustomStrategy.Builder.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS))
.accepts(this.hints);
}

Loading…
Cancel
Save