Skip to content

Commit

Permalink
Refine @EmbeddableInstantiator reflection hints
Browse files Browse the repository at this point in the history
Based on @odrotbohm proposal to manage a wider set
of use cases.

Closes gh-31534
  • Loading branch information
sdeleuze committed Nov 15, 2023
1 parent 8eebb5e commit 183c2f8
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import jakarta.persistence.Convert;
import jakarta.persistence.Converter;
import jakarta.persistence.Embedded;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.IdClass;
import jakarta.persistence.PostLoad;
Expand Down Expand Up @@ -200,16 +199,22 @@ private void contributeHibernateHints(RuntimeHints hints, Class<?> managedClass)
return;
}
ReflectionHints reflection = hints.reflection();
registerInstantiatorForReflection(reflection,
AnnotationUtils.findAnnotation(managedClass, embeddableInstantiatorClass));
ReflectionUtils.doWithFields(managedClass, field -> {
Embedded embeddedAnnotation = AnnotationUtils.findAnnotation(field, Embedded.class);
if (embeddedAnnotation != null && field.getAnnotatedType().getType() instanceof Class<?> embeddedClass) {
Annotation embeddableInstantiatorAnnotation = AnnotationUtils.findAnnotation(embeddedClass, embeddableInstantiatorClass);
if (embeddableInstantiatorAnnotation != null) {
Class<?> embeddableInstantiatorClass = (Class<?>) AnnotationUtils.getAnnotationAttributes(embeddableInstantiatorAnnotation).get("value");
reflection.registerType(embeddableInstantiatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}
registerInstantiatorForReflection(reflection,
AnnotationUtils.findAnnotation(field, embeddableInstantiatorClass));
registerInstantiatorForReflection(reflection,
AnnotationUtils.findAnnotation(field.getType(), embeddableInstantiatorClass));
});
}

private void registerInstantiatorForReflection(ReflectionHints reflection, @Nullable Annotation annotation) {
if (annotation == null) {
return;
}
Class<?> embeddableInstantiatorClass = (Class<?>) AnnotationUtils.getAnnotationAttributes(annotation).get("value");
reflection.registerType(embeddableInstantiatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
}
}

0 comments on commit 183c2f8

Please sign in to comment.