diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 1054aa5d974..538df6a2457 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -172,7 +172,7 @@ public class GenericConversionService implements ConversionService, ConverterReg } GenericConverter converter = getConverter(sourceType, targetType); if (converter == null) { - if (targetType.getType().isInstance(source)) { + if (source == null || targetType.getType().isInstance(source)) { logger.debug("No converter found - returning assignable source object as-is"); return source; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java index 69009f096e9..b2813a7fb15 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/PropertyTypeDescriptor.java @@ -117,4 +117,13 @@ public class PropertyTypeDescriptor extends TypeDescriptor { return anns; } + public TypeDescriptor forElementType(Class elementType) { + if (elementType == null) { + return TypeDescriptor.UNKNOWN; + } + else { + return new PropertyTypeDescriptor(this.propertyDescriptor, getMethodParameter(), elementType); + } + } + } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java index 12d46fb9da0..94bdb79b342 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * 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. @@ -16,12 +16,6 @@ package org.springframework.core.convert.support; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNull; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - import java.math.BigDecimal; import java.math.BigInteger; import java.util.AbstractList; @@ -39,7 +33,9 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import static org.junit.Assert.*; import org.junit.Test; + import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConverterNotFoundException; @@ -509,7 +505,7 @@ public class DefaultConversionTests { foo.add("1"); foo.add("2"); foo.add("3"); - List bar = (List) conversionService.convert(foo, TypeDescriptor.valueOf(LinkedHashSet.class), + List bar = (List) conversionService.convert(foo, TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericList"))); assertEquals(new Integer(1), bar.get(0)); assertEquals(new Integer(2), bar.get(1)); @@ -544,7 +540,7 @@ public class DefaultConversionTests { map.put("3", "3"); Collection values = map.values(); List bar = (List) conversionService.convert(values, - TypeDescriptor.valueOf(values.getClass()), new TypeDescriptor(getClass().getField("genericList"))); + TypeDescriptor.forObject(values), new TypeDescriptor(getClass().getField("genericList"))); assertEquals(3, bar.size()); assertEquals(new Integer(1), bar.get(0)); assertEquals(new Integer(2), bar.get(1)); @@ -558,8 +554,8 @@ public class DefaultConversionTests { Map foo = new HashMap(); foo.put("1", "BAR"); foo.put("2", "BAZ"); - Map map = (Map) conversionService.convert(foo, TypeDescriptor - .valueOf(Map.class), new TypeDescriptor(getClass().getField("genericMap"))); + Map map = (Map) conversionService.convert(foo, + TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap"))); assertEquals(FooEnum.BAR, map.get(1)); assertEquals(FooEnum.BAZ, map.get(2)); }