DATACMNS-867 - Fixed glitch causing failure in PropertyDescriptor lookup for ProjectionInformation.

Re added PropertyDescriptor collection. Looks like the method got dropped during one of the rebase operations.
Additionally make use of .distinct() name collection in tests as descriptors might appear multiple times for the same property name.
This commit is contained in:
Christoph Strobl
2017-01-30 15:27:14 +01:00
committed by Oliver Gierke
parent cc1c07ecca
commit 8bcd8cb3da
3 changed files with 41 additions and 18 deletions

View File

@@ -20,15 +20,17 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.util.Assert;
/**
* Default implementation of {@link ProjectionInformation}. Exposes all properties of the type as required input
* properties.
*
*
* @author Oliver Gierke
* @since 1.12
*/
@@ -39,7 +41,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
/**
* Creates a new {@link DefaultProjectionInformation} for the given type.
*
*
* @param type must not be {@literal null}.
*/
public DefaultProjectionInformation(Class<?> type) {
@@ -47,10 +49,10 @@ class DefaultProjectionInformation implements ProjectionInformation {
Assert.notNull(type, "Projection type must not be null!");
this.projectionType = type;
this.properties = Arrays.asList(BeanUtils.getPropertyDescriptors(type));
this.properties = collectDescriptors(type);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.projection.ProjectionInformation#getType()
*/
@@ -71,7 +73,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
.collect(Collectors.toList());
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.projection.ProjectionInformation#isDynamic()
*/
@@ -84,7 +86,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
* Returns whether the given {@link PropertyDescriptor} describes an input property for the projection, i.e. a
* property that needs to be present on the source to be able to create reasonable projections for the type the
* descriptor was looked up on.
*
*
* @param descriptor will never be {@literal null}.
* @return
*/
@@ -92,6 +94,26 @@ class DefaultProjectionInformation implements ProjectionInformation {
return true;
}
/**
* Collects {@link PropertyDescriptor}s for all properties exposed by the given type and all its super interfaces.
*
* @param type must not be {@literal null}.
* @return
*/
private static List<PropertyDescriptor> collectDescriptors(Class<?> type) {
List<PropertyDescriptor> result = new ArrayList<PropertyDescriptor>();
result.addAll(Arrays.stream(BeanUtils.getPropertyDescriptors(type))//
.filter(it -> !hasDefaultGetter(it))//
.collect(Collectors.toList()));
for (Class<?> interfaze : type.getInterfaces()) {
result.addAll(collectDescriptors(interfaze));
}
return result.stream().distinct().collect(Collectors.toList());
}
/**
* Returns whether the given {@link PropertyDescriptor} has a getter that is a Java 8 default method.
*