Skip to content

Commit 0243059

Browse files
committed
Polishing
1 parent b027b73 commit 0243059

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessor.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@
6464
* @author Sebastien Deleuze
6565
* @since 6.0
6666
*/
67-
@SuppressWarnings("unchecked")
6867
class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
6968

7069
private static final boolean jpaPresent = ClassUtils.isPresent("jakarta.persistence.Entity",
7170
PersistenceManagedTypesBeanRegistrationAotProcessor.class.getClassLoader());
7271

73-
@Nullable
7472
@Override
73+
@Nullable
7574
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
7675
if (jpaPresent) {
7776
if (PersistenceManagedTypes.class.isAssignableFrom(registeredBean.getBeanClass())) {
@@ -82,12 +81,12 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe
8281
return null;
8382
}
8483

84+
8585
private static final class JpaManagedTypesBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
8686

8787
private static final List<Class<? extends Annotation>> CALLBACK_TYPES = List.of(PreUpdate.class,
8888
PostUpdate.class, PrePersist.class, PostPersist.class, PreRemove.class, PostRemove.class, PostLoad.class);
8989

90-
9190
private static final ParameterizedTypeName LIST_OF_STRINGS_TYPE = ParameterizedTypeName.get(List.class, String.class);
9291

9392
private final RegisteredBean registeredBean;
@@ -102,8 +101,8 @@ private JpaManagedTypesBeanRegistrationCodeFragments(BeanRegistrationCodeFragmen
102101

103102
@Override
104103
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext,
105-
BeanRegistrationCode beanRegistrationCode,
106-
boolean allowDirectSupplierShortcut) {
104+
BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) {
105+
107106
PersistenceManagedTypes persistenceManagedTypes = this.registeredBean.getBeanFactory()
108107
.getBean(this.registeredBean.getBeanName(), PersistenceManagedTypes.class);
109108
contributeHints(generationContext.getRuntimeHints(),
@@ -140,7 +139,7 @@ private void contributeHints(RuntimeHints hints, @Nullable ClassLoader classLoad
140139
contributeHibernateHints(hints, classLoader, managedClass);
141140
}
142141
catch (ClassNotFoundException ex) {
143-
throw new IllegalArgumentException("Failed to instantiate the managed class: " + managedClassName, ex);
142+
throw new IllegalArgumentException("Failed to instantiate JPA managed class: " + managedClassName, ex);
144143
}
145144
}
146145
}
@@ -149,7 +148,8 @@ private void contributeEntityListenersHints(RuntimeHints hints, Class<?> managed
149148
EntityListeners entityListeners = AnnotationUtils.findAnnotation(managedClass, EntityListeners.class);
150149
if (entityListeners != null) {
151150
for (Class<?> entityListener : entityListeners.value()) {
152-
hints.reflection().registerType(entityListener, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS);
151+
hints.reflection().registerType(entityListener,
152+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS);
153153
}
154154
}
155155
}
@@ -169,12 +169,14 @@ private void contributeConverterHints(RuntimeHints hints, Class<?> managedClass)
169169
}
170170
Convert convertClassAnnotation = AnnotationUtils.findAnnotation(managedClass, Convert.class);
171171
if (convertClassAnnotation != null) {
172-
reflectionHints.registerType(convertClassAnnotation.converter(), MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
172+
reflectionHints.registerType(convertClassAnnotation.converter(),
173+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
173174
}
174175
ReflectionUtils.doWithFields(managedClass, field -> {
175176
Convert convertFieldAnnotation = AnnotationUtils.findAnnotation(field, Convert.class);
176177
if (convertFieldAnnotation != null && convertFieldAnnotation.converter() != void.class) {
177-
reflectionHints.registerType(convertFieldAnnotation.converter(), MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
178+
reflectionHints.registerType(convertFieldAnnotation.converter(),
179+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
178180
}
179181
});
180182
}
@@ -186,11 +188,11 @@ private void contributeCallbackHints(RuntimeHints hints, Class<?> managedClass)
186188
method -> CALLBACK_TYPES.stream().anyMatch(method::isAnnotationPresent));
187189
}
188190

189-
@SuppressWarnings("unchecked")
190191
private void contributeHibernateHints(RuntimeHints hints, @Nullable ClassLoader classLoader, Class<?> managedClass) {
191192
ReflectionHints reflection = hints.reflection();
192193

193-
Class<? extends Annotation> embeddableInstantiatorClass = loadClass("org.hibernate.annotations.EmbeddableInstantiator", classLoader);
194+
Class<? extends Annotation> embeddableInstantiatorClass =
195+
loadClass("org.hibernate.annotations.EmbeddableInstantiator", classLoader);
194196
if (embeddableInstantiatorClass != null) {
195197
registerForReflection(reflection,
196198
AnnotationUtils.findAnnotation(managedClass, embeddableInstantiatorClass), "value");
@@ -204,23 +206,26 @@ private void contributeHibernateHints(RuntimeHints hints, @Nullable ClassLoader
204206
AnnotationUtils.findAnnotation(method, embeddableInstantiatorClass), "value"));
205207
}
206208

207-
Class<? extends Annotation> valueGenerationTypeClass = loadClass("org.hibernate.annotations.ValueGenerationType", classLoader);
209+
Class<? extends Annotation> valueGenerationTypeClass =
210+
loadClass("org.hibernate.annotations.ValueGenerationType", classLoader);
208211
if (valueGenerationTypeClass != null) {
209212
ReflectionUtils.doWithFields(managedClass, field -> registerForReflection(reflection,
210213
AnnotationUtils.findAnnotation(field, valueGenerationTypeClass), "generatedBy"));
211214
ReflectionUtils.doWithMethods(managedClass, method -> registerForReflection(reflection,
212215
AnnotationUtils.findAnnotation(method, valueGenerationTypeClass), "generatedBy"));
213216
}
214217

215-
Class<? extends Annotation> idGeneratorTypeClass = loadClass("org.hibernate.annotations.IdGeneratorType", classLoader);
218+
Class<? extends Annotation> idGeneratorTypeClass =
219+
loadClass("org.hibernate.annotations.IdGeneratorType", classLoader);
216220
if (idGeneratorTypeClass != null) {
217221
ReflectionUtils.doWithFields(managedClass, field -> registerForReflection(reflection,
218222
AnnotationUtils.findAnnotation(field, idGeneratorTypeClass), "value"));
219223
ReflectionUtils.doWithMethods(managedClass, method -> registerForReflection(reflection,
220224
AnnotationUtils.findAnnotation(method, idGeneratorTypeClass), "value"));
221225
}
222226

223-
Class<? extends Annotation> attributeBinderTypeClass = loadClass("org.hibernate.annotations.AttributeBinderType", classLoader);
227+
Class<? extends Annotation> attributeBinderTypeClass =
228+
loadClass("org.hibernate.annotations.AttributeBinderType", classLoader);
224229
if (attributeBinderTypeClass != null) {
225230
ReflectionUtils.doWithFields(managedClass, field -> registerForReflection(reflection,
226231
AnnotationUtils.findAnnotation(field, attributeBinderTypeClass), "binder"));
@@ -229,6 +234,7 @@ private void contributeHibernateHints(RuntimeHints hints, @Nullable ClassLoader
229234
}
230235
}
231236

237+
@SuppressWarnings("unchecked")
232238
@Nullable
233239
private static Class<? extends Annotation> loadClass(String className, @Nullable ClassLoader classLoader) {
234240
try {
@@ -239,13 +245,14 @@ private static Class<? extends Annotation> loadClass(String className, @Nullable
239245
}
240246
}
241247

242-
@SuppressWarnings("NullAway")
248+
@SuppressWarnings("NullAway") // Not-null assertion performed in ReflectionHints.registerType
243249
private void registerForReflection(ReflectionHints reflection, @Nullable Annotation annotation, String attribute) {
244250
if (annotation == null) {
245251
return;
246252
}
247-
Class<?> embeddableInstantiatorClass = (Class<?>) AnnotationUtils.getAnnotationAttributes(annotation).get(attribute);
248-
reflection.registerType(embeddableInstantiatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
253+
Class<?> type = (Class<?>) AnnotationUtils.getAnnotationAttributes(annotation).get(attribute);
254+
reflection.registerType(type, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
249255
}
250256
}
257+
251258
}

spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceManagedTypesBeanRegistrationAotProcessorTests.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ void processEntityManagerWithPackagesToScan() {
6666
GenericApplicationContext context = new AnnotationConfigApplicationContext();
6767
context.registerBean(JpaDomainConfiguration.class);
6868
compile(context, (initializer, compiled) -> {
69-
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(
70-
initializer);
69+
GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer);
7170
PersistenceManagedTypes persistenceManagedTypes = freshApplicationContext.getBean(
7271
"persistenceManagedTypes", PersistenceManagedTypes.class);
7372
assertThat(persistenceManagedTypes.getManagedClassNames()).containsExactlyInAnyOrder(
@@ -121,6 +120,7 @@ void contributeHibernateHints() {
121120
@SuppressWarnings("unchecked")
122121
private void compile(GenericApplicationContext applicationContext,
123122
BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
123+
124124
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
125125
TestGenerationContext generationContext = new TestGenerationContext();
126126
generator.processAheadOfTime(applicationContext, generationContext);
@@ -131,6 +131,7 @@ private void compile(GenericApplicationContext applicationContext,
131131

132132
private GenericApplicationContext toFreshApplicationContext(
133133
ApplicationContextInitializer<GenericApplicationContext> initializer) {
134+
134135
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
135136
initializer.initialize(freshApplicationContext);
136137
freshApplicationContext.refresh();
@@ -144,21 +145,6 @@ private void contributeHints(GenericApplicationContext applicationContext, Consu
144145
result.accept(generationContext.getRuntimeHints());
145146
}
146147

147-
public static class JpaDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration {
148-
149-
@Override
150-
protected String packageToScan() {
151-
return "org.springframework.orm.jpa.domain";
152-
}
153-
}
154-
155-
public static class HibernateDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration {
156-
157-
@Override
158-
protected String packageToScan() {
159-
return "org.springframework.orm.jpa.hibernate.domain";
160-
}
161-
}
162148

163149
public abstract static class AbstractEntityManagerWithPackagesToScanConfiguration {
164150

@@ -179,13 +165,13 @@ public HibernateJpaVendorAdapter jpaVendorAdapter() {
179165
@Bean
180166
public PersistenceManagedTypes persistenceManagedTypes(ResourceLoader resourceLoader) {
181167
this.scanningInvoked = true;
182-
return new PersistenceManagedTypesScanner(resourceLoader)
183-
.scan(packageToScan());
168+
return new PersistenceManagedTypesScanner(resourceLoader).scan(packageToScan());
184169
}
185170

186171
@Bean
187172
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
188173
JpaVendorAdapter jpaVendorAdapter, PersistenceManagedTypes persistenceManagedTypes) {
174+
189175
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
190176
entityManagerFactoryBean.setDataSource(dataSource);
191177
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
@@ -194,7 +180,24 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource da
194180
}
195181

196182
protected abstract String packageToScan();
183+
}
184+
197185

186+
public static class JpaDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration {
187+
188+
@Override
189+
protected String packageToScan() {
190+
return "org.springframework.orm.jpa.domain";
191+
}
192+
}
193+
194+
195+
public static class HibernateDomainConfiguration extends AbstractEntityManagerWithPackagesToScanConfiguration {
196+
197+
@Override
198+
protected String packageToScan() {
199+
return "org.springframework.orm.jpa.hibernate.domain";
200+
}
198201
}
199202

200203
}

0 commit comments

Comments
 (0)