From 5dc2d56600368e27da113dc31facd487b555b16d Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Thu, 16 Jun 2011 07:53:42 +0000 Subject: [PATCH] Fix regression with covariant property return types Prior to this change, the Property class introduced in 3.1 M2 validated read/write property method pairs based on whether their parameter/return types were equal to one another. This precluded the valid possibility of read method that returns a subtype of the write method's parameter type, and represented a regression against 3.1 M1 and earlier versions. The implementation now uses isAssignableFrom rather than a straight equals check against the types. Issue: SPR-8432 --- .../core/convert/Property.java | 4 +-- .../core/convert/TypeDescriptorTests.java | 27 ++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) 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();