Browse Source

canConvert checks Collection/Map element types as well (SPR-6564)

pull/23217/head
Juergen Hoeller 15 years ago
parent
commit
3db5a299bb
  1. 49
      org.springframework.context/src/test/java/org/springframework/beans/ResourceTestBean.java
  2. 48
      org.springframework.context/src/test/java/org/springframework/context/support/ConversionServiceFactoryBeanTests.java
  3. 31
      org.springframework.context/src/test/java/org/springframework/context/support/conversionService.xml
  4. 35
      org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

49
org.springframework.context/src/test/java/org/springframework/beans/ResourceTestBean.java

@ -1,6 +1,23 @@ @@ -1,6 +1,23 @@
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans;
import java.io.InputStream;
import java.util.Map;
import org.springframework.core.io.Resource;
@ -14,6 +31,13 @@ public class ResourceTestBean { @@ -14,6 +31,13 @@ public class ResourceTestBean {
private InputStream inputStream;
private Resource[] resourceArray;
private Map<String, Resource> resourceMap;
private Map<String, Resource[]> resourceArrayMap;
public ResourceTestBean() {
}
@ -22,6 +46,7 @@ public class ResourceTestBean { @@ -22,6 +46,7 @@ public class ResourceTestBean {
this.inputStream = inputStream;
}
public void setResource(Resource resource) {
this.resource = resource;
}
@ -38,4 +63,28 @@ public class ResourceTestBean { @@ -38,4 +63,28 @@ public class ResourceTestBean {
return inputStream;
}
public Resource[] getResourceArray() {
return resourceArray;
}
public void setResourceArray(Resource[] resourceArray) {
this.resourceArray = resourceArray;
}
public Map<String, Resource> getResourceMap() {
return resourceMap;
}
public void setResourceMap(Map<String, Resource> resourceMap) {
this.resourceMap = resourceMap;
}
public Map<String, Resource[]> getResourceArrayMap() {
return resourceArrayMap;
}
public void setResourceArrayMap(Map<String, Resource[]> resourceArrayMap) {
this.resourceArrayMap = resourceArrayMap;
}
}

48
org.springframework.context/src/test/java/org/springframework/context/support/ConversionServiceFactoryBeanTests.java

@ -1,18 +1,42 @@ @@ -1,18 +1,42 @@
package org.springframework.context.support;
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import static org.junit.Assert.assertTrue;
package org.springframework.context.support;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.ResourceTestBean;
import org.springframework.context.ApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
/**
* @author Keith Donald
* @author Juergen Hoeller
*/
public class ConversionServiceFactoryBeanTests {
@Test
@ -67,15 +91,27 @@ public class ConversionServiceFactoryBeanTests { @@ -67,15 +91,27 @@ public class ConversionServiceFactoryBeanTests {
factory.afterPropertiesSet();
}
@Test
public void conversionServiceInApplicationContext() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("conversionService.xml", getClass());
ResourceTestBean tb = ctx.getBean("resourceTestBean", ResourceTestBean.class);
assertTrue(tb.getResource() instanceof ClassPathResource);
assertTrue(tb.getResourceArray().length > 1);
assertTrue(tb.getResourceArray()[0] instanceof FileSystemResource);
assertTrue(tb.getResourceMap().size() == 1);
assertTrue(tb.getResourceMap().get("key1") instanceof ClassPathResource);
assertTrue(tb.getResourceArrayMap().size() == 1);
assertTrue(tb.getResourceArrayMap().get("key1").length > 1);
assertTrue(tb.getResourceArrayMap().get("key1")[0] instanceof FileSystemResource);
}
public static class Foo {
}
public static class Bar {
}
public static class Baz {
}
}

31
org.springframework.context/src/test/java/org/springframework/context/support/conversionService.xml

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map key-type="java.lang.String" value-type="java.lang.Class">
<entry key="org.springframework.core.io.Resource[]" value="org.springframework.core.io.support.ResourceArrayPropertyEditor"/>
</map>
</property>
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"/>
<bean id="resourceTestBean" class="org.springframework.beans.ResourceTestBean">
<property name="resource" value="org/springframework/context/support/conversionService.xml"/>
<property name="resourceArray" value="org/springframework/context/support/*.xml"/>
<property name="resourceMap">
<map>
<entry key="key1" value="org/springframework/context/support/conversionService.xml"/>
</map>
</property>
<property name="resourceArrayMap">
<map>
<entry key="key1" value="org/springframework/context/support/*.xml"/>
</map>
</property>
</bean>
</beans>

35
org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

@ -269,7 +269,7 @@ public class TypeDescriptor { @@ -269,7 +269,7 @@ public class TypeDescriptor {
@SuppressWarnings("unchecked")
public Class<?> getMapKeyType() {
if (this.field != null) {
return GenericCollectionTypeResolver.getMapKeyFieldType(field);
return GenericCollectionTypeResolver.getMapKeyFieldType(this.field);
}
else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter);
@ -370,7 +370,38 @@ public class TypeDescriptor { @@ -370,7 +370,38 @@ public class TypeDescriptor {
* @return true if this type is assignable to the target
*/
public boolean isAssignableTo(TypeDescriptor targetType) {
return isTypeAssignableTo(targetType.getType());
Class targetClass = targetType.getType();
if (!isTypeAssignableTo(targetClass)) {
return false;
}
if (targetClass != null) {
if (Collection.class.isAssignableFrom(targetClass)) {
Class<?> elementType = targetType.getCollectionElementType();
if (elementType != null) {
Class<?> sourceElementType = getCollectionElementType();
if (sourceElementType == null || !elementType.isAssignableFrom(sourceElementType)) {
return false;
}
}
}
else if (Map.class.isAssignableFrom(targetClass)) {
Class<?> keyType = targetType.getMapKeyType();
if (keyType != null) {
Class<?> sourceKeyType = getMapKeyType();
if (sourceKeyType == null || !keyType.isAssignableFrom(sourceKeyType)) {
return false;
}
}
Class<?> valueType = targetType.getMapValueType();
if (valueType != null) {
Class<?> sourceValueType = getMapValueType();
if (sourceValueType == null || !valueType.isAssignableFrom(sourceValueType)) {
return false;
}
}
}
}
return true;
}
/**

Loading…
Cancel
Save