constructor argument type matching supports arrays and simple class names now (SPR-5844)
This commit is contained in:
parent
35b2a7e612
commit
cf29d1c367
|
|
@ -156,7 +156,7 @@ public class ConstructorArgumentValues {
|
|||
ValueHolder valueHolder = this.indexedArgumentValues.get(index);
|
||||
if (valueHolder != null &&
|
||||
(valueHolder.getType() == null ||
|
||||
(requiredType != null && requiredType.getName().equals(valueHolder.getType()))) &&
|
||||
(requiredType != null && ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) &&
|
||||
(valueHolder.getName() == null ||
|
||||
(requiredName != null && requiredName.equals(valueHolder.getName())))) {
|
||||
return valueHolder;
|
||||
|
|
@ -247,10 +247,12 @@ public class ConstructorArgumentValues {
|
|||
if (usedValueHolders != null && usedValueHolders.contains(valueHolder)) {
|
||||
continue;
|
||||
}
|
||||
if (valueHolder.getName() != null && (requiredName == null || !valueHolder.getName().equals(requiredName))) {
|
||||
if (valueHolder.getName() != null &&
|
||||
(requiredName == null || !valueHolder.getName().equals(requiredName))) {
|
||||
continue;
|
||||
}
|
||||
if (valueHolder.getType() != null && (requiredType == null || !valueHolder.getType().equals(requiredType.getName()))) {
|
||||
if (valueHolder.getType() != null &&
|
||||
(requiredType == null || !ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) {
|
||||
continue;
|
||||
}
|
||||
if (requiredType != null && valueHolder.getType() == null && valueHolder.getName() == null &&
|
||||
|
|
|
|||
|
|
@ -158,6 +158,10 @@
|
|||
<constructor-arg value="false"/>
|
||||
</bean>
|
||||
|
||||
<bean id="nan" class="java.lang.Double" factory-method="valueOf">
|
||||
<constructor-arg value="NaN" type="double"/>
|
||||
</bean>
|
||||
|
||||
<bean id="beanWithDoubleBoolean" class="org.springframework.beans.factory.xml.XmlBeanFactoryTests$DoubleBooleanConstructorBean" autowire="constructor" scope="prototype">
|
||||
<constructor-arg type="java.lang.Boolean"><value>true</value></constructor-arg>
|
||||
</bean>
|
||||
|
|
@ -166,4 +170,20 @@
|
|||
<constructor-arg index="1"><value>true</value></constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="constructorArray" class="org.springframework.beans.factory.xml.XmlBeanFactoryTests$ConstructorArrayTestBean">
|
||||
<constructor-arg type="int[]">
|
||||
<array value-type="int">
|
||||
<value>1</value>
|
||||
</array>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="indexedConstructorArray" class="org.springframework.beans.factory.xml.XmlBeanFactoryTests$ConstructorArrayTestBean">
|
||||
<constructor-arg index="0" type="int[]">
|
||||
<array value-type="int">
|
||||
<value>1</value>
|
||||
</array>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
|
|
|||
|
|
@ -1363,6 +1363,22 @@ public final class XmlBeanFactoryTests {
|
|||
assertEquals(Boolean.TRUE, bean.boolean2);
|
||||
}
|
||||
|
||||
public @Test void testPrimitiveConstructorArray() {
|
||||
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
|
||||
ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("constructorArray");
|
||||
assertTrue(bean.array instanceof int[]);
|
||||
assertEquals(1, ((int[]) bean.array).length);
|
||||
assertEquals(1, ((int[]) bean.array)[0]);
|
||||
}
|
||||
|
||||
public @Test void testIndexedPrimitiveConstructorArray() {
|
||||
XmlBeanFactory xbf = new XmlBeanFactory(CONSTRUCTOR_ARG_CONTEXT);
|
||||
ConstructorArrayTestBean bean = (ConstructorArrayTestBean) xbf.getBean("indexedConstructorArray");
|
||||
assertTrue(bean.array instanceof int[]);
|
||||
assertEquals(1, ((int[]) bean.array).length);
|
||||
assertEquals(1, ((int[]) bean.array)[0]);
|
||||
}
|
||||
|
||||
public @Test void testWithDuplicateName() throws Exception {
|
||||
try {
|
||||
new XmlBeanFactory(TEST_WITH_DUP_NAMES_CONTEXT);
|
||||
|
|
@ -1590,6 +1606,24 @@ public final class XmlBeanFactoryTests {
|
|||
}
|
||||
|
||||
|
||||
public static class ConstructorArrayTestBean {
|
||||
|
||||
public final Object array;
|
||||
|
||||
public ConstructorArrayTestBean(int[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
public ConstructorArrayTestBean(float[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
|
||||
public ConstructorArrayTestBean(short[] array) {
|
||||
this.array = array;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class WrappingPostProcessor implements BeanPostProcessor {
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
|
|
|
|||
|
|
@ -542,6 +542,17 @@ public abstract class ClassUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the given class matches the user-specified type name.
|
||||
* @param clazz the class to check
|
||||
* @param typeName the type name to match
|
||||
*/
|
||||
public static boolean matchesTypeName(Class clazz, String typeName) {
|
||||
return (typeName != null &&
|
||||
(typeName.equals(clazz.getName()) || typeName.equals(clazz.getSimpleName()) ||
|
||||
(clazz.isArray() && typeName.equals(getQualifiedNameForArray(clazz)))));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether the given class has a constructor with the given signature.
|
||||
|
|
@ -806,6 +817,7 @@ public abstract class ClassUtils {
|
|||
* @return the corresponding fully qualified class name
|
||||
*/
|
||||
public static String convertResourcePathToClassName(String resourcePath) {
|
||||
Assert.notNull(resourcePath, "Resource path must not be null");
|
||||
return resourcePath.replace('/', '.');
|
||||
}
|
||||
|
||||
|
|
@ -815,6 +827,7 @@ public abstract class ClassUtils {
|
|||
* @return the corresponding resource path, pointing to the class
|
||||
*/
|
||||
public static String convertClassNameToResourcePath(String className) {
|
||||
Assert.notNull(className, "Class name must not be null");
|
||||
return className.replace('.', '/');
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue