Skip to content

Commit 70be04b

Browse files
committed
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
1 parent b976ee3 commit 70be04b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,15 @@ private void forEachJacksonAnnotation(AnnotatedElement element, Consumer<MergedA
195195
}
196196

197197
private void registerHintsForClassAttributes(ReflectionHints hints, MergedAnnotation<Annotation> annotation) {
198-
annotation.getRoot().asMap().values().forEach(value -> {
198+
annotation.getRoot().asMap().forEach((key,value) -> {
199199
if (value instanceof Class<?> classValue && value != Void.class) {
200-
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
200+
if (key.equals("builder")) {
201+
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
202+
MemberCategory.INVOKE_DECLARED_METHODS);
203+
}
204+
else {
205+
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
206+
}
201207
}
202208
});
203209
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ void registerTypeForJacksonCustomStrategy() {
289289
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleRecordWithJacksonCustomStrategy.class);
290290
assertThat(RuntimeHintsPredicates.reflection().onType(PropertyNamingStrategies.UpperSnakeCaseStrategy.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
291291
.accepts(this.hints);
292-
assertThat(RuntimeHintsPredicates.reflection().onType(SampleRecordWithJacksonCustomStrategy.Builder.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
292+
assertThat(RuntimeHintsPredicates.reflection().onType(SampleRecordWithJacksonCustomStrategy.Builder.class)
293+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS))
293294
.accepts(this.hints);
294295
}
295296

0 commit comments

Comments
 (0)