Skip to content

Commit d77b36b

Browse files
ledoyensbrannen
authored andcommitted
Open parameter autowiring utility for external use (#2060)
* Make AutowireUtils public and protect its current API * Move ParameterAutowireUtils features to AutowireUtils Closes gh-2060
1 parent 3a42f11 commit d77b36b

File tree

4 files changed

+235
-209
lines changed

4 files changed

+235
-209
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
import java.beans.PropertyDescriptor;
2020
import java.io.Serializable;
21+
import java.lang.annotation.Annotation;
22+
import java.lang.reflect.AnnotatedElement;
2123
import java.lang.reflect.Constructor;
2224
import java.lang.reflect.Executable;
2325
import java.lang.reflect.InvocationHandler;
2426
import java.lang.reflect.InvocationTargetException;
2527
import java.lang.reflect.Method;
2628
import java.lang.reflect.Modifier;
29+
import java.lang.reflect.Parameter;
2730
import java.lang.reflect.ParameterizedType;
2831
import java.lang.reflect.Proxy;
2932
import java.lang.reflect.Type;
@@ -33,8 +36,17 @@
3336
import java.util.Set;
3437

3538
import org.springframework.beans.BeanMetadataElement;
39+
import org.springframework.beans.BeansException;
3640
import org.springframework.beans.factory.ObjectFactory;
41+
import org.springframework.beans.factory.annotation.Autowired;
42+
import org.springframework.beans.factory.annotation.Qualifier;
43+
import org.springframework.beans.factory.annotation.Value;
44+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
45+
import org.springframework.beans.factory.config.DependencyDescriptor;
3746
import org.springframework.beans.factory.config.TypedStringValue;
47+
import org.springframework.core.MethodParameter;
48+
import org.springframework.core.annotation.AnnotatedElementUtils;
49+
import org.springframework.core.annotation.SynthesizingMethodParameter;
3850
import org.springframework.lang.Nullable;
3951
import org.springframework.util.Assert;
4052
import org.springframework.util.ClassUtils;
@@ -49,13 +61,29 @@
4961
* @since 1.1.2
5062
* @see AbstractAutowireCapableBeanFactory
5163
*/
52-
abstract class AutowireUtils {
64+
public abstract class AutowireUtils {
5365

5466
private static final Comparator<Executable> EXECUTABLE_COMPARATOR = (e1, e2) -> {
5567
int result = Boolean.compare(Modifier.isPublic(e2.getModifiers()), Modifier.isPublic(e1.getModifiers()));
5668
return result != 0 ? result : Integer.compare(e2.getParameterCount(), e1.getParameterCount());
5769
};
5870

71+
private static final AnnotatedElement EMPTY_ANNOTATED_ELEMENT = new AnnotatedElement() {
72+
@Override
73+
@Nullable
74+
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
75+
return null;
76+
}
77+
@Override
78+
public Annotation[] getAnnotations() {
79+
return new Annotation[0];
80+
}
81+
@Override
82+
public Annotation[] getDeclaredAnnotations() {
83+
return new Annotation[0];
84+
}
85+
};
86+
5987

6088
/**
6189
* Sort the given constructors, preferring public constructors and "greedy" ones with
@@ -64,7 +92,7 @@ abstract class AutowireUtils {
6492
* decreasing number of arguments.
6593
* @param constructors the constructor array to sort
6694
*/
67-
public static void sortConstructors(Constructor<?>[] constructors) {
95+
static void sortConstructors(Constructor<?>[] constructors) {
6896
Arrays.sort(constructors, EXECUTABLE_COMPARATOR);
6997
}
7098

@@ -75,7 +103,7 @@ public static void sortConstructors(Constructor<?>[] constructors) {
75103
* decreasing number of arguments.
76104
* @param factoryMethods the factory method array to sort
77105
*/
78-
public static void sortFactoryMethods(Method[] factoryMethods) {
106+
static void sortFactoryMethods(Method[] factoryMethods) {
79107
Arrays.sort(factoryMethods, EXECUTABLE_COMPARATOR);
80108
}
81109

@@ -85,7 +113,7 @@ public static void sortFactoryMethods(Method[] factoryMethods) {
85113
* @param pd the PropertyDescriptor of the bean property
86114
* @return whether the bean property is excluded
87115
*/
88-
public static boolean isExcludedFromDependencyCheck(PropertyDescriptor pd) {
116+
static boolean isExcludedFromDependencyCheck(PropertyDescriptor pd) {
89117
Method wm = pd.getWriteMethod();
90118
if (wm == null) {
91119
return false;
@@ -107,7 +135,7 @@ public static boolean isExcludedFromDependencyCheck(PropertyDescriptor pd) {
107135
* @param interfaces the Set of interfaces (Class objects)
108136
* @return whether the setter method is defined by an interface
109137
*/
110-
public static boolean isSetterDefinedInInterface(PropertyDescriptor pd, Set<Class<?>> interfaces) {
138+
static boolean isSetterDefinedInInterface(PropertyDescriptor pd, Set<Class<?>> interfaces) {
111139
Method setter = pd.getWriteMethod();
112140
if (setter != null) {
113141
Class<?> targetClass = setter.getDeclaringClass();
@@ -128,7 +156,7 @@ public static boolean isSetterDefinedInInterface(PropertyDescriptor pd, Set<Clas
128156
* @param requiredType the type to assign the result to
129157
* @return the resolved value
130158
*/
131-
public static Object resolveAutowiringValue(Object autowiringValue, Class<?> requiredType) {
159+
static Object resolveAutowiringValue(Object autowiringValue, Class<?> requiredType) {
132160
if (autowiringValue instanceof ObjectFactory && !requiredType.isInstance(autowiringValue)) {
133161
ObjectFactory<?> factory = (ObjectFactory<?>) autowiringValue;
134162
if (autowiringValue instanceof Serializable && requiredType.isInterface()) {
@@ -173,7 +201,7 @@ public static Object resolveAutowiringValue(Object autowiringValue, Class<?> req
173201
* @return the resolved target return type or the standard method return type
174202
* @since 3.2.5
175203
*/
176-
public static Class<?> resolveReturnTypeForFactoryMethod(
204+
static Class<?> resolveReturnTypeForFactoryMethod(
177205
Method method, Object[] args, @Nullable ClassLoader classLoader) {
178206

179207
Assert.notNull(method, "Method must not be null");
@@ -264,6 +292,103 @@ else if (arg instanceof TypedStringValue) {
264292
return method.getReturnType();
265293
}
266294

295+
/**
296+
* Determine if the supplied {@link Parameter} can <em>potentially</em> be
297+
* autowired from an {@link AutowireCapableBeanFactory}.
298+
* <p>Returns {@code true} if the supplied parameter is annotated or
299+
* meta-annotated with {@link Autowired @Autowired},
300+
* {@link Qualifier @Qualifier}, or {@link Value @Value}.
301+
* <p>Note that {@link #resolveDependency} may still be able to resolve the
302+
* dependency for the supplied parameter even if this method returns {@code false}.
303+
* @param parameter the parameter whose dependency should be autowired
304+
* @param parameterIndex the index of the parameter in the constructor or method
305+
* that declares the parameter
306+
* @see #resolveDependency
307+
* @since 5.2
308+
*/
309+
public static boolean isAutowirable(Parameter parameter, int parameterIndex) {
310+
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
311+
return (AnnotatedElementUtils.hasAnnotation(annotatedParameter, Autowired.class) ||
312+
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Qualifier.class) ||
313+
AnnotatedElementUtils.hasAnnotation(annotatedParameter, Value.class));
314+
}
315+
316+
/**
317+
* Resolve the dependency for the supplied {@link Parameter} from the
318+
* supplied {@link AutowireCapableBeanFactory}.
319+
* <p>Provides comprehensive autowiring support for individual method parameters
320+
* on par with Spring's dependency injection facilities for autowired fields and
321+
* methods, including support for {@link Autowired @Autowired},
322+
* {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
323+
* placeholders and SpEL expressions in {@code @Value} declarations.
324+
* <p>The dependency is required unless the parameter is annotated or meta-annotated
325+
* with {@link Autowired @Autowired} with the {@link Autowired#required required}
326+
* flag set to {@code false}.
327+
* <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
328+
* will be used as the qualifier for resolving ambiguities.
329+
* @param parameter the parameter whose dependency should be resolved
330+
* @param parameterIndex the index of the parameter in the constructor or method
331+
* that declares the parameter
332+
* @param containingClass the concrete class that contains the parameter; this may
333+
* differ from the class that declares the parameter in that it may be a subclass
334+
* thereof, potentially substituting type variables
335+
* @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
336+
* the dependency
337+
* @return the resolved object, or {@code null} if none found
338+
* @throws BeansException if dependency resolution failed
339+
* @see #isAutowirable
340+
* @see Autowired#required
341+
* @see SynthesizingMethodParameter#forExecutable(Executable, int)
342+
* @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
343+
* @since 5.2
344+
*/
345+
@Nullable
346+
public static Object resolveDependency(
347+
Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory)
348+
throws BeansException {
349+
350+
AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
351+
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
352+
boolean required = (autowired == null || autowired.required());
353+
354+
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
355+
parameter.getDeclaringExecutable(), parameterIndex);
356+
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
357+
descriptor.setContainingClass(containingClass);
358+
return beanFactory.resolveDependency(descriptor, null);
359+
}
360+
361+
/**
362+
* Due to a bug in {@code javac} on JDK versions prior to JDK 9, looking up
363+
* annotations directly on a {@link Parameter} will fail for inner class
364+
* constructors.
365+
* <h4>Bug in javac in JDK &lt; 9</h4>
366+
* <p>The parameter annotations array in the compiled byte code excludes an entry
367+
* for the implicit <em>enclosing instance</em> parameter for an inner class
368+
* constructor.
369+
* <h4>Workaround</h4>
370+
* <p>This method provides a workaround for this off-by-one error by allowing the
371+
* caller to access annotations on the preceding {@link Parameter} object (i.e.,
372+
* {@code index - 1}). If the supplied {@code index} is zero, this method returns
373+
* an empty {@code AnnotatedElement}.
374+
* <h4>WARNING</h4>
375+
* <p>The {@code AnnotatedElement} returned by this method should never be cast and
376+
* treated as a {@code Parameter} since the metadata (e.g., {@link Parameter#getName()},
377+
* {@link Parameter#getType()}, etc.) will not match those for the declared parameter
378+
* at the given index in an inner class constructor.
379+
* @return the supplied {@code parameter} or the <em>effective</em> {@code Parameter}
380+
* if the aforementioned bug is in effect
381+
*/
382+
private static AnnotatedElement getEffectiveAnnotatedParameter(Parameter parameter, int index) {
383+
Executable executable = parameter.getDeclaringExecutable();
384+
if (executable instanceof Constructor && ClassUtils.isInnerClass(executable.getDeclaringClass()) &&
385+
executable.getParameterAnnotations().length == executable.getParameterCount() - 1) {
386+
// Bug in javac in JDK <9: annotation array excludes enclosing instance parameter
387+
// for inner classes, so access it with the actual parameter index lowered by 1
388+
return (index == 0 ? EMPTY_ANNOTATED_ELEMENT : executable.getParameters()[index - 1]);
389+
}
390+
return parameter;
391+
}
267392

268393
/**
269394
* Reflective InvocationHandler for lazy access to the current target object.

0 commit comments

Comments
 (0)