Browse Source

introspect element type in case of incoming Collection/Map in order to not accidentally say canConvert=true (SPR-6564)

pull/23217/head
Juergen Hoeller 15 years ago
parent
commit
2153b2fbd5
  1. 2
      org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java
  2. 20
      org.springframework.context/src/test/java/org/springframework/context/conversionservice/Bar.java
  3. 26
      org.springframework.context/src/test/java/org/springframework/context/conversionservice/ConversionServiceContextConfigTests.java
  4. 19
      org.springframework.context/src/test/java/org/springframework/context/conversionservice/StringToBarConverter.java
  5. 53
      org.springframework.context/src/test/java/org/springframework/context/conversionservice/TestClient.java
  6. 16
      org.springframework.context/src/test/resources/org/springframework/context/conversionservice/conversionService.xml
  7. 64
      org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  8. 26
      org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java

2
org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

@ -194,7 +194,7 @@ class TypeConverterDelegate {
// No custom editor but custom ConversionService specified? // No custom editor but custom ConversionService specified?
ConversionService conversionService = this.propertyEditorRegistry.getConversionService(); ConversionService conversionService = this.propertyEditorRegistry.getConversionService();
if (editor == null && conversionService != null && convertedValue != null) { if (editor == null && conversionService != null && convertedValue != null) {
TypeDescriptor sourceTypeDesc = TypeDescriptor.valueOf(convertedValue.getClass()); TypeDescriptor sourceTypeDesc = new TypeDescriptor(convertedValue);
if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) { if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {
return (T) conversionService.convert(convertedValue, sourceTypeDesc, typeDescriptor); return (T) conversionService.convert(convertedValue, sourceTypeDesc, typeDescriptor);
} }

20
org.springframework.context/src/test/java/org/springframework/context/conversionservice/Bar.java

@ -1,5 +1,24 @@
/*
* Copyright 2002-2006 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.context.conversionservice; package org.springframework.context.conversionservice;
/**
* @author Keith Donald
*/
public class Bar { public class Bar {
private String value; private String value;
@ -11,4 +30,5 @@ public class Bar {
public String getValue() { public String getValue() {
return value; return value;
} }
} }

26
org.springframework.context/src/test/java/org/springframework/context/conversionservice/ConversionServiceContextConfigTests.java

@ -1,16 +1,32 @@
package org.springframework.context.conversionservice; /*
* Copyright 2002-2006 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.assertEquals; package org.springframework.context.conversionservice;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Keith Donald
*/
public class ConversionServiceContextConfigTests { public class ConversionServiceContextConfigTests {
@Test @Test
@Ignore
public void testConfigOk() { public void testConfigOk() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/context/conversionservice/conversionService.xml"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/springframework/context/conversionservice/conversionService.xml");
TestClient client = context.getBean("testClient", TestClient.class); TestClient client = context.getBean("testClient", TestClient.class);

19
org.springframework.context/src/test/java/org/springframework/context/conversionservice/StringToBarConverter.java

@ -1,7 +1,26 @@
/*
* Copyright 2002-2006 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.context.conversionservice; package org.springframework.context.conversionservice;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
/**
* @author Keith Donald
*/
public class StringToBarConverter implements Converter<String, Bar> { public class StringToBarConverter implements Converter<String, Bar> {
public Bar convert(String source) { public Bar convert(String source) {

53
org.springframework.context/src/test/java/org/springframework/context/conversionservice/TestClient.java

@ -1,15 +1,44 @@
/*
* Copyright 2002-2006 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.context.conversionservice; package org.springframework.context.conversionservice;
import java.util.List; import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
/**
* @author Keith Donald
* @author Juergen Hoeller
*/
public class TestClient { public class TestClient {
private List<Bar> bars; private List<Bar> bars;
private boolean bool; private boolean bool;
private Resource[] resourceArray;
private List<Resource> resourceList;
private Map<String, Resource> resourceMap;
public List<Bar> getBars() { public List<Bar> getBars() {
return bars; return bars;
} }
@ -27,4 +56,28 @@ public class TestClient {
this.bool = bool; this.bool = bool;
} }
public Resource[] getResourceArray() {
return resourceArray;
}
public void setResourceArray(Resource[] resourceArray) {
this.resourceArray = resourceArray;
}
public List<Resource> getResourceList() {
return resourceList;
}
public void setResourceList(List<Resource> resourceList) {
this.resourceList = resourceList;
}
public Map<String, Resource> getResourceMap() {
return resourceMap;
}
public void setResourceMap(Map<String, Resource> resourceMap) {
this.resourceMap = resourceMap;
}
} }

16
org.springframework.context/src/test/resources/org/springframework/context/conversionservice/conversionService.xml

@ -5,7 +5,7 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="conversionService" class="org.springframework.core.convert.support.DefaultConversionService"> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters"> <property name="converters">
<bean class="org.springframework.context.conversionservice.StringToBarConverter" /> <bean class="org.springframework.context.conversionservice.StringToBarConverter" />
</property> </property>
@ -13,6 +13,20 @@
<bean id="testClient" class="org.springframework.context.conversionservice.TestClient"> <bean id="testClient" class="org.springframework.context.conversionservice.TestClient">
<property name="bool" value="true"/> <property name="bool" value="true"/>
<property name="resourceArray">
<value>classpath:test.xml</value>
</property>
<property name="resourceList">
<list>
<value>classpath:test.xml</value>
</list>
</property>
<property name="resourceMap">
<map>
<entry key="res1" value="classpath:test1.xml"/>
<entry key="res2" value="classpath:test2.xml"/>
</map>
</property>
</bean> </bean>
<bean class="org.springframework.context.conversionservice.Bar"> <bean class="org.springframework.context.conversionservice.Bar">

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

@ -47,6 +47,8 @@ public class TypeDescriptor {
public static final TypeDescriptor STRING = TypeDescriptor.valueOf(String.class); public static final TypeDescriptor STRING = TypeDescriptor.valueOf(String.class);
private Object value;
private Class<?> type; private Class<?> type;
private TypeDescriptor elementType; private TypeDescriptor elementType;
@ -58,10 +60,22 @@ public class TypeDescriptor {
private Annotation[] cachedFieldAnnotations; private Annotation[] cachedFieldAnnotations;
/**
* Create a new descriptor for the type of the given value.
* <p>Use this constructor when a conversion point comes from a source such as a Map or
* Collection, where no additional context is available but elements can be introspected.
* @param type the actual type to wrap
*/
public TypeDescriptor(Object value) {
Assert.notNull(value, "Value must not be null");
this.value = value;
this.type = value.getClass();
}
/** /**
* Create a new descriptor for the given type. * Create a new descriptor for the given type.
* <p>Use this constructor when a conversion point comes from a source such as a Map * <p>Use this constructor when a conversion point comes from a plain source type,
* or Collection, where no additional context is available. * where no additional context is available.
* @param type the actual type to wrap * @param type the actual type to wrap
*/ */
public TypeDescriptor(Class<?> type) { public TypeDescriptor(Class<?> type) {
@ -242,6 +256,7 @@ public class TypeDescriptor {
* Determine the generic key type of the wrapped Map parameter/field, if any. * Determine the generic key type of the wrapped Map parameter/field, if any.
* @return the generic type, or <code>null</code> if none * @return the generic type, or <code>null</code> if none
*/ */
@SuppressWarnings("unchecked")
public Class<?> getMapKeyType() { public Class<?> getMapKeyType() {
if (this.field != null) { if (this.field != null) {
return GenericCollectionTypeResolver.getMapKeyFieldType(field); return GenericCollectionTypeResolver.getMapKeyFieldType(field);
@ -249,6 +264,18 @@ public class TypeDescriptor {
else if (this.methodParameter != null) { else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter); return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter);
} }
else if (this.value instanceof Map) {
Map map = (Map) this.value;
if (!map.isEmpty()) {
Object key = map.keySet().iterator().next();
if (key != null) {
return key.getClass();
}
}
}
if (this.type != null) {
return GenericCollectionTypeResolver.getMapKeyType((Class<? extends Map>) this.type);
}
else { else {
return null; return null;
} }
@ -258,6 +285,7 @@ public class TypeDescriptor {
* Determine the generic value type of the wrapped Map parameter/field, if any. * Determine the generic value type of the wrapped Map parameter/field, if any.
* @return the generic type, or <code>null</code> if none * @return the generic type, or <code>null</code> if none
*/ */
@SuppressWarnings("unchecked")
public Class<?> getMapValueType() { public Class<?> getMapValueType() {
if (this.field != null) { if (this.field != null) {
return GenericCollectionTypeResolver.getMapValueFieldType(this.field); return GenericCollectionTypeResolver.getMapValueFieldType(this.field);
@ -265,6 +293,18 @@ public class TypeDescriptor {
else if (this.methodParameter != null) { else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter); return GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter);
} }
else if (this.value instanceof Map) {
Map map = (Map) this.value;
if (!map.isEmpty()) {
Object val = map.values().iterator().next();
if (val != null) {
return val.getClass();
}
}
}
if (this.type != null) {
return GenericCollectionTypeResolver.getMapValueType((Class<? extends Map>) this.type);
}
else { else {
return null; return null;
} }
@ -349,14 +389,26 @@ public class TypeDescriptor {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Class<?> getCollectionElementType() { private Class<?> getCollectionElementType() {
if (this.field != null) {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
}
else if (this.methodParameter != null) {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
}
else if (this.value instanceof Collection) {
Collection coll = (Collection) this.value;
if (!coll.isEmpty()) {
Object elem = coll.iterator().next();
if (elem != null) {
return elem.getClass();
}
}
}
if (this.type != null) { if (this.type != null) {
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type); return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
} }
else if (this.field != null) {
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
}
else { else {
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter); return null;
} }
} }

26
org.springframework.core/src/test/java/org/springframework/core/io/support/ResourceArrayPropertyEditorTests.java

@ -1,15 +1,33 @@
/*
* Copyright 2002-2006 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.core.io.support; package org.springframework.core.io.support;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.Resource;
/**
* @author Dave Syer
*/
public class ResourceArrayPropertyEditorTests { public class ResourceArrayPropertyEditorTests {
private ResourceArrayPropertyEditor editor = new ResourceArrayPropertyEditor(); private ResourceArrayPropertyEditor editor = new ResourceArrayPropertyEditor();
@Test @Test
public void testVanillaResource() throws Exception { public void testVanillaResource() throws Exception {
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class"); editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");

Loading…
Cancel
Save