Complete refactor of AOT concepts
Remove the AOT code that now has an alternative API. Closes gh-28414
This commit is contained in:
@@ -19,7 +19,6 @@ package org.springframework.orm.jpa.support;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
@@ -46,8 +45,6 @@ import org.springframework.aot.generate.GenerationContext;
|
||||
import org.springframework.aot.generate.MethodGenerator;
|
||||
import org.springframework.aot.generate.MethodNameGenerator;
|
||||
import org.springframework.aot.generate.MethodReference;
|
||||
import org.springframework.aot.generator.CodeContribution;
|
||||
import org.springframework.aot.generator.ProtectedAccess.Options;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.PropertyValues;
|
||||
@@ -66,9 +63,6 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.NamedBeanHolder;
|
||||
import org.springframework.beans.factory.generator.AotContributingBeanPostProcessor;
|
||||
import org.springframework.beans.factory.generator.BeanFieldGenerator;
|
||||
import org.springframework.beans.factory.generator.BeanInstantiationContribution;
|
||||
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
@@ -81,7 +75,6 @@ import org.springframework.javapoet.CodeBlock;
|
||||
import org.springframework.javapoet.JavaFile;
|
||||
import org.springframework.javapoet.MethodSpec;
|
||||
import org.springframework.javapoet.TypeSpec;
|
||||
import org.springframework.javapoet.support.MultiStatement;
|
||||
import org.springframework.jndi.JndiLocatorDelegate;
|
||||
import org.springframework.jndi.JndiTemplate;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -198,9 +191,8 @@ import org.springframework.util.StringUtils;
|
||||
* @see jakarta.persistence.PersistenceContext
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class PersistenceAnnotationBeanPostProcessor
|
||||
implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor,
|
||||
MergedBeanDefinitionPostProcessor, AotContributingBeanPostProcessor, BeanRegistrationAotProcessor,
|
||||
public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwareBeanPostProcessor,
|
||||
DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, BeanRegistrationAotProcessor,
|
||||
PriorityOrdered, BeanFactoryAware, Serializable {
|
||||
|
||||
@Nullable
|
||||
@@ -365,16 +357,6 @@ public class PersistenceAnnotationBeanPostProcessor
|
||||
findInjectionMetadata(beanDefinition, beanType, beanName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BeanInstantiationContribution contribute(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
|
||||
InjectionMetadata metadata = findInjectionMetadata(beanDefinition, beanType, beanName);
|
||||
Collection<InjectedElement> injectedElements = metadata.getInjectedElements();
|
||||
if (!CollectionUtils.isEmpty(injectedElements)) {
|
||||
return new PersistenceAnnotationBeanInstantiationContribution(injectedElements);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||
Class<?> beanClass = registeredBean.getBeanClass();
|
||||
@@ -783,68 +765,6 @@ public class PersistenceAnnotationBeanPostProcessor
|
||||
}
|
||||
}
|
||||
|
||||
private static final class PersistenceAnnotationBeanInstantiationContribution implements BeanInstantiationContribution {
|
||||
|
||||
private static final BeanFieldGenerator fieldGenerator = new BeanFieldGenerator();
|
||||
|
||||
private final Collection<PersistenceElement> injectedElements;
|
||||
|
||||
private PersistenceAnnotationBeanInstantiationContribution(Collection<InjectedElement> injectedElements) {
|
||||
this.injectedElements = injectedElements.stream()
|
||||
.filter(obj -> obj instanceof PersistenceElement)
|
||||
.map(PersistenceElement.class::cast).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyTo(CodeContribution contribution) {
|
||||
this.injectedElements.forEach(element -> {
|
||||
Member member = element.getMember();
|
||||
analyzeMember(contribution, member);
|
||||
injectElement(contribution, element);
|
||||
});
|
||||
}
|
||||
|
||||
private void analyzeMember(CodeContribution contribution, Member member) {
|
||||
if (member instanceof Method) {
|
||||
contribution.protectedAccess().analyze(member, Options.defaults().build());
|
||||
}
|
||||
else if (member instanceof Field field) {
|
||||
contribution.protectedAccess().analyze(member, BeanFieldGenerator.FIELD_OPTIONS);
|
||||
if (Modifier.isPrivate(field.getModifiers())) {
|
||||
contribution.runtimeHints().reflection().registerField(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void injectElement(CodeContribution contribution, PersistenceElement element) {
|
||||
MultiStatement statements = contribution.statements();
|
||||
statements.addStatement("$T entityManagerFactory = $T.findEntityManagerFactory(beanFactory, $S)",
|
||||
EntityManagerFactory.class, EntityManagerFactoryUtils.class, element.unitName);
|
||||
boolean requireEntityManager = (element.type != null);
|
||||
if (requireEntityManager) {
|
||||
Properties persistenceProperties = element.properties;
|
||||
boolean hasPersistenceProperties = persistenceProperties != null && !persistenceProperties.isEmpty();
|
||||
if (hasPersistenceProperties) {
|
||||
statements.addStatement("$T persistenceProperties = new Properties()", Properties.class);
|
||||
persistenceProperties.stringPropertyNames().stream().sorted(String::compareTo).forEach(propertyName ->
|
||||
statements.addStatement("persistenceProperties.put($S, $S)",
|
||||
propertyName, persistenceProperties.getProperty(propertyName)));
|
||||
}
|
||||
statements.addStatement("$T entityManager = $T.createSharedEntityManager(entityManagerFactory, $L, $L)",
|
||||
EntityManager.class, SharedEntityManagerCreator.class, (hasPersistenceProperties) ? "persistenceProperties" : null, element.synchronizedWithTransaction);
|
||||
}
|
||||
Member member = element.getMember();
|
||||
CodeBlock value = (requireEntityManager) ? CodeBlock.of("entityManager") : CodeBlock.of("entityManagerFactory");
|
||||
if (member instanceof Field field) {
|
||||
statements.add(fieldGenerator.generateSetValue("bean", field, value));
|
||||
}
|
||||
else {
|
||||
statements.addStatement("bean.$L($L)", member.getName(), value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class AotContribution implements BeanRegistrationAotContribution {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user