From 766c9078f23d9af9a8d6ebc3b991c07fadee3594 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 14 Aug 2017 13:39:25 +0200 Subject: [PATCH] DATACMNS-1139 - AbstractPersistentProperty.getRawType() now correctly resolves generics. We're now favoring the generic TypeInformation over trying to resolve the property type via field or PropertyDescriptor as only the former does proper generic type resolution. --- .../model/AbstractPersistentProperty.java | 3 +- .../AbstractPersistentPropertyUnitTests.java | 65 ++++++++----------- 2 files changed, 30 insertions(+), 38 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 c36e1ad04..9e64ebfa8 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -62,8 +62,9 @@ public abstract class AbstractPersistentProperty

Assert.notNull(owner, "Owner entity must not be null!"); this.name = field == null ? propertyDescriptor.getName() : field.getName(); - this.rawType = field == null ? propertyDescriptor.getPropertyType() : field.getType(); 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; 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 6a2007827..65c8b494f 100644 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -36,7 +36,6 @@ import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.Person; import org.springframework.data.util.ClassTypeInformation; -import org.springframework.data.util.TypeInformation; import org.springframework.util.ReflectionUtils; /** @@ -46,60 +45,50 @@ import org.springframework.util.ReflectionUtils; */ public class AbstractPersistentPropertyUnitTests { - TypeInformation typeInfo; - PersistentEntity entity; SimpleTypeHolder typeHolder; @Before public void setUp() { - - typeInfo = ClassTypeInformation.from(TestClassComplex.class); - entity = new BasicPersistentEntity(typeInfo); typeHolder = new SimpleTypeHolder(); } @Test // DATACMNS-68 public void discoversComponentTypeCorrectly() throws Exception { - Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet"); + SamplePersistentProperty property = getProperty(TestClassComplex.class, "testClassSet"); - SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder); property.getComponentType(); } @Test // DATACMNS-101 public void returnsNestedEntityTypeCorrectly() { - Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet"); + SamplePersistentProperty property = getProperty(TestClassComplex.class, "testClassSet", null); - SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder); assertThat(property.getPersistentEntityType().iterator().hasNext(), is(false)); } @Test // DATACMNS-132 public void isEntityWorksForUntypedMaps() throws Exception { - Field field = ReflectionUtils.findField(TestClassComplex.class, "map"); - SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder); + SamplePersistentProperty property = getProperty(TestClassComplex.class, "map", null); + assertThat(property.isEntity(), is(false)); } @Test // DATACMNS-132 public void isEntityWorksForUntypedCollection() throws Exception { - Field field = ReflectionUtils.findField(TestClassComplex.class, "collection"); - SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder); + SamplePersistentProperty property = getProperty(TestClassComplex.class, "collection", null); + assertThat(property.isEntity(), is(false)); } @Test // DATACMNS-121 public void considersPropertiesEqualIfFieldEquals() { - Field first = ReflectionUtils.findField(FirstConcrete.class, "genericField"); - Field second = ReflectionUtils.findField(SecondConcrete.class, "genericField"); - - SamplePersistentProperty firstProperty = new SamplePersistentProperty(first, null, entity, typeHolder); - SamplePersistentProperty secondProperty = new SamplePersistentProperty(second, null, entity, typeHolder); + SamplePersistentProperty firstProperty = getProperty(FirstConcrete.class, "genericField", null); + SamplePersistentProperty secondProperty = getProperty(SecondConcrete.class, "genericField", null); assertThat(firstProperty, is(secondProperty)); assertThat(firstProperty.hashCode(), is(secondProperty.hashCode())); @@ -108,18 +97,15 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-180 public void doesNotConsiderJavaTransientFieldsTransient() { - Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField"); + PersistentProperty property = getProperty(TestClassComplex.class, "transientField", null); - PersistentProperty property = new SamplePersistentProperty(transientField, null, entity, typeHolder); assertThat(property.isTransient(), is(false)); } @Test // DATACMNS-206 public void findsSimpleGettersAndASetters() { - Field field = ReflectionUtils.findField(AccessorTestClass.class, "id"); - PersistentProperty property = new SamplePersistentProperty(field, getPropertyDescriptor( - AccessorTestClass.class, "id"), entity, typeHolder); + PersistentProperty property = getProperty(AccessorTestClass.class, "id"); assertThat(property.getGetter(), is(notNullValue())); assertThat(property.getSetter(), is(notNullValue())); @@ -128,9 +114,7 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-206 public void doesNotUseInvalidGettersAndASetters() { - Field field = ReflectionUtils.findField(AccessorTestClass.class, "anotherId"); - PersistentProperty property = new SamplePersistentProperty(field, getPropertyDescriptor( - AccessorTestClass.class, "anotherId"), entity, typeHolder); + PersistentProperty property = getProperty(AccessorTestClass.class, "anotherId"); assertThat(property.getGetter(), is(nullValue())); assertThat(property.getSetter(), is(nullValue())); @@ -139,9 +123,7 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-206 public void usesCustomGetter() { - Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetAnotherId"); - PersistentProperty property = new SamplePersistentProperty(field, getPropertyDescriptor( - AccessorTestClass.class, "yetAnotherId"), entity, typeHolder); + PersistentProperty property = getProperty(AccessorTestClass.class, "yetAnotherId"); assertThat(property.getGetter(), is(notNullValue())); assertThat(property.getSetter(), is(nullValue())); @@ -150,9 +132,8 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-206 public void usesCustomSetter() { - Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetYetAnotherId"); - PersistentProperty property = new SamplePersistentProperty(field, getPropertyDescriptor( - AccessorTestClass.class, "yetYetAnotherId"), entity, typeHolder); + PersistentProperty property = getProperty(AccessorTestClass.class, "yetAnotherId", + getPropertyDescriptor(AccessorTestClass.class, "yetYetAnotherId")); assertThat(property.getGetter(), is(nullValue())); assertThat(property.getSetter(), is(notNullValue())); @@ -161,9 +142,7 @@ public class AbstractPersistentPropertyUnitTests { @Test // DATACMNS-206 public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() { - Field field = ReflectionUtils.findField(AccessorTestClass.class, "id"); - PersistentProperty property = new SamplePersistentProperty(field, null, entity, - typeHolder); + PersistentProperty property = getProperty(AccessorTestClass.class, "id", null); assertThat(property.getGetter(), is(nullValue())); assertThat(property.getSetter(), is(nullValue())); @@ -221,13 +200,25 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.isEntity(), is(false)); } + @Test // DATACMNS-1139 + public void resolvesGenericsForRawType() { + + SamplePersistentProperty property = getProperty(FirstConcrete.class, "genericField"); + + assertThat(property.getRawType(), is(typeCompatibleWith(String.class))); + } + private SamplePersistentProperty getProperty(Class type, String name) { + return getProperty(type, name, getPropertyDescriptor(type, name)); + } + + private SamplePersistentProperty getProperty(Class type, String name, PropertyDescriptor descriptor) { BasicPersistentEntity entity = new BasicPersistentEntity( ClassTypeInformation.from(type)); Field field = ReflectionUtils.findField(type, name); - return new SamplePersistentProperty(field, null, entity, typeHolder); + return new SamplePersistentProperty(field, descriptor, entity, typeHolder); } private static PropertyDescriptor getPropertyDescriptor(Class type, String propertyName) {