Skip to content

Commit 56dfcd1

Browse files
jhoellerunknown
authored andcommitted
Aligned with refinements in 3.2.x branch
Issue: SPR-11034
1 parent 24dfe8e commit 56dfcd1

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public TypedStringValue(String value) {
6161
* @param value the String value
6262
* @param targetType the type to convert to
6363
*/
64-
public TypedStringValue(String value, Class targetType) {
64+
public TypedStringValue(String value, Class<?> targetType) {
6565
setValue(value);
6666
setTargetType(targetType);
6767
}
@@ -101,7 +101,7 @@ public String getValue() {
101101
* for example in BeanFactoryPostProcessors.
102102
* @see PropertyPlaceholderConfigurer
103103
*/
104-
public void setTargetType(Class targetType) {
104+
public void setTargetType(Class<?> targetType) {
105105
Assert.notNull(targetType, "'targetType' must not be null");
106106
this.targetType = targetType;
107107
}

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Comparator;
3232
import java.util.Set;
3333

34+
import org.springframework.beans.BeanMetadataElement;
3435
import org.springframework.beans.factory.ObjectFactory;
3536
import org.springframework.beans.factory.config.TypedStringValue;
3637
import org.springframework.util.Assert;
@@ -195,8 +196,8 @@ public static Class<?> resolveReturnTypeForFactoryMethod(Method method, Object[]
195196

196197
TypeVariable<Method>[] declaredTypeVariables = method.getTypeParameters();
197198
Type genericReturnType = method.getGenericReturnType();
198-
Type[] methodArgumentTypes = method.getGenericParameterTypes();
199-
Assert.isTrue(args.length == methodArgumentTypes.length, "Argument array does not match parameter count");
199+
Type[] methodParameterTypes = method.getGenericParameterTypes();
200+
Assert.isTrue(args.length == methodParameterTypes.length, "Argument array does not match parameter count");
200201

201202
// Ensure that the type variable (e.g., T) is declared directly on the method
202203
// itself (e.g., via <T>), not on the enclosing class or interface.
@@ -209,17 +210,33 @@ public static Class<?> resolveReturnTypeForFactoryMethod(Method method, Object[]
209210
}
210211

211212
if (locallyDeclaredTypeVariableMatchesReturnType) {
212-
for (int i = 0; i < methodArgumentTypes.length; i++) {
213-
Type currentMethodArgumentType = methodArgumentTypes[i];
214-
if (currentMethodArgumentType.equals(genericReturnType)) {
215-
return args[i].getClass();
213+
for (int i = 0; i < methodParameterTypes.length; i++) {
214+
Type methodParameterType = methodParameterTypes[i];
215+
Object arg = args[i];
216+
if (methodParameterType.equals(genericReturnType)) {
217+
if (arg instanceof TypedStringValue) {
218+
TypedStringValue typedValue = ((TypedStringValue) arg);
219+
if (typedValue.hasTargetType()) {
220+
return typedValue.getTargetType();
221+
}
222+
try {
223+
return typedValue.resolveTargetType(classLoader);
224+
}
225+
catch (ClassNotFoundException ex) {
226+
throw new IllegalStateException("Failed to resolve typed value", ex);
227+
}
228+
}
229+
// Only consider argument type if it is a simple value...
230+
if (arg != null && !(arg instanceof BeanMetadataElement)) {
231+
return arg.getClass();
232+
}
233+
return method.getReturnType();
216234
}
217-
if (currentMethodArgumentType instanceof ParameterizedType) {
218-
ParameterizedType parameterizedType = (ParameterizedType) currentMethodArgumentType;
235+
else if (methodParameterType instanceof ParameterizedType) {
236+
ParameterizedType parameterizedType = (ParameterizedType) methodParameterType;
219237
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
220238
for (Type typeArg : actualTypeArguments) {
221239
if (typeArg.equals(genericReturnType)) {
222-
Object arg = args[i];
223240
if (arg instanceof Class) {
224241
return (Class<?>) arg;
225242
}
@@ -229,7 +246,11 @@ public static Class<?> resolveReturnTypeForFactoryMethod(Method method, Object[]
229246
className = (String) arg;
230247
}
231248
else if (arg instanceof TypedStringValue) {
232-
className = ((TypedStringValue) arg).getValue();
249+
TypedStringValue typedValue = ((TypedStringValue) arg);
250+
String targetTypeName = typedValue.getTargetTypeName();
251+
if (targetTypeName == null || Class.class.getName().equals(targetTypeName)) {
252+
className = typedValue.getValue();
253+
}
233254
}
234255
if (className != null) {
235256
try {
@@ -240,11 +261,9 @@ else if (arg instanceof TypedStringValue) {
240261
"Could not resolve specified class name argument [" + arg + "]", ex);
241262
}
242263
}
243-
else {
244-
// Consider adding logic to determine the class of the typeArg, if possible.
245-
// For now, just fall back...
246-
return method.getReturnType();
247-
}
264+
// Consider adding logic to determine the class of the typeArg, if possible.
265+
// For now, just fall back...
266+
return method.getReturnType();
248267
}
249268
}
250269
}

0 commit comments

Comments
 (0)