diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java b/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java index 9ba74afffc4..a9f60899502 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/Property.java @@ -133,8 +133,8 @@ public final class Property { if (read == null && write == null) { throw new IllegalStateException("Property is neither readable nor writeable"); } - if (read != null && write != null && !read.getParameterType().equals(write.getParameterType())) { - throw new IllegalStateException("Read and write parameter types are not the same"); + if (read != null && write != null && !write.getParameterType().isAssignableFrom(read.getParameterType())) { + throw new IllegalStateException("Write parameter is not assignable from read parameter"); } return read != null ? read : write; } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java index 69a87f83ad6..141b86515ca 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java @@ -226,6 +226,14 @@ public class TypeDescriptorTests { assertEquals(Integer.class, desc.getType()); } + @Test + public void propertyTypeCovariance() throws Exception { + GenericType genericBean = new NumberType(); + Property property = new Property(getClass(), genericBean.getClass().getMethod("getProperty", null), genericBean.getClass().getMethod("setProperty", Number.class)); + TypeDescriptor desc = new TypeDescriptor(property); + assertEquals(Integer.class, desc.getType()); + } + @Test public void propertyGenericTypeList() throws Exception { GenericType genericBean = new IntegerType(); @@ -239,7 +247,7 @@ public class TypeDescriptorTests { T getProperty(); void setProperty(T t); - + List getListProperty(); void setListProperty(List t); @@ -270,6 +278,23 @@ public class TypeDescriptorTests { } + public class NumberType implements GenericType { + + public Integer getProperty() { + return null; + } + + public void setProperty(Number t) { + } + + public List getListProperty() { + return null; + } + + public void setListProperty(List t) { + } + } + @Test public void propertyGenericClassList() throws Exception { IntegerClass genericBean = new IntegerClass();