From 183c2f82b2cd02cd4907f94436a96397f7362edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Wed, 15 Nov 2023 18:48:27 +0100 Subject: [PATCH] Refine `@EmbeddableInstantiator` reflection hints Based on @odrotbohm proposal to manage a wider set of use cases. Closes gh-31534 --- ...agedTypesBeanRegistrationAotProcessor.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java index b334d327ba..75c0834f19 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java @@ -23,7 +23,6 @@ import javax.lang.model.element.Modifier; import jakarta.persistence.Convert; import jakarta.persistence.Converter; -import jakarta.persistence.Embedded; import jakarta.persistence.EntityListeners; import jakarta.persistence.IdClass; import jakarta.persistence.PostLoad; @@ -200,16 +199,22 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr 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); + } } }