|
|
|
|
@ -64,14 +64,13 @@ import org.springframework.util.ReflectionUtils;
@@ -64,14 +64,13 @@ import org.springframework.util.ReflectionUtils;
|
|
|
|
|
* @author Sebastien Deleuze |
|
|
|
|
* @since 6.0 |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { |
|
|
|
|
|
|
|
|
|
private static final boolean jpaPresent = ClassUtils.isPresent("jakarta.persistence.Entity", |
|
|
|
|
PersistenceManagedTypesBeanRegistrationAotProcessor.class.getClassLoader()); |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
@Override |
|
|
|
|
@Nullable |
|
|
|
|
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { |
|
|
|
|
if (jpaPresent) { |
|
|
|
|
if (PersistenceManagedTypes.class.isAssignableFrom(registeredBean.getBeanClass())) { |
|
|
|
|
@ -82,12 +81,12 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -82,12 +81,12 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator { |
|
|
|
|
|
|
|
|
|
private static final List<Class<? extends Annotation>> CALLBACK_TYPES = List.of(PreUpdate.class, |
|
|
|
|
PostUpdate.class, PrePersist.class, PostPersist.class, PreRemove.class, PostRemove.class, PostLoad.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final ParameterizedTypeName LIST_OF_STRINGS_TYPE = ParameterizedTypeName.get(List.class, String.class); |
|
|
|
|
|
|
|
|
|
private final RegisteredBean registeredBean; |
|
|
|
|
@ -102,8 +101,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -102,8 +101,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext, |
|
|
|
|
BeanRegistrationCode beanRegistrationCode, |
|
|
|
|
boolean allowDirectSupplierShortcut) { |
|
|
|
|
BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) { |
|
|
|
|
|
|
|
|
|
PersistenceManagedTypes persistenceManagedTypes = this.registeredBean.getBeanFactory() |
|
|
|
|
.getBean(this.registeredBean.getBeanName(), PersistenceManagedTypes.class); |
|
|
|
|
contributeHints(generationContext.getRuntimeHints(), |
|
|
|
|
@ -140,7 +139,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -140,7 +139,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
contributeHibernateHints(hints, classLoader, managedClass); |
|
|
|
|
} |
|
|
|
|
catch (ClassNotFoundException ex) { |
|
|
|
|
throw new IllegalArgumentException("Failed to instantiate the managed class: " + managedClassName, ex); |
|
|
|
|
throw new IllegalArgumentException("Failed to instantiate JPA managed class: " + managedClassName, ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -149,7 +148,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -149,7 +148,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
EntityListeners entityListeners = AnnotationUtils.findAnnotation(managedClass, EntityListeners.class); |
|
|
|
|
if (entityListeners != null) { |
|
|
|
|
for (Class<?> entityListener : entityListeners.value()) { |
|
|
|
|
hints.reflection().registerType(entityListener, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS); |
|
|
|
|
hints.reflection().registerType(entityListener, |
|
|
|
|
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -169,12 +169,14 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -169,12 +169,14 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
} |
|
|
|
|
Convert convertClassAnnotation = AnnotationUtils.findAnnotation(managedClass, Convert.class); |
|
|
|
|
if (convertClassAnnotation != null) { |
|
|
|
|
reflectionHints.registerType(convertClassAnnotation.converter(), MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
|
|
|
|
reflectionHints.registerType(convertClassAnnotation.converter(), |
|
|
|
|
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
|
|
|
|
} |
|
|
|
|
ReflectionUtils.doWithFields(managedClass, field -> { |
|
|
|
|
Convert convertFieldAnnotation = AnnotationUtils.findAnnotation(field, Convert.class); |
|
|
|
|
if (convertFieldAnnotation != null && convertFieldAnnotation.converter() != void.class) { |
|
|
|
|
reflectionHints.registerType(convertFieldAnnotation.converter(), MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
|
|
|
|
reflectionHints.registerType(convertFieldAnnotation.converter(), |
|
|
|
|
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
@ -186,11 +188,11 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -186,11 +188,11 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
method -> CALLBACK_TYPES.stream().anyMatch(method::isAnnotationPresent)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
private void contributeHibernateHints(RuntimeHints hints, @Nullable ClassLoader classLoader, Class<?> managedClass) { |
|
|
|
|
ReflectionHints reflection = hints.reflection(); |
|
|
|
|
|
|
|
|
|
Class<? extends Annotation> embeddableInstantiatorClass = loadClass("org.hibernate.annotations.EmbeddableInstantiator", classLoader); |
|
|
|
|
Class<? extends Annotation> embeddableInstantiatorClass = |
|
|
|
|
loadClass("org.hibernate.annotations.EmbeddableInstantiator", classLoader); |
|
|
|
|
if (embeddableInstantiatorClass != null) { |
|
|
|
|
registerForReflection(reflection, |
|
|
|
|
AnnotationUtils.findAnnotation(managedClass, embeddableInstantiatorClass), "value"); |
|
|
|
|
@ -204,7 +206,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -204,7 +206,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
AnnotationUtils.findAnnotation(method, embeddableInstantiatorClass), "value")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Class<? extends Annotation> valueGenerationTypeClass = loadClass("org.hibernate.annotations.ValueGenerationType", classLoader); |
|
|
|
|
Class<? extends Annotation> valueGenerationTypeClass = |
|
|
|
|
loadClass("org.hibernate.annotations.ValueGenerationType", classLoader); |
|
|
|
|
if (valueGenerationTypeClass != null) { |
|
|
|
|
ReflectionUtils.doWithFields(managedClass, field -> registerForReflection(reflection, |
|
|
|
|
AnnotationUtils.findAnnotation(field, valueGenerationTypeClass), "generatedBy")); |
|
|
|
|
@ -212,7 +215,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -212,7 +215,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
AnnotationUtils.findAnnotation(method, valueGenerationTypeClass), "generatedBy")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Class<? extends Annotation> idGeneratorTypeClass = loadClass("org.hibernate.annotations.IdGeneratorType", classLoader); |
|
|
|
|
Class<? extends Annotation> idGeneratorTypeClass = |
|
|
|
|
loadClass("org.hibernate.annotations.IdGeneratorType", classLoader); |
|
|
|
|
if (idGeneratorTypeClass != null) { |
|
|
|
|
ReflectionUtils.doWithFields(managedClass, field -> registerForReflection(reflection, |
|
|
|
|
AnnotationUtils.findAnnotation(field, idGeneratorTypeClass), "value")); |
|
|
|
|
@ -220,7 +224,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -220,7 +224,8 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
AnnotationUtils.findAnnotation(method, idGeneratorTypeClass), "value")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Class<? extends Annotation> attributeBinderTypeClass = loadClass("org.hibernate.annotations.AttributeBinderType", classLoader); |
|
|
|
|
Class<? extends Annotation> attributeBinderTypeClass = |
|
|
|
|
loadClass("org.hibernate.annotations.AttributeBinderType", classLoader); |
|
|
|
|
if (attributeBinderTypeClass != null) { |
|
|
|
|
ReflectionUtils.doWithFields(managedClass, field -> registerForReflection(reflection, |
|
|
|
|
AnnotationUtils.findAnnotation(field, attributeBinderTypeClass), "binder")); |
|
|
|
|
@ -229,6 +234,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -229,6 +234,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
@Nullable |
|
|
|
|
private static Class<? extends Annotation> loadClass(String className, @Nullable ClassLoader classLoader) { |
|
|
|
|
try { |
|
|
|
|
@ -239,13 +245,14 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
@@ -239,13 +245,14 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("NullAway") |
|
|
|
|
@SuppressWarnings("NullAway") // Not-null assertion performed in ReflectionHints.registerType
|
|
|
|
|
private void registerForReflection(ReflectionHints reflection, @Nullable Annotation annotation, String attribute) { |
|
|
|
|
if (annotation == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
Class<?> embeddableInstantiatorClass = (Class<?>) AnnotationUtils.getAnnotationAttributes(annotation).get(attribute); |
|
|
|
|
reflection.registerType(embeddableInstantiatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
|
|
|
|
Class<?> type = (Class<?>) AnnotationUtils.getAnnotationAttributes(annotation).get(attribute); |
|
|
|
|
reflection.registerType(type, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|