Skip to content

Commit

Permalink
Infer reflection hints for Jackson annotations builder attributes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
simonbasle committed Feb 13, 2024
1 parent b976ee3 commit 70be04b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,15 @@ private void forEachJacksonAnnotation(AnnotatedElement element, Consumer<MergedA
}

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);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ void registerTypeForJacksonCustomStrategy() {
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);
}

Expand Down

0 comments on commit 70be04b

Please sign in to comment.