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 ->
@@ -341,7 +341,7 @@ public BeanWrapper instantiateUsingFactoryMethod(final String beanName, final Ro
341
341
this .beanFactory .initBeanWrapper (bw );
342
342
343
343
Object factoryBean ;
344
- Class factoryClass ;
344
+ Class <?> factoryClass ;
345
345
boolean isStatic ;
346
346
347
347
String factoryBeanName = mbd .getFactoryBeanName ();
@@ -399,7 +399,7 @@ public BeanWrapper instantiateUsingFactoryMethod(final String beanName, final Ro
399
399
factoryClass = ClassUtils .getUserClass (factoryClass );
400
400
Method [] rawCandidates ;
401
401
402
- final Class factoryClazz = factoryClass ;
402
+ final Class <?> factoryClazz = factoryClass ;
403
403
if (System .getSecurityManager () != null ) {
404
404
rawCandidates = AccessController .doPrivileged (new PrivilegedAction <Method []>() {
405
405
public Method [] run () {
@@ -445,7 +445,7 @@ public Method[] run() {
445
445
446
446
for (int i = 0 ; i < candidates .length ; i ++) {
447
447
Method candidate = candidates [i ];
448
- Class [] paramTypes = candidate .getParameterTypes ();
448
+ Class <?> [] paramTypes = candidate .getParameterTypes ();
449
449
450
450
if (paramTypes .length >= minNrOfArgs ) {
451
451
ArgumentsHolder argsHolder ;
@@ -503,7 +503,15 @@ public Method[] run() {
503
503
minTypeDiffWeight = typeDiffWeight ;
504
504
ambiguousFactoryMethods = null ;
505
505
}
506
- else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight ) {
506
+ // Find out about ambiguity: In case of the same type difference weight
507
+ // for methods with the same number of parameters, collect such candidates
508
+ // and eventually raise an ambiguity exception.
509
+ // However, only perform that check in non-lenient constructor resolution mode,
510
+ // and explicitly ignore overridden methods (with the same parameter signature).
511
+ else if (factoryMethodToUse != null && typeDiffWeight == minTypeDiffWeight &&
512
+ !mbd .isLenientConstructorResolution () &&
513
+ paramTypes .length == factoryMethodToUse .getParameterTypes ().length &&
514
+ !Arrays .equals (paramTypes , factoryMethodToUse .getParameterTypes ())) {
507
515
if (ambiguousFactoryMethods == null ) {
508
516
ambiguousFactoryMethods = new LinkedHashSet <Method >();
509
517
ambiguousFactoryMethods .add (factoryMethodToUse );
@@ -540,7 +548,7 @@ else if (void.class.equals(factoryMethodToUse.getReturnType())) {
540
548
"Invalid factory method '" + mbd .getFactoryMethodName () +
541
549
"': needs to have a non-void return type!" );
542
550
}
543
- else if (ambiguousFactoryMethods != null && ! mbd . isLenientConstructorResolution () ) {
551
+ else if (ambiguousFactoryMethods != null ) {
544
552
throw new BeanCreationException (mbd .getResourceDescription (), beanName ,
545
553
"Ambiguous factory method matches found in bean '" + beanName + "' " +
546
554
"(hint: specify index/type/name arguments for simple parameters to avoid type ambiguities): " +
@@ -644,7 +652,7 @@ private int resolveConstructorArguments(
644
652
*/
645
653
private ArgumentsHolder createArgumentArray (
646
654
String beanName , RootBeanDefinition mbd , ConstructorArgumentValues resolvedValues ,
647
- BeanWrapper bw , Class [] paramTypes , String [] paramNames , Object methodOrCtor ,
655
+ BeanWrapper bw , Class <?> [] paramTypes , String [] paramNames , Object methodOrCtor ,
648
656
boolean autowiring ) throws UnsatisfiedDependencyException {
649
657
650
658
String methodType = (methodOrCtor instanceof Constructor ? "constructor" : "factory method" );
@@ -750,7 +758,7 @@ private ArgumentsHolder createArgumentArray(
750
758
private Object [] resolvePreparedArguments (
751
759
String beanName , RootBeanDefinition mbd , BeanWrapper bw , Member methodOrCtor , Object [] argsToResolve ) {
752
760
753
- Class [] paramTypes = (methodOrCtor instanceof Method ?
761
+ Class <?> [] paramTypes = (methodOrCtor instanceof Method ?
754
762
((Method ) methodOrCtor ).getParameterTypes () : ((Constructor ) methodOrCtor ).getParameterTypes ());
755
763
TypeConverter converter = (this .beanFactory .getCustomTypeConverter () != null ?
756
764
this .beanFactory .getCustomTypeConverter () : bw );
@@ -822,7 +830,7 @@ public ArgumentsHolder(Object[] args) {
822
830
this .preparedArguments = args ;
823
831
}
824
832
825
- public int getTypeDifferenceWeight (Class [] paramTypes ) {
833
+ public int getTypeDifferenceWeight (Class <?> [] paramTypes ) {
826
834
// If valid arguments found, determine type difference weight.
827
835
// Try type difference weight on both the converted arguments and
828
836
// the raw arguments. If the raw weight is better, use it.
@@ -832,7 +840,7 @@ public int getTypeDifferenceWeight(Class[] paramTypes) {
832
840
return (rawTypeDiffWeight < typeDiffWeight ? rawTypeDiffWeight : typeDiffWeight );
833
841
}
834
842
835
- public int getAssignabilityWeight (Class [] paramTypes ) {
843
+ public int getAssignabilityWeight (Class <?> [] paramTypes ) {
836
844
for (int i = 0 ; i < paramTypes .length ; i ++) {
837
845
if (!ClassUtils .isAssignableValue (paramTypes [i ], this .arguments [i ])) {
838
846
return Integer .MAX_VALUE ;
0 commit comments