71
71
*/
72
72
class ConstructorResolver {
73
73
74
- private static final String CONSTRUCTOR_PROPERTIES_CLASS_NAME = "java.beans.ConstructorProperties" ;
75
-
76
- private static final boolean constructorPropertiesAnnotationAvailable =
77
- ClassUtils .isPresent (CONSTRUCTOR_PROPERTIES_CLASS_NAME , ConstructorResolver .class .getClassLoader ());
78
-
79
74
private final AbstractAutowireCapableBeanFactory beanFactory ;
80
75
81
76
@@ -183,10 +178,7 @@ public BeanWrapper autowireConstructor(
183
178
ArgumentsHolder argsHolder ;
184
179
if (resolvedValues != null ) {
185
180
try {
186
- String [] paramNames = null ;
187
- if (constructorPropertiesAnnotationAvailable ) {
188
- paramNames = ConstructorPropertiesChecker .evaluateAnnotation (candidate , paramTypes .length );
189
- }
181
+ String [] paramNames = ConstructorPropertiesChecker .evaluate (candidate , paramTypes .length );
190
182
if (paramNames == null ) {
191
183
ParameterNameDiscoverer pnd = this .beanFactory .getParameterNameDiscoverer ();
192
184
if (pnd != null ) {
@@ -297,17 +289,21 @@ public Object run() {
297
289
*/
298
290
public void resolveFactoryMethodIfPossible (RootBeanDefinition mbd ) {
299
291
Class <?> factoryClass ;
292
+ boolean isStatic ;
300
293
if (mbd .getFactoryBeanName () != null ) {
301
294
factoryClass = this .beanFactory .getType (mbd .getFactoryBeanName ());
295
+ isStatic = false ;
302
296
}
303
297
else {
304
298
factoryClass = mbd .getBeanClass ();
299
+ isStatic = true ;
305
300
}
306
301
factoryClass = ClassUtils .getUserClass (factoryClass );
307
- Method [] candidates = ReflectionUtils .getAllDeclaredMethods (factoryClass );
302
+
303
+ Method [] candidates = getCandidateMethods (factoryClass , mbd );
308
304
Method uniqueCandidate = null ;
309
305
for (Method candidate : candidates ) {
310
- if (mbd .isFactoryMethod (candidate )) {
306
+ if (Modifier . isStatic ( candidate . getModifiers ()) == isStatic && mbd .isFactoryMethod (candidate )) {
311
307
if (uniqueCandidate == null ) {
312
308
uniqueCandidate = candidate ;
313
309
}
@@ -322,6 +318,27 @@ else if (!Arrays.equals(uniqueCandidate.getParameterTypes(), candidate.getParame
322
318
}
323
319
}
324
320
321
+ /**
322
+ * Retrieve all candidate methods for the given class, considering
323
+ * the {@link RootBeanDefinition#isNonPublicAccessAllowed()} flag.
324
+ * Called as the starting point for factory method determination.
325
+ */
326
+ private Method [] getCandidateMethods (final Class <?> factoryClass , final RootBeanDefinition mbd ) {
327
+ if (System .getSecurityManager () != null ) {
328
+ return AccessController .doPrivileged (new PrivilegedAction <Method []>() {
329
+ @ Override
330
+ public Method [] run () {
331
+ return (mbd .isNonPublicAccessAllowed () ?
332
+ ReflectionUtils .getAllDeclaredMethods (factoryClass ) : factoryClass .getMethods ());
333
+ }
334
+ });
335
+ }
336
+ else {
337
+ return (mbd .isNonPublicAccessAllowed () ?
338
+ ReflectionUtils .getAllDeclaredMethods (factoryClass ) : factoryClass .getMethods ());
339
+ }
340
+ }
341
+
325
342
/**
326
343
* Instantiate the bean using a named factory method. The method may be static, if the
327
344
* bean definition parameter specifies a class, rather than a "factory-bean", or
@@ -337,7 +354,9 @@ else if (!Arrays.equals(uniqueCandidate.getParameterTypes(), candidate.getParame
337
354
* method, or {@code null} if none (-> use constructor argument values from bean definition)
338
355
* @return a BeanWrapper for the new instance
339
356
*/
340
- public BeanWrapper instantiateUsingFactoryMethod (final String beanName , final RootBeanDefinition mbd , final Object [] explicitArgs ) {
357
+ public BeanWrapper instantiateUsingFactoryMethod (
358
+ final String beanName , final RootBeanDefinition mbd , final Object [] explicitArgs ) {
359
+
341
360
BeanWrapperImpl bw = new BeanWrapperImpl ();
342
361
this .beanFactory .initBeanWrapper (bw );
343
362
@@ -398,28 +417,11 @@ public BeanWrapper instantiateUsingFactoryMethod(final String beanName, final Ro
398
417
// Need to determine the factory method...
399
418
// Try all methods with this name to see if they match the given arguments.
400
419
factoryClass = ClassUtils .getUserClass (factoryClass );
401
- Method [] rawCandidates ;
402
-
403
- final Class <?> factoryClazz = factoryClass ;
404
- if (System .getSecurityManager () != null ) {
405
- rawCandidates = AccessController .doPrivileged (new PrivilegedAction <Method []>() {
406
- @ Override
407
- public Method [] run () {
408
- return (mbd .isNonPublicAccessAllowed () ?
409
- ReflectionUtils .getAllDeclaredMethods (factoryClazz ) : factoryClazz .getMethods ());
410
- }
411
- });
412
- }
413
- else {
414
- rawCandidates = (mbd .isNonPublicAccessAllowed () ?
415
- ReflectionUtils .getAllDeclaredMethods (factoryClazz ) : factoryClazz .getMethods ());
416
- }
417
420
421
+ Method [] rawCandidates = getCandidateMethods (factoryClass , mbd );
418
422
List <Method > candidateSet = new ArrayList <Method >();
419
423
for (Method candidate : rawCandidates ) {
420
- if (Modifier .isStatic (candidate .getModifiers ()) == isStatic &&
421
- candidate .getName ().equals (mbd .getFactoryMethodName ()) &&
422
- mbd .isFactoryMethod (candidate )) {
424
+ if (Modifier .isStatic (candidate .getModifiers ()) == isStatic && mbd .isFactoryMethod (candidate )) {
423
425
candidateSet .add (candidate );
424
426
}
425
427
}
@@ -880,11 +882,11 @@ private static class AutowiredArgumentMarker {
880
882
881
883
882
884
/**
883
- * Inner class to avoid a Java 6 dependency .
885
+ * Delegate for checking Java 6's {@link ConstructorProperties} annotation .
884
886
*/
885
887
private static class ConstructorPropertiesChecker {
886
888
887
- public static String [] evaluateAnnotation (Constructor <?> candidate , int paramCount ) {
889
+ public static String [] evaluate (Constructor <?> candidate , int paramCount ) {
888
890
ConstructorProperties cp = candidate .getAnnotation (ConstructorProperties .class );
889
891
if (cp != null ) {
890
892
String [] names = cp .value ();
0 commit comments