Skip to content

Commit 28cd39a

Browse files
committed
Remove unused AutowiredAnnotationBeanPostProcessor template methods
Closes gh-29487
1 parent aaeb5eb commit 28cd39a

File tree

2 files changed

+23
-66
lines changed

2 files changed

+23
-66
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import org.springframework.beans.factory.BeanCreationException;
5454
import org.springframework.beans.factory.BeanFactory;
5555
import org.springframework.beans.factory.BeanFactoryAware;
56-
import org.springframework.beans.factory.BeanFactoryUtils;
5756
import org.springframework.beans.factory.InjectionPoint;
5857
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
5958
import org.springframework.beans.factory.UnsatisfiedDependencyException;
@@ -77,7 +76,6 @@
7776
import org.springframework.core.MethodParameter;
7877
import org.springframework.core.Ordered;
7978
import org.springframework.core.PriorityOrdered;
80-
import org.springframework.core.annotation.AnnotationAttributes;
8179
import org.springframework.core.annotation.AnnotationUtils;
8280
import org.springframework.core.annotation.MergedAnnotation;
8381
import org.springframework.core.annotation.MergedAnnotations;
@@ -613,39 +611,10 @@ private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
613611
* @return whether the annotation indicates that a dependency is required
614612
*/
615613
protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
616-
return determineRequiredStatus(ann.<AnnotationAttributes> asMap(
617-
mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType())));
618-
}
619-
620-
/**
621-
* Determine if the annotated field or method requires its dependency.
622-
* <p>A 'required' dependency means that autowiring should fail when no beans
623-
* are found. Otherwise, the autowiring process will simply bypass the field
624-
* or method when no beans are found.
625-
* @param ann the Autowired annotation
626-
* @return whether the annotation indicates that a dependency is required
627-
* @deprecated since 5.2, in favor of {@link #determineRequiredStatus(MergedAnnotation)}
628-
*/
629-
@Deprecated
630-
protected boolean determineRequiredStatus(AnnotationAttributes ann) {
631-
return (!ann.containsKey(this.requiredParameterName) ||
614+
return (ann.getValue(this.requiredParameterName).isEmpty() ||
632615
this.requiredParameterValue == ann.getBoolean(this.requiredParameterName));
633616
}
634617

635-
/**
636-
* Obtain all beans of the given type as autowire candidates.
637-
* @param type the type of the bean
638-
* @return the target beans, or an empty Collection if no bean of this type is found
639-
* @throws BeansException if bean retrieval failed
640-
*/
641-
protected <T> Map<String, T> findAutowireCandidates(Class<T> type) throws BeansException {
642-
if (this.beanFactory == null) {
643-
throw new IllegalStateException("No BeanFactory configured - " +
644-
"override the getBeanOfType method or specify the 'beanFactory' property");
645-
}
646-
return BeanFactoryUtils.beansOfTypeIncludingAncestors(this.beanFactory, type);
647-
}
648-
649618
/**
650619
* Register the specified bean as dependent on the autowired beans.
651620
*/
@@ -685,11 +654,10 @@ private abstract static class AutowiredElement extends InjectionMetadata.Injecte
685654

686655
protected final boolean required;
687656

688-
protected AutowiredElement(Member member, PropertyDescriptor pd, boolean required) {
657+
protected AutowiredElement(Member member, @Nullable PropertyDescriptor pd, boolean required) {
689658
super(member, pd);
690659
this.required = required;
691660
}
692-
693661
}
694662

695663

@@ -926,10 +894,8 @@ private static class AotContribution implements BeanRegistrationAotContribution
926894
this.candidateResolver = candidateResolver;
927895
}
928896

929-
930897
@Override
931-
public void applyTo(GenerationContext generationContext,
932-
BeanRegistrationCode beanRegistrationCode) {
898+
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
933899
GeneratedClass generatedClass = generationContext.getGeneratedClasses()
934900
.addForFeatureComponent("Autowiring", this.target, type -> {
935901
type.addJavadoc("Autowiring for {@link $T}.", this.target);
@@ -1003,15 +969,13 @@ private CodeBlock generateMethodStatementForMethod(ClassName targetClassName,
1003969
(!required) ? "forMethod" : "forRequiredMethod");
1004970
code.add("($S", method.getName());
1005971
if (method.getParameterCount() > 0) {
1006-
code.add(", $L",
1007-
generateParameterTypesCode(method.getParameterTypes()));
972+
code.add(", $L", generateParameterTypesCode(method.getParameterTypes()));
1008973
}
1009974
code.add(")");
1010975
AccessControl accessControl = AccessControl.forMember(method);
1011976
if (!accessControl.isAccessibleFrom(targetClassName)) {
1012977
hints.reflection().registerMethod(method, ExecutableMode.INVOKE);
1013-
code.add(".resolveAndInvoke($L, $L)", REGISTERED_BEAN_PARAMETER,
1014-
INSTANCE_PARAMETER);
978+
code.add(".resolveAndInvoke($L, $L)", REGISTERED_BEAN_PARAMETER, INSTANCE_PARAMETER);
1015979
}
1016980
else {
1017981
hints.reflection().registerMethod(method, ExecutableMode.INTROSPECT);
@@ -1038,27 +1002,27 @@ private void registerHints(RuntimeHints runtimeHints) {
10381002
boolean required = autowiredElement.required;
10391003
Member member = autowiredElement.getMember();
10401004
if (member instanceof Field field) {
1041-
DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(
1042-
field, required);
1005+
DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(field, required);
10431006
registerProxyIfNecessary(runtimeHints, dependencyDescriptor);
10441007
}
10451008
if (member instanceof Method method) {
10461009
Class<?>[] parameterTypes = method.getParameterTypes();
10471010
for (int i = 0; i < parameterTypes.length; i++) {
10481011
MethodParameter methodParam = new MethodParameter(method, i);
1049-
DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(
1050-
methodParam, required);
1012+
DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(methodParam, required);
10511013
registerProxyIfNecessary(runtimeHints, dependencyDescriptor);
10521014
}
10531015
}
10541016
});
10551017
}
10561018

10571019
private void registerProxyIfNecessary(RuntimeHints runtimeHints, DependencyDescriptor dependencyDescriptor) {
1058-
Class<?> proxyType = this.candidateResolver
1059-
.getLazyResolutionProxyClass(dependencyDescriptor, null);
1060-
if (proxyType != null && Proxy.isProxyClass(proxyType)) {
1061-
runtimeHints.proxies().registerJdkProxy(proxyType.getInterfaces());
1020+
if (this.candidateResolver != null) {
1021+
Class<?> proxyType =
1022+
this.candidateResolver.getLazyResolutionProxyClass(dependencyDescriptor, null);
1023+
if (proxyType != null && Proxy.isProxyClass(proxyType)) {
1024+
runtimeHints.proxies().registerJdkProxy(proxyType.getInterfaces());
1025+
}
10621026
}
10631027
}
10641028

spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -767,21 +767,17 @@ private static class AotContribution implements BeanRegistrationAotContribution
767767

768768
private static final String INSTANCE_PARAMETER = "instance";
769769

770-
771770
private final Class<?> target;
772771

773772
private final Collection<InjectedElement> injectedElements;
774773

775-
776774
AotContribution(Class<?> target, Collection<InjectedElement> injectedElements) {
777775
this.target = target;
778776
this.injectedElements = injectedElements;
779777
}
780778

781-
782779
@Override
783-
public void applyTo(GenerationContext generationContext,
784-
BeanRegistrationCode beanRegistrationCode) {
780+
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
785781
GeneratedClass generatedClass = generationContext.getGeneratedClasses()
786782
.addForFeatureComponent("PersistenceInjection", this.target, type -> {
787783
type.addJavadoc("Persistence injection for {@link $T}.", this.target);
@@ -801,8 +797,8 @@ public void applyTo(GenerationContext generationContext,
801797

802798
private CodeBlock generateMethodCode(RuntimeHints hints, GeneratedClass generatedClass) {
803799
CodeBlock.Builder code = CodeBlock.builder();
804-
InjectionCodeGenerator injectionCodeGenerator = new InjectionCodeGenerator(
805-
generatedClass.getName(), hints);
800+
InjectionCodeGenerator injectionCodeGenerator =
801+
new InjectionCodeGenerator(generatedClass.getName(), hints);
806802
for (InjectedElement injectedElement : this.injectedElements) {
807803
CodeBlock resourceToInject = generateResourceToInjectCode(generatedClass.getMethods(),
808804
(PersistenceElement) injectedElement);
@@ -814,8 +810,9 @@ private CodeBlock generateMethodCode(RuntimeHints hints, GeneratedClass generate
814810
return code.build();
815811
}
816812

817-
private CodeBlock generateResourceToInjectCode(GeneratedMethods generatedMethods,
818-
PersistenceElement injectedElement) {
813+
private CodeBlock generateResourceToInjectCode(
814+
GeneratedMethods generatedMethods, PersistenceElement injectedElement) {
815+
819816
String unitName = injectedElement.unitName;
820817
boolean requireEntityManager = (injectedElement.type != null);
821818
if (!requireEntityManager) {
@@ -824,14 +821,13 @@ private CodeBlock generateResourceToInjectCode(GeneratedMethods generatedMethods
824821
EntityManagerFactoryUtils.class, ListableBeanFactory.class,
825822
REGISTERED_BEAN_PARAMETER, unitName);
826823
}
827-
String[] methodNameParts = { "get", unitName, "EntityManager" };
824+
String[] methodNameParts = {"get", unitName, "EntityManager"};
828825
GeneratedMethod generatedMethod = generatedMethods.add(methodNameParts, method ->
829826
generateGetEntityManagerMethod(method, injectedElement));
830827
return CodeBlock.of("$L($L)", generatedMethod.getName(), REGISTERED_BEAN_PARAMETER);
831828
}
832829

833-
private void generateGetEntityManagerMethod(MethodSpec.Builder method,
834-
PersistenceElement injectedElement) {
830+
private void generateGetEntityManagerMethod(MethodSpec.Builder method, PersistenceElement injectedElement) {
835831
String unitName = injectedElement.unitName;
836832
Properties properties = injectedElement.properties;
837833
method.addJavadoc("Get the '$L' {@link $T}",
@@ -849,10 +845,8 @@ private void generateGetEntityManagerMethod(MethodSpec.Builder method,
849845
if (hasProperties) {
850846
method.addStatement("$T properties = new Properties()",
851847
Properties.class);
852-
for (String propertyName : new TreeSet<>(
853-
properties.stringPropertyNames())) {
854-
method.addStatement("properties.put($S, $S)", propertyName,
855-
properties.getProperty(propertyName));
848+
for (String propertyName : new TreeSet<>(properties.stringPropertyNames())) {
849+
method.addStatement("properties.put($S, $S)", propertyName, properties.getProperty(propertyName));
856850
}
857851
}
858852
method.addStatement(
@@ -861,7 +855,6 @@ private void generateGetEntityManagerMethod(MethodSpec.Builder method,
861855
(hasProperties) ? "properties" : null,
862856
injectedElement.synchronizedWithTransaction);
863857
}
864-
865858
}
866859

867860
}

0 commit comments

Comments
 (0)