Browse Source

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.
pull/27/head
Oliver Gierke 13 years ago
parent
commit
8d69459f74
  1. 30
      src/main/java/org/springframework/data/util/TypeDiscoverer.java
  2. 5
      src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java

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

@ -198,9 +198,37 @@ class TypeDiscoverer<S> implements TypeInformation<S> { @@ -198,9 +198,37 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
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<Class<?>> superTypes = new ArrayList<Class<?>>();
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;
}
/**

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

@ -394,8 +394,11 @@ public class ClassTypeInformationUnitTests { @@ -394,8 +394,11 @@ public class ClassTypeInformationUnitTests {
Category getCategory();
}
interface Category {
interface Category extends Identifiable {
}
interface Identifiable {
Long getId();
}
}

Loading…
Cancel
Save