From cf29d1c367683fcb5159e2ea2721b81eb7925a00 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 16 Jul 2009 11:25:52 +0000 Subject: [PATCH] constructor argument type matching supports arrays and simple class names now (SPR-5844) --- .../config/ConstructorArgumentValues.java | 8 +++-- .../XmlBeanFactoryTests-constructorArg.xml | 20 +++++++++++ .../factory/xml/XmlBeanFactoryTests.java | 34 +++++++++++++++++++ .../org/springframework/util/ClassUtils.java | 13 +++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java index e0812e1fa8e..70d8f6e9ab8 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/ConstructorArgumentValues.java @@ -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 && diff --git a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml index ab1803c4398..c61eb498015 100644 --- a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml +++ b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests-constructorArg.xml @@ -158,6 +158,10 @@ + + + + true @@ -166,4 +170,20 @@ true + + + + 1 + + + + + + + + 1 + + + + diff --git a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 8d578b2541b..9162d47c8b9 100644 --- a/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/org.springframework.context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -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 { diff --git a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java index 638f8333e1a..249eb03d659 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java @@ -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('.', '/'); }