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
This commit is contained in:
Chris Beams 2011-06-16 07:53:42 +00:00
parent cc814519c8
commit 5dc2d56600
2 changed files with 28 additions and 3 deletions

View File

@ -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;
}

View File

@ -226,6 +226,14 @@ public class TypeDescriptorTests {
assertEquals(Integer.class, desc.getType());
}
@Test
public void propertyTypeCovariance() throws Exception {
GenericType<Number> 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<Integer> genericBean = new IntegerType();
@ -270,6 +278,23 @@ public class TypeDescriptorTests {
}
public class NumberType implements GenericType<Number> {
public Integer getProperty() {
return null;
}
public void setProperty(Number t) {
}
public List<Number> getListProperty() {
return null;
}
public void setListProperty(List<Number> t) {
}
}
@Test
public void propertyGenericClassList() throws Exception {
IntegerClass genericBean = new IntegerClass();