From 8d69459f740bec41aee76a685d47b8e5a13a5abc Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 12 Apr 2013 16:41:39 +0200 Subject: [PATCH] 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. --- .../data/util/TypeDiscoverer.java | 30 ++++++++++++++++++- .../util/ClassTypeInformationUnitTests.java | 5 +++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index 0ee9e0b2d..2a8e59c72 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -198,9 +198,37 @@ class TypeDiscoverer implements TypeInformation { return createInfo(field.getGenericType()); } + PropertyDescriptor descriptor = findPropertyDescriptor(type, fieldname); + return descriptor == null ? null : createInfo(getGenericType(descriptor)); + } + + /** + * Finds the {@link PropertyDescriptor} for the property with the given name on the given type. + * + * @param type must not be {@literal null}. + * @param fieldname must not be {@literal null} or empty. + * @return + */ + private static PropertyDescriptor findPropertyDescriptor(Class type, String fieldname) { + PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(type, fieldname); - return descriptor == null ? null : createInfo(getGenericType(descriptor)); + if (descriptor != null) { + return descriptor; + } + + List> superTypes = new ArrayList>(); + superTypes.addAll(Arrays.asList(type.getInterfaces())); + superTypes.add(type.getSuperclass()); + + for (Class interfaceType : type.getInterfaces()) { + descriptor = findPropertyDescriptor(interfaceType, fieldname); + if (descriptor != null) { + return descriptor; + } + } + + return null; } /** diff --git a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index 032d66162..139941a26 100644 --- a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -394,8 +394,11 @@ public class ClassTypeInformationUnitTests { Category getCategory(); } - interface Category { + interface Category extends Identifiable { + } + + interface Identifiable { Long getId(); } }