Remove unused AutowiredAnnotationBeanPostProcessor template methods

Closes gh-29487
This commit is contained in:
Juergen Hoeller 2022-11-14 23:23:39 +01:00
parent aaeb5eb0d2
commit 28cd39abf9
2 changed files with 23 additions and 66 deletions

View File

@ -53,7 +53,6 @@ import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InjectionPoint; import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.UnsatisfiedDependencyException; import org.springframework.beans.factory.UnsatisfiedDependencyException;
@ -77,7 +76,6 @@ import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered; import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotation; import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations;
@ -613,39 +611,10 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
* @return whether the annotation indicates that a dependency is required * @return whether the annotation indicates that a dependency is required
*/ */
protected boolean determineRequiredStatus(MergedAnnotation<?> ann) { protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
return determineRequiredStatus(ann.<AnnotationAttributes> asMap( return (ann.getValue(this.requiredParameterName).isEmpty() ||
mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType())));
}
/**
* Determine if the annotated field or method requires its dependency.
* <p>A 'required' dependency means that autowiring should fail when no beans
* are found. Otherwise, the autowiring process will simply bypass the field
* or method when no beans are found.
* @param ann the Autowired annotation
* @return whether the annotation indicates that a dependency is required
* @deprecated since 5.2, in favor of {@link #determineRequiredStatus(MergedAnnotation)}
*/
@Deprecated
protected boolean determineRequiredStatus(AnnotationAttributes ann) {
return (!ann.containsKey(this.requiredParameterName) ||
this.requiredParameterValue == ann.getBoolean(this.requiredParameterName)); this.requiredParameterValue == ann.getBoolean(this.requiredParameterName));
} }
/**
* Obtain all beans of the given type as autowire candidates.
* @param type the type of the bean
* @return the target beans, or an empty Collection if no bean of this type is found
* @throws BeansException if bean retrieval failed
*/
protected <T> Map<String, T> findAutowireCandidates(Class<T> type) throws BeansException {
if (this.beanFactory == null) {
throw new IllegalStateException("No BeanFactory configured - " +
"override the getBeanOfType method or specify the 'beanFactory' property");
}
return BeanFactoryUtils.beansOfTypeIncludingAncestors(this.beanFactory, type);
}
/** /**
* Register the specified bean as dependent on the autowired beans. * Register the specified bean as dependent on the autowired beans.
*/ */
@ -685,11 +654,10 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
protected final boolean required; protected final boolean required;
protected AutowiredElement(Member member, PropertyDescriptor pd, boolean required) { protected AutowiredElement(Member member, @Nullable PropertyDescriptor pd, boolean required) {
super(member, pd); super(member, pd);
this.required = required; this.required = required;
} }
} }
@ -926,10 +894,8 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
this.candidateResolver = candidateResolver; this.candidateResolver = candidateResolver;
} }
@Override @Override
public void applyTo(GenerationContext generationContext, public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
BeanRegistrationCode beanRegistrationCode) {
GeneratedClass generatedClass = generationContext.getGeneratedClasses() GeneratedClass generatedClass = generationContext.getGeneratedClasses()
.addForFeatureComponent("Autowiring", this.target, type -> { .addForFeatureComponent("Autowiring", this.target, type -> {
type.addJavadoc("Autowiring for {@link $T}.", this.target); type.addJavadoc("Autowiring for {@link $T}.", this.target);
@ -1003,15 +969,13 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
(!required) ? "forMethod" : "forRequiredMethod"); (!required) ? "forMethod" : "forRequiredMethod");
code.add("($S", method.getName()); code.add("($S", method.getName());
if (method.getParameterCount() > 0) { if (method.getParameterCount() > 0) {
code.add(", $L", code.add(", $L", generateParameterTypesCode(method.getParameterTypes()));
generateParameterTypesCode(method.getParameterTypes()));
} }
code.add(")"); code.add(")");
AccessControl accessControl = AccessControl.forMember(method); AccessControl accessControl = AccessControl.forMember(method);
if (!accessControl.isAccessibleFrom(targetClassName)) { if (!accessControl.isAccessibleFrom(targetClassName)) {
hints.reflection().registerMethod(method, ExecutableMode.INVOKE); hints.reflection().registerMethod(method, ExecutableMode.INVOKE);
code.add(".resolveAndInvoke($L, $L)", REGISTERED_BEAN_PARAMETER, code.add(".resolveAndInvoke($L, $L)", REGISTERED_BEAN_PARAMETER, INSTANCE_PARAMETER);
INSTANCE_PARAMETER);
} }
else { else {
hints.reflection().registerMethod(method, ExecutableMode.INTROSPECT); hints.reflection().registerMethod(method, ExecutableMode.INTROSPECT);
@ -1038,16 +1002,14 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
boolean required = autowiredElement.required; boolean required = autowiredElement.required;
Member member = autowiredElement.getMember(); Member member = autowiredElement.getMember();
if (member instanceof Field field) { if (member instanceof Field field) {
DependencyDescriptor dependencyDescriptor = new DependencyDescriptor( DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(field, required);
field, required);
registerProxyIfNecessary(runtimeHints, dependencyDescriptor); registerProxyIfNecessary(runtimeHints, dependencyDescriptor);
} }
if (member instanceof Method method) { if (member instanceof Method method) {
Class<?>[] parameterTypes = method.getParameterTypes(); Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) { for (int i = 0; i < parameterTypes.length; i++) {
MethodParameter methodParam = new MethodParameter(method, i); MethodParameter methodParam = new MethodParameter(method, i);
DependencyDescriptor dependencyDescriptor = new DependencyDescriptor( DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(methodParam, required);
methodParam, required);
registerProxyIfNecessary(runtimeHints, dependencyDescriptor); registerProxyIfNecessary(runtimeHints, dependencyDescriptor);
} }
} }
@ -1055,10 +1017,12 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
} }
private void registerProxyIfNecessary(RuntimeHints runtimeHints, DependencyDescriptor dependencyDescriptor) { private void registerProxyIfNecessary(RuntimeHints runtimeHints, DependencyDescriptor dependencyDescriptor) {
Class<?> proxyType = this.candidateResolver if (this.candidateResolver != null) {
.getLazyResolutionProxyClass(dependencyDescriptor, null); Class<?> proxyType =
if (proxyType != null && Proxy.isProxyClass(proxyType)) { this.candidateResolver.getLazyResolutionProxyClass(dependencyDescriptor, null);
runtimeHints.proxies().registerJdkProxy(proxyType.getInterfaces()); if (proxyType != null && Proxy.isProxyClass(proxyType)) {
runtimeHints.proxies().registerJdkProxy(proxyType.getInterfaces());
}
} }
} }

View File

@ -767,21 +767,17 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
private static final String INSTANCE_PARAMETER = "instance"; private static final String INSTANCE_PARAMETER = "instance";
private final Class<?> target; private final Class<?> target;
private final Collection<InjectedElement> injectedElements; private final Collection<InjectedElement> injectedElements;
AotContribution(Class<?> target, Collection<InjectedElement> injectedElements) { AotContribution(Class<?> target, Collection<InjectedElement> injectedElements) {
this.target = target; this.target = target;
this.injectedElements = injectedElements; this.injectedElements = injectedElements;
} }
@Override @Override
public void applyTo(GenerationContext generationContext, public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
BeanRegistrationCode beanRegistrationCode) {
GeneratedClass generatedClass = generationContext.getGeneratedClasses() GeneratedClass generatedClass = generationContext.getGeneratedClasses()
.addForFeatureComponent("PersistenceInjection", this.target, type -> { .addForFeatureComponent("PersistenceInjection", this.target, type -> {
type.addJavadoc("Persistence injection for {@link $T}.", this.target); type.addJavadoc("Persistence injection for {@link $T}.", this.target);
@ -801,8 +797,8 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
private CodeBlock generateMethodCode(RuntimeHints hints, GeneratedClass generatedClass) { private CodeBlock generateMethodCode(RuntimeHints hints, GeneratedClass generatedClass) {
CodeBlock.Builder code = CodeBlock.builder(); CodeBlock.Builder code = CodeBlock.builder();
InjectionCodeGenerator injectionCodeGenerator = new InjectionCodeGenerator( InjectionCodeGenerator injectionCodeGenerator =
generatedClass.getName(), hints); new InjectionCodeGenerator(generatedClass.getName(), hints);
for (InjectedElement injectedElement : this.injectedElements) { for (InjectedElement injectedElement : this.injectedElements) {
CodeBlock resourceToInject = generateResourceToInjectCode(generatedClass.getMethods(), CodeBlock resourceToInject = generateResourceToInjectCode(generatedClass.getMethods(),
(PersistenceElement) injectedElement); (PersistenceElement) injectedElement);
@ -814,8 +810,9 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
return code.build(); return code.build();
} }
private CodeBlock generateResourceToInjectCode(GeneratedMethods generatedMethods, private CodeBlock generateResourceToInjectCode(
PersistenceElement injectedElement) { GeneratedMethods generatedMethods, PersistenceElement injectedElement) {
String unitName = injectedElement.unitName; String unitName = injectedElement.unitName;
boolean requireEntityManager = (injectedElement.type != null); boolean requireEntityManager = (injectedElement.type != null);
if (!requireEntityManager) { if (!requireEntityManager) {
@ -824,14 +821,13 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
EntityManagerFactoryUtils.class, ListableBeanFactory.class, EntityManagerFactoryUtils.class, ListableBeanFactory.class,
REGISTERED_BEAN_PARAMETER, unitName); REGISTERED_BEAN_PARAMETER, unitName);
} }
String[] methodNameParts = { "get", unitName, "EntityManager" }; String[] methodNameParts = {"get", unitName, "EntityManager"};
GeneratedMethod generatedMethod = generatedMethods.add(methodNameParts, method -> GeneratedMethod generatedMethod = generatedMethods.add(methodNameParts, method ->
generateGetEntityManagerMethod(method, injectedElement)); generateGetEntityManagerMethod(method, injectedElement));
return CodeBlock.of("$L($L)", generatedMethod.getName(), REGISTERED_BEAN_PARAMETER); return CodeBlock.of("$L($L)", generatedMethod.getName(), REGISTERED_BEAN_PARAMETER);
} }
private void generateGetEntityManagerMethod(MethodSpec.Builder method, private void generateGetEntityManagerMethod(MethodSpec.Builder method, PersistenceElement injectedElement) {
PersistenceElement injectedElement) {
String unitName = injectedElement.unitName; String unitName = injectedElement.unitName;
Properties properties = injectedElement.properties; Properties properties = injectedElement.properties;
method.addJavadoc("Get the '$L' {@link $T}", method.addJavadoc("Get the '$L' {@link $T}",
@ -849,10 +845,8 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
if (hasProperties) { if (hasProperties) {
method.addStatement("$T properties = new Properties()", method.addStatement("$T properties = new Properties()",
Properties.class); Properties.class);
for (String propertyName : new TreeSet<>( for (String propertyName : new TreeSet<>(properties.stringPropertyNames())) {
properties.stringPropertyNames())) { method.addStatement("properties.put($S, $S)", propertyName, properties.getProperty(propertyName));
method.addStatement("properties.put($S, $S)", propertyName,
properties.getProperty(propertyName));
} }
} }
method.addStatement( method.addStatement(
@ -861,7 +855,6 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
(hasProperties) ? "properties" : null, (hasProperties) ? "properties" : null,
injectedElement.synchronizedWithTransaction); injectedElement.synchronizedWithTransaction);
} }
} }
} }