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:
parent
cc814519c8
commit
5dc2d56600
|
|
@ -133,8 +133,8 @@ public final class Property {
|
||||||
if (read == null && write == null) {
|
if (read == null && write == null) {
|
||||||
throw new IllegalStateException("Property is neither readable nor writeable");
|
throw new IllegalStateException("Property is neither readable nor writeable");
|
||||||
}
|
}
|
||||||
if (read != null && write != null && !read.getParameterType().equals(write.getParameterType())) {
|
if (read != null && write != null && !write.getParameterType().isAssignableFrom(read.getParameterType())) {
|
||||||
throw new IllegalStateException("Read and write parameter types are not the same");
|
throw new IllegalStateException("Write parameter is not assignable from read parameter");
|
||||||
}
|
}
|
||||||
return read != null ? read : write;
|
return read != null ? read : write;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -226,6 +226,14 @@ public class TypeDescriptorTests {
|
||||||
assertEquals(Integer.class, desc.getType());
|
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
|
@Test
|
||||||
public void propertyGenericTypeList() throws Exception {
|
public void propertyGenericTypeList() throws Exception {
|
||||||
GenericType<Integer> genericBean = new IntegerType();
|
GenericType<Integer> genericBean = new IntegerType();
|
||||||
|
|
@ -239,7 +247,7 @@ public class TypeDescriptorTests {
|
||||||
T getProperty();
|
T getProperty();
|
||||||
|
|
||||||
void setProperty(T t);
|
void setProperty(T t);
|
||||||
|
|
||||||
List<T> getListProperty();
|
List<T> getListProperty();
|
||||||
|
|
||||||
void setListProperty(List<T> t);
|
void setListProperty(List<T> t);
|
||||||
|
|
@ -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
|
@Test
|
||||||
public void propertyGenericClassList() throws Exception {
|
public void propertyGenericClassList() throws Exception {
|
||||||
IntegerClass genericBean = new IntegerClass();
|
IntegerClass genericBean = new IntegerClass();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue