Skip to content

Commit 9b93c94

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. Closes gh-32238
1 parent abe3814 commit 9b93c94

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
@@ -200,9 +200,15 @@ private void forEachJacksonAnnotation(AnnotatedElement element, Consumer<MergedA
200200
}
201201

202202
private void registerHintsForClassAttributes(ReflectionHints hints, MergedAnnotation<Annotation> annotation) {
203-
annotation.getRoot().asMap().values().forEach(value -> {
203+
annotation.getRoot().asMap().forEach((key,value) -> {
204204
if (value instanceof Class<?> classValue && value != Void.class) {
205-
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
205+
if (key.equals("builder")) {
206+
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
207+
MemberCategory.INVOKE_DECLARED_METHODS);
208+
}
209+
else {
210+
hints.registerType(classValue, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
211+
}
206212
}
207213
});
208214
}

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
@@ -287,7 +287,8 @@ void registerTypeForJacksonCustomStrategy() {
287287
bindingRegistrar.registerReflectionHints(this.hints.reflection(), SampleRecordWithJacksonCustomStrategy.class);
288288
assertThat(RuntimeHintsPredicates.reflection().onType(PropertyNamingStrategies.UpperSnakeCaseStrategy.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
289289
.accepts(this.hints);
290-
assertThat(RuntimeHintsPredicates.reflection().onType(SampleRecordWithJacksonCustomStrategy.Builder.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS))
290+
assertThat(RuntimeHintsPredicates.reflection().onType(SampleRecordWithJacksonCustomStrategy.Builder.class)
291+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_DECLARED_METHODS))
291292
.accepts(this.hints);
292293
}
293294

0 commit comments

Comments
 (0)