1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -151,7 +151,7 @@ public BeanWrapper autowireConstructor(
151
151
// Take specified constructors, if any.
152
152
Constructor [] candidates = chosenCtors ;
153
153
if (candidates == null ) {
154
- Class beanClass = mbd .getBeanClass ();
154
+ Class <?> beanClass = mbd .getBeanClass ();
155
155
try {
156
156
candidates = (mbd .isNonPublicAccessAllowed () ?
157
157
beanClass .getDeclaredConstructors () : beanClass .getConstructors ());
@@ -169,7 +169,7 @@ public BeanWrapper autowireConstructor(
169
169
170
170
for (int i = 0 ; i < candidates .length ; i ++) {
171
171
Constructor <?> candidate = candidates [i ];
172
- Class [] paramTypes = candidate .getParameterTypes ();
172
+ Class <?> [] paramTypes = candidate .getParameterTypes ();
173
173
174
174
if (constructorToUse != null && argsToUse .length > paramTypes .length ) {
175
175
// Already found greedy constructor that can be satisfied ->
@@ -342,7 +342,7 @@ public BeanWrapper instantiateUsingFactoryMethod(final String beanName, final Ro
342
342
this .beanFactory .initBeanWrapper (bw );
343
343
344
344
Object factoryBean ;
345
- Class factoryClass ;
345
+ Class <?> factoryClass ;
346
346
boolean isStatic ;
347
347
348
348
String factoryBeanName = mbd .getFactoryBeanName ();
@@ -400,7 +400,7 @@ public BeanWrapper instantiateUsingFactoryMethod(final String beanName, final Ro
400
400
factoryClass = ClassUtils .getUserClass (factoryClass );
401
401
Method [] rawCandidates ;
402
402
403
- final Class factoryClazz = factoryClass ;
403
+ final Class <?> factoryClazz = factoryClass ;
404
404
if (System .getSecurityManager () != null ) {
405
405
rawCandidates = AccessController .doPrivileged (new PrivilegedAction <Method []>() {
406
406
@ Override
@@ -447,7 +447,7 @@ public Method[] run() {
447
447
448
448
for (int i = 0 ; i < candidates .length ; i ++) {
449
449
Method candidate = candidates [i ];
450
- Class [] paramTypes = candidate .getParameterTypes ();
450
+ Class <?> [] paramTypes = candidate .getParameterTypes ();
451
451
452
452
if (paramTypes .length >= minNrOfArgs ) {
453
453
ArgumentsHolder argsHolder ;
@@ -505,7 +505,15 @@ public Method[] run() {
505
505
minTypeDiffWeight = typeDiffWeight ;
506
506
ambiguousFactoryMethods = null ;
507
507
}
508
- else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight ) {
508
+ // Find out about ambiguity: In case of the same type difference weight
509
+ // for methods with the same number of parameters, collect such candidates
510
+ // and eventually raise an ambiguity exception.
511
+ // However, only perform that check in non-lenient constructor resolution mode,
512
+ // and explicitly ignore overridden methods (with the same parameter signature).
513
+ else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight &&
514
+ !mbd .isLenientConstructorResolution () &&
515
+ paramTypes .length == factoryMethodToUse .getParameterTypes ().length &&
516
+ !Arrays .equals (paramTypes , factoryMethodToUse .getParameterTypes ())) {
509
517
if (ambiguousFactoryMethods == null ) {
510
518
ambiguousFactoryMethods = new LinkedHashSet <Method >();
511
519
ambiguousFactoryMethods .add (factoryMethodToUse );
@@ -542,7 +550,7 @@ else if (void.class.equals(factoryMethodToUse.getReturnType())) {
542
550
"Invalid factory method '" + mbd .getFactoryMethodName () +
543
551
"': needs to have a non-void return type!" );
544
552
}
545
- else if (ambiguousFactoryMethods != null && ! mbd . isLenientConstructorResolution () ) {
553
+ else if (ambiguousFactoryMethods != null ) {
546
554
throw new BeanCreationException (mbd .getResourceDescription (), beanName ,
547
555
"Ambiguous factory method matches found in bean '" + beanName + "' " +
548
556
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
@@ -647,7 +655,7 @@ private int resolveConstructorArguments(
647
655
*/
648
656
private ArgumentsHolder createArgumentArray (
649
657
String beanName , RootBeanDefinition mbd , ConstructorArgumentValues resolvedValues ,
650
- BeanWrapper bw , Class [] paramTypes , String [] paramNames , Object methodOrCtor ,
658
+ BeanWrapper bw , Class <?> [] paramTypes , String [] paramNames , Object methodOrCtor ,
651
659
boolean autowiring ) throws UnsatisfiedDependencyException {
652
660
653
661
String methodType = (methodOrCtor instanceof Constructor ? "constructor" : "factory method" );
@@ -753,7 +761,7 @@ private ArgumentsHolder createArgumentArray(
753
761
private Object [] resolvePreparedArguments (
754
762
String beanName , RootBeanDefinition mbd , BeanWrapper bw , Member methodOrCtor , Object [] argsToResolve ) {
755
763
756
- Class [] paramTypes = (methodOrCtor instanceof Method ?
764
+ Class <?> [] paramTypes = (methodOrCtor instanceof Method ?
757
765
((Method ) methodOrCtor ).getParameterTypes () : ((Constructor ) methodOrCtor ).getParameterTypes ());
758
766
TypeConverter converter = (this .beanFactory .getCustomTypeConverter () != null ?
759
767
this .beanFactory .getCustomTypeConverter () : bw );
@@ -825,7 +833,7 @@ public ArgumentsHolder(Object[] args) {
825
833
this .preparedArguments = args ;
826
834
}
827
835
828
- public int getTypeDifferenceWeight (Class [] paramTypes ) {
836
+ public int getTypeDifferenceWeight (Class <?> [] paramTypes ) {
829
837
// If valid arguments found, determine type difference weight.
830
838
// Try type difference weight on both the converted arguments and
831
839
// the raw arguments. If the raw weight is better, use it.
@@ -835,7 +843,7 @@ public int getTypeDifferenceWeight(Class[] paramTypes) {
835
843
return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight );
836
844
}
837
845
838
- public int getAssignabilityWeight (Class [] paramTypes ) {
846
+ public int getAssignabilityWeight (Class <?> [] paramTypes ) {
839
847
for (int i = 0 ; i < paramTypes .length ; i ++) {
840
848
if (!ClassUtils .isAssignableValue (paramTypes [i ], this .arguments [i ])) {
841
849
return Integer .MAX_VALUE ;
0 commit comments