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);
|
ValueHolder valueHolder = this.indexedArgumentValues.get(index);
|
||||||
if (valueHolder != null &&
|
if (valueHolder != null &&
|
||||||
(valueHolder.getType() == null ||
|
(valueHolder.getType() == null ||
|
||||||
(requiredType != null && requiredType.getName().equals(valueHolder.getType()))) &&
|
(requiredType != null && ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) &&
|
||||||
(valueHolder.getName() == null ||
|
(valueHolder.getName() == null ||
|
||||||
(requiredName != null && requiredName.equals(valueHolder.getName())))) {
|
(requiredName != null && requiredName.equals(valueHolder.getName())))) {
|
||||||
return valueHolder;
|
return valueHolder;
|
||||||
|
|
@ -247,10 +247,12 @@ public class ConstructorArgumentValues {
|
||||||
if (usedValueHolders != null && usedValueHolders.contains(valueHolder)) {
|
if (usedValueHolders != null && usedValueHolders.contains(valueHolder)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (valueHolder.getName() != null && (requiredName == null || !valueHolder.getName().equals(requiredName))) {
|
if (valueHolder.getName() != null &&
|
||||||
|
(requiredName == null || !valueHolder.getName().equals(requiredName))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (valueHolder.getType() != null && (requiredType == null || !valueHolder.getType().equals(requiredType.getName()))) {
|
if (valueHolder.getType() != null &&
|
||||||
|
(requiredType == null || !ClassUtils.matchesTypeName(requiredType, valueHolder.getType()))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (requiredType != null && valueHolder.getType() == null && valueHolder.getName() == null &&
|
if (requiredType != null && valueHolder.getType() == null && valueHolder.getName() == null &&
|
||||||
|
|
|
||||||
|
|
@ -158,6 +158,10 @@
|
||||||
<constructor-arg value="false"/>
|
<constructor-arg value="false"/>
|
||||||
</bean>
|
</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">
|
<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>
|
<constructor-arg type="java.lang.Boolean"><value>true</value></constructor-arg>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
@ -166,4 +170,20 @@
|
||||||
<constructor-arg index="1"><value>true</value></constructor-arg>
|
<constructor-arg index="1"><value>true</value></constructor-arg>
|
||||||
</bean>
|
</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>
|
</beans>
|
||||||
|
|
|
||||||
|
|
@ -1363,6 +1363,22 @@ public final class XmlBeanFactoryTests {
|
||||||
assertEquals(Boolean.TRUE, bean.boolean2);
|
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 {
|
public @Test void testWithDuplicateName() throws Exception {
|
||||||
try {
|
try {
|
||||||
new XmlBeanFactory(TEST_WITH_DUP_NAMES_CONTEXT);
|
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 static class WrappingPostProcessor implements BeanPostProcessor {
|
||||||
|
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
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.
|
* 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
|
* @return the corresponding fully qualified class name
|
||||||
*/
|
*/
|
||||||
public static String convertResourcePathToClassName(String resourcePath) {
|
public static String convertResourcePathToClassName(String resourcePath) {
|
||||||
|
Assert.notNull(resourcePath, "Resource path must not be null");
|
||||||
return resourcePath.replace('/', '.');
|
return resourcePath.replace('/', '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -815,6 +827,7 @@ public abstract class ClassUtils {
|
||||||
* @return the corresponding resource path, pointing to the class
|
* @return the corresponding resource path, pointing to the class
|
||||||
*/
|
*/
|
||||||
public static String convertClassNameToResourcePath(String className) {
|
public static String convertClassNameToResourcePath(String className) {
|
||||||
|
Assert.notNull(className, "Class name must not be null");
|
||||||
return className.replace('.', '/');
|
return className.replace('.', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue