DATACMNS-1180 - Fixed accessor lookup for generic properties.

In AbstractPersistentProperty, we now resolve the potentially generic return and parameter types of getters and setters.
This commit is contained in:
Oliver Gierke
2017-10-26 23:29:02 +02:00
parent 14a56dee00
commit ce8d923382
2 changed files with 35 additions and 6 deletions

View File

@@ -61,15 +61,15 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
Assert.notNull(simpleTypeHolder, "SimpleTypeHolder must not be null!");
Assert.notNull(owner, "Owner entity must not be null!");
this.propertyDescriptor = propertyDescriptor;
this.field = field;
this.owner = owner;
this.simpleTypeHolder = simpleTypeHolder;
this.name = field == null ? propertyDescriptor.getName() : field.getName();
this.information = owner.getTypeInformation().getProperty(this.name);
this.rawType = this.information != null ? information.getType()
: field == null ? propertyDescriptor.getPropertyType() : field.getType();
this.propertyDescriptor = propertyDescriptor;
this.field = field;
this.association = isAssociation() ? createAssociation() : null;
this.owner = owner;
this.simpleTypeHolder = simpleTypeHolder;
this.hashCode = this.field == null ? this.propertyDescriptor.hashCode() : this.field.hashCode();
}
@@ -163,7 +163,11 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
return null;
}
return rawType.isAssignableFrom(getter.getReturnType()) ? getter : null;
Class<?> returnType = owner.getTypeInformation() //
.getReturnType(getter) //
.getType();
return rawType.isAssignableFrom(returnType) ? getter : null;
}
/*
@@ -183,7 +187,12 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
return null;
}
return setter.getParameterTypes()[0].isAssignableFrom(rawType) ? setter : null;
Class<?> parameterType = owner.getTypeInformation() //
.getParameterTypes(setter) //
.get(0) //
.getType();
return parameterType.isAssignableFrom(rawType) ? setter : null;
}
/*

View File

@@ -18,6 +18,9 @@ package org.springframework.data.mapping.model;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import lombok.Getter;
import lombok.Setter;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
@@ -208,6 +211,15 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.getRawType(), is(typeCompatibleWith(String.class)));
}
@Test // DATACMNS-1180
public void returnsAccessorsForGenericReturnType() {
SamplePersistentProperty property = getProperty(ConcreteGetter.class, "genericField");
assertThat(property.getSetter(), is(notNullValue()));
assertThat(property.getGetter(), is(notNullValue()));
}
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
return getProperty(type, name, getPropertyDescriptor(type, name));
}
@@ -253,6 +265,14 @@ public class AbstractPersistentPropertyUnitTests {
}
@Getter
@Setter
class GenericGetter<T> {
T genericField;
}
class ConcreteGetter extends GenericGetter<String> {}
@SuppressWarnings("serial")
class TestClassSet extends TreeSet<Object> {}