From 0243059f53903961039dff6bb77d62a355e410d6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 5 Nov 2025 12:22:56 +0100 Subject: [PATCH] Polishing --- ...agedTypesBeanRegistrationAotProcessor.java | 41 +++++++++++-------- ...ypesBeanRegistrationAotProcessorTests.java | 41 ++++++++++--------- 2 files changed, 46 insertions(+), 36 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 a69444ffb1b..7f76f77c0c8 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 @@ -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 return null; } + private static final class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator { private static final List> 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 @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 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 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 } 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 method -> CALLBACK_TYPES.stream().anyMatch(method::isAnnotationPresent)); } - @SuppressWarnings("unchecked") private void contributeHibernateHints(RuntimeHints hints, @Nullable ClassLoader classLoader, Class managedClass) { ReflectionHints reflection = hints.reflection(); - Class embeddableInstantiatorClass = loadClass("org.hibernate.annotations.EmbeddableInstantiator", classLoader); + Class 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 AnnotationUtils.findAnnotation(method, embeddableInstantiatorClass), "value")); } - Class valueGenerationTypeClass = loadClass("org.hibernate.annotations.ValueGenerationType", classLoader); + Class 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 AnnotationUtils.findAnnotation(method, valueGenerationTypeClass), "generatedBy")); } - Class idGeneratorTypeClass = loadClass("org.hibernate.annotations.IdGeneratorType", classLoader); + Class 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 AnnotationUtils.findAnnotation(method, idGeneratorTypeClass), "value")); } - Class attributeBinderTypeClass = loadClass("org.hibernate.annotations.AttributeBinderType", classLoader); + Class 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 } } + @SuppressWarnings("unchecked") @Nullable private static Class loadClass(String className, @Nullable ClassLoader classLoader) { try { @@ -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); } } + } diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java index 5763d190da4..e2a319c3b34 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java @@ -66,8 +66,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { GenericApplicationContext context = new AnnotationConfigApplicationContext(); context.registerBean(JpaDomainConfiguration.class); compile(context, (initializer, compiled) -> { - GenericApplicationContext freshApplicationContext = toFreshApplicationContext( - initializer); + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); PersistenceManagedTypes persistenceManagedTypes = freshApplicationContext.getBean( "persistenceManagedTypes", PersistenceManagedTypes.class); assertThat(persistenceManagedTypes.getManagedClassNames()).containsExactlyInAnyOrder( @@ -121,6 +120,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { @SuppressWarnings("unchecked") private void compile(GenericApplicationContext applicationContext, BiConsumer, Compiled> result) { + ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator(); TestGenerationContext generationContext = new TestGenerationContext(); generator.processAheadOfTime(applicationContext, generationContext); @@ -131,6 +131,7 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { private GenericApplicationContext toFreshApplicationContext( ApplicationContextInitializer initializer) { + GenericApplicationContext freshApplicationContext = new GenericApplicationContext(); initializer.initialize(freshApplicationContext); freshApplicationContext.refresh(); @@ -144,21 +145,6 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { result.accept(generationContext.getRuntimeHints()); } - public static class JpaDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration { - - @Override - protected String packageToScan() { - return "org.springframework.orm.jpa.domain"; - } - } - - public static class HibernateDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration { - - @Override - protected String packageToScan() { - return "org.springframework.orm.jpa.hibernate.domain"; - } - } public abstract static class AbstractEntityManagerWithPackagesToScanConfiguration { @@ -179,13 +165,13 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { @Bean public PersistenceManagedTypes persistenceManagedTypes(ResourceLoader resourceLoader) { this.scanningInvoked = true; - return new PersistenceManagedTypesScanner(resourceLoader) - .scan(packageToScan()); + return new PersistenceManagedTypesScanner(resourceLoader).scan(packageToScan()); } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter, PersistenceManagedTypes persistenceManagedTypes) { + LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource); entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter); @@ -194,7 +180,24 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests { } protected abstract String packageToScan(); + } + + public static class JpaDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration { + + @Override + protected String packageToScan() { + return "org.springframework.orm.jpa.domain"; + } + } + + + public static class HibernateDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration { + + @Override + protected String packageToScan() { + return "org.springframework.orm.jpa.hibernate.domain"; + } } }