Skip to content

Commit 8d69459

Browse files
committed
DATACMNS-311 - TypeDiscoverer now also finds properties in type hierarchy.
We now also inspect the entire type hierarchy to lookup a property descriptor to inspect for read methods in case no field is found for a property.
1 parent 59af577 commit 8d69459

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/main/java/org/springframework/data/util/TypeDiscoverer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,37 @@ private TypeInformation<?> getPropertyInformation(String fieldname) {
198198
return createInfo(field.getGenericType());
199199
}
200200

201+
PropertyDescriptor descriptor = findPropertyDescriptor(type, fieldname);
202+
return descriptor == null ? null : createInfo(getGenericType(descriptor));
203+
}
204+
205+
/**
206+
* Finds the {@link PropertyDescriptor} for the property with the given name on the given type.
207+
*
208+
* @param type must not be {@literal null}.
209+
* @param fieldname must not be {@literal null} or empty.
210+
* @return
211+
*/
212+
private static PropertyDescriptor findPropertyDescriptor(Class<?> type, String fieldname) {
213+
201214
PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(type, fieldname);
202215

203-
return descriptor == null ? null : createInfo(getGenericType(descriptor));
216+
if (descriptor != null) {
217+
return descriptor;
218+
}
219+
220+
List<Class<?>> superTypes = new ArrayList<Class<?>>();
221+
superTypes.addAll(Arrays.asList(type.getInterfaces()));
222+
superTypes.add(type.getSuperclass());
223+
224+
for (Class<?> interfaceType : type.getInterfaces()) {
225+
descriptor = findPropertyDescriptor(interfaceType, fieldname);
226+
if (descriptor != null) {
227+
return descriptor;
228+
}
229+
}
230+
231+
return null;
204232
}
205233

206234
/**

src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,11 @@ interface Product {
394394
Category getCategory();
395395
}
396396

397-
interface Category {
397+
interface Category extends Identifiable {
398398

399+
}
400+
401+
interface Identifiable {
399402
Long getId();
400403
}
401404
}

0 commit comments

Comments
 (0)