Skip to content

Commit e9eb672

Browse files
committed
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.
1 parent 1725cba commit e9eb672

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public AbstractPersistentProperty(Field field, PropertyDescriptor propertyDescri
6262
Assert.notNull(owner, "Owner entity must not be null!");
6363

6464
this.name = field == null ? propertyDescriptor.getName() : field.getName();
65-
this.rawType = field == null ? propertyDescriptor.getPropertyType() : field.getType();
6665
this.information = owner.getTypeInformation().getProperty(this.name);
66+
this.rawType = this.information != null ? information.getType()
67+
: field == null ? propertyDescriptor.getPropertyType() : field.getType();
6768
this.propertyDescriptor = propertyDescriptor;
6869
this.field = field;
6970
this.association = isAssociation() ? createAssociation() : null;

src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

+28-37
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.springframework.data.mapping.PersistentProperty;
3737
import org.springframework.data.mapping.Person;
3838
import org.springframework.data.util.ClassTypeInformation;
39-
import org.springframework.data.util.TypeInformation;
4039
import org.springframework.util.ReflectionUtils;
4140

4241
/**
@@ -46,15 +45,10 @@
4645
*/
4746
public class AbstractPersistentPropertyUnitTests {
4847

49-
TypeInformation<TestClassComplex> typeInfo;
50-
PersistentEntity<TestClassComplex, SamplePersistentProperty> entity;
5148
SimpleTypeHolder typeHolder;
5249

5350
@Before
5451
public void setUp() {
55-
56-
typeInfo = ClassTypeInformation.from(TestClassComplex.class);
57-
entity = new BasicPersistentEntity<TestClassComplex, SamplePersistentProperty>(typeInfo);
5852
typeHolder = new SimpleTypeHolder();
5953
}
6054

@@ -64,9 +58,8 @@ public void setUp() {
6458
@Test
6559
public void discoversComponentTypeCorrectly() throws Exception {
6660

67-
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
61+
SamplePersistentProperty property = getProperty(TestClassComplex.class, "testClassSet");
6862

69-
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
7063
property.getComponentType();
7164
}
7265

@@ -76,9 +69,8 @@ public void discoversComponentTypeCorrectly() throws Exception {
7669
@Test
7770
public void returnsNestedEntityTypeCorrectly() {
7871

79-
Field field = ReflectionUtils.findField(TestClassComplex.class, "testClassSet");
72+
SamplePersistentProperty property = getProperty(TestClassComplex.class, "testClassSet", null);
8073

81-
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
8274
assertThat(property.getPersistentEntityType().iterator().hasNext(), is(false));
8375
}
8476

@@ -88,8 +80,8 @@ public void returnsNestedEntityTypeCorrectly() {
8880
@Test
8981
public void isEntityWorksForUntypedMaps() throws Exception {
9082

91-
Field field = ReflectionUtils.findField(TestClassComplex.class, "map");
92-
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
83+
SamplePersistentProperty property = getProperty(TestClassComplex.class, "map", null);
84+
9385
assertThat(property.isEntity(), is(false));
9486
}
9587

@@ -99,8 +91,8 @@ public void isEntityWorksForUntypedMaps() throws Exception {
9991
@Test
10092
public void isEntityWorksForUntypedCollection() throws Exception {
10193

102-
Field field = ReflectionUtils.findField(TestClassComplex.class, "collection");
103-
SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder);
94+
SamplePersistentProperty property = getProperty(TestClassComplex.class, "collection", null);
95+
10496
assertThat(property.isEntity(), is(false));
10597
}
10698

@@ -110,11 +102,8 @@ public void isEntityWorksForUntypedCollection() throws Exception {
110102
@Test
111103
public void considersPropertiesEqualIfFieldEquals() {
112104

113-
Field first = ReflectionUtils.findField(FirstConcrete.class, "genericField");
114-
Field second = ReflectionUtils.findField(SecondConcrete.class, "genericField");
115-
116-
SamplePersistentProperty firstProperty = new SamplePersistentProperty(first, null, entity, typeHolder);
117-
SamplePersistentProperty secondProperty = new SamplePersistentProperty(second, null, entity, typeHolder);
105+
SamplePersistentProperty firstProperty = getProperty(FirstConcrete.class, "genericField", null);
106+
SamplePersistentProperty secondProperty = getProperty(SecondConcrete.class, "genericField", null);
118107

119108
assertThat(firstProperty, is(secondProperty));
120109
assertThat(firstProperty.hashCode(), is(secondProperty.hashCode()));
@@ -126,9 +115,8 @@ public void considersPropertiesEqualIfFieldEquals() {
126115
@Test
127116
public void doesNotConsiderJavaTransientFieldsTransient() {
128117

129-
Field transientField = ReflectionUtils.findField(TestClassComplex.class, "transientField");
118+
PersistentProperty<?> property = getProperty(TestClassComplex.class, "transientField", null);
130119

131-
PersistentProperty<?> property = new SamplePersistentProperty(transientField, null, entity, typeHolder);
132120
assertThat(property.isTransient(), is(false));
133121
}
134122

@@ -138,9 +126,7 @@ public void doesNotConsiderJavaTransientFieldsTransient() {
138126
@Test
139127
public void findsSimpleGettersAndASetters() {
140128

141-
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
142-
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
143-
AccessorTestClass.class, "id"), entity, typeHolder);
129+
PersistentProperty<SamplePersistentProperty> property = getProperty(AccessorTestClass.class, "id");
144130

145131
assertThat(property.getGetter(), is(notNullValue()));
146132
assertThat(property.getSetter(), is(notNullValue()));
@@ -152,9 +138,7 @@ public void findsSimpleGettersAndASetters() {
152138
@Test
153139
public void doesNotUseInvalidGettersAndASetters() {
154140

155-
Field field = ReflectionUtils.findField(AccessorTestClass.class, "anotherId");
156-
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
157-
AccessorTestClass.class, "anotherId"), entity, typeHolder);
141+
PersistentProperty<SamplePersistentProperty> property = getProperty(AccessorTestClass.class, "anotherId");
158142

159143
assertThat(property.getGetter(), is(nullValue()));
160144
assertThat(property.getSetter(), is(nullValue()));
@@ -166,9 +150,7 @@ public void doesNotUseInvalidGettersAndASetters() {
166150
@Test
167151
public void usesCustomGetter() {
168152

169-
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetAnotherId");
170-
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
171-
AccessorTestClass.class, "yetAnotherId"), entity, typeHolder);
153+
PersistentProperty<SamplePersistentProperty> property = getProperty(AccessorTestClass.class, "yetAnotherId");
172154

173155
assertThat(property.getGetter(), is(notNullValue()));
174156
assertThat(property.getSetter(), is(nullValue()));
@@ -180,9 +162,8 @@ public void usesCustomGetter() {
180162
@Test
181163
public void usesCustomSetter() {
182164

183-
Field field = ReflectionUtils.findField(AccessorTestClass.class, "yetYetAnotherId");
184-
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, getPropertyDescriptor(
185-
AccessorTestClass.class, "yetYetAnotherId"), entity, typeHolder);
165+
PersistentProperty<SamplePersistentProperty> property = getProperty(AccessorTestClass.class, "yetAnotherId",
166+
getPropertyDescriptor(AccessorTestClass.class, "yetYetAnotherId"));
186167

187168
assertThat(property.getGetter(), is(nullValue()));
188169
assertThat(property.getSetter(), is(notNullValue()));
@@ -194,9 +175,7 @@ public void usesCustomSetter() {
194175
@Test
195176
public void returnsNullGetterAndSetterIfNoPropertyDescriptorGiven() {
196177

197-
Field field = ReflectionUtils.findField(AccessorTestClass.class, "id");
198-
PersistentProperty<SamplePersistentProperty> property = new SamplePersistentProperty(field, null, entity,
199-
typeHolder);
178+
PersistentProperty<SamplePersistentProperty> property = getProperty(AccessorTestClass.class, "id", null);
200179

201180
assertThat(property.getGetter(), is(nullValue()));
202181
assertThat(property.getSetter(), is(nullValue()));
@@ -272,13 +251,25 @@ public void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
272251
assertThat(property.isEntity(), is(false));
273252
}
274253

254+
@Test // DATACMNS-1139
255+
public void resolvesGenericsForRawType() {
256+
257+
SamplePersistentProperty property = getProperty(FirstConcrete.class, "genericField");
258+
259+
assertThat(property.getRawType(), is(typeCompatibleWith(String.class)));
260+
}
261+
275262
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
263+
return getProperty(type, name, getPropertyDescriptor(type, name));
264+
}
265+
266+
private <T> SamplePersistentProperty getProperty(Class<T> type, String name, PropertyDescriptor descriptor) {
276267

277268
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
278269
ClassTypeInformation.from(type));
279270

280271
Field field = ReflectionUtils.findField(type, name);
281-
return new SamplePersistentProperty(field, null, entity, typeHolder);
272+
return new SamplePersistentProperty(field, descriptor, entity, typeHolder);
282273
}
283274

284275
private static PropertyDescriptor getPropertyDescriptor(Class<?> type, String propertyName) {

0 commit comments

Comments
 (0)