From ce8d923382145d651780aa5e480c294dcc9ce6b3 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 26 Oct 2017 23:29:02 +0200 Subject: [PATCH] DATACMNS-1180 - Fixed accessor lookup for generic properties. In AbstractPersistentProperty, we now resolve the potentially generic return and parameter types of getters and setters. --- .../model/AbstractPersistentProperty.java | 21 +++++++++++++------ .../AbstractPersistentPropertyUnitTests.java | 20 ++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index 9e64ebfa8..e45d47b6b 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -61,15 +61,15 @@ public abstract class AbstractPersistentProperty

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

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

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; } /* diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 65c8b494f..dac2eb2ac 100644 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -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 SamplePersistentProperty getProperty(Class type, String name) { return getProperty(type, name, getPropertyDescriptor(type, name)); } @@ -253,6 +265,14 @@ public class AbstractPersistentPropertyUnitTests { } + @Getter + @Setter + class GenericGetter { + T genericField; + } + + class ConcreteGetter extends GenericGetter {} + @SuppressWarnings("serial") class TestClassSet extends TreeSet {}