From 431ca9314af6929a53b177640522323602e6d224 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 18 Mar 2016 18:51:33 +0100 Subject: [PATCH] Leniently allow constructor argument matches if required name is not resolvable Issue: SPR-13987 --- .../factory/config/ConstructorArgumentValues.java | 12 ++++++------ .../beans/factory/support/ConstructorResolver.java | 2 +- .../beans/factory/xml/XmlBeanFactoryTests.java | 4 +++- .../xml/XmlBeanFactoryTests-constructorArg.xml | 4 ++++ 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java index 9a57c7aef3..6f7d2d904a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -156,7 +156,7 @@ public class ConstructorArgumentValues { * @param requiredType the type to match (can be {@code null} to match * untyped values only) * @param requiredName the type to match (can be {@code null} to match - * unnamed values only) + * unnamed values only, or empty String to match any name) * @return the ValueHolder for the argument, or {@code null} if none set */ public ValueHolder getIndexedArgumentValue(int index, Class requiredType, String requiredName) { @@ -165,7 +165,7 @@ public class ConstructorArgumentValues { if (valueHolder != null && (valueHolder.getType() == null || (requiredType != null && ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) && - (valueHolder.getName() == null || + (valueHolder.getName() == null || "".equals(requiredName) || (requiredName != null && requiredName.equals(valueHolder.getName())))) { return valueHolder; } @@ -268,7 +268,7 @@ public class ConstructorArgumentValues { * @param requiredType the type to match (can be {@code null} to find * an arbitrary next generic argument value) * @param requiredName the name to match (can be {@code null} to not - * match argument values by name) + * match argument values by name, or empty String to match any name) * @param usedValueHolders a Set of ValueHolder objects that have already been used * in the current resolution process and should therefore not be returned again * @return the ValueHolder for the argument, or {@code null} if none found @@ -278,7 +278,7 @@ public class ConstructorArgumentValues { if (usedValueHolders != null && usedValueHolders.contains(valueHolder)) { continue; } - if (valueHolder.getName() != null && + if (valueHolder.getName() != null && !"".equals(requiredName) && (requiredName == null || !valueHolder.getName().equals(requiredName))) { continue; } @@ -335,7 +335,7 @@ public class ConstructorArgumentValues { * @param requiredType the parameter type to match (can be {@code null} * to find an untyped argument value) * @param requiredName the parameter name to match (can be {@code null} - * to find an unnamed argument value) + * to find an unnamed argument value, or empty String to match any name) * @param usedValueHolders a Set of ValueHolder objects that have already * been used in the current resolution process and should therefore not * be returned again (allowing to return the next generic argument match diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index ff7d4d8b07..00437ae86b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -675,7 +675,7 @@ class ConstructorResolver { for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) { Class paramType = paramTypes[paramIndex]; - String paramName = (paramNames != null ? paramNames[paramIndex] : null); + String paramName = (paramNames != null ? paramNames[paramIndex] : ""); // Try to find matching constructor argument value, either indexed or generic. ConstructorArgumentValues.ValueHolder valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders); diff --git a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 88d1cb09f3..e526b0d001 100644 --- a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -1612,12 +1612,14 @@ public class XmlBeanFactoryTests { assertEquals(0, ((String[]) bean.array).length); } - @Test @Ignore // TODO: SPR-13987 + @Test public void testConstructorWithUnresolvableParameterName() { DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT); AtomicInteger bean = (AtomicInteger) xbf.getBean("constructorUnresolvableName"); assertEquals(1, bean.get()); + bean = (AtomicInteger) xbf.getBean("constructorUnresolvableNameWithIndex"); + assertEquals(1, bean.get()); } @Test diff --git a/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml b/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml index b26bee540a..d7241c9c92 100644 --- a/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml +++ b/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml @@ -231,4 +231,8 @@ + + + +