Consistently apply method ordering in DefaultProjectionInformation.

Fixes #2718.
This commit is contained in:
Oliver Drotbohm
2022-10-17 14:25:32 +02:00
parent 1d1566acd3
commit 39e79a144e

View File

@@ -29,7 +29,6 @@ import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.core.log.LogMessage;
import org.springframework.core.type.AnnotationMetadata;
@@ -159,7 +158,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
Stream<PropertyDescriptor> allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
.filter(it -> !hasDefaultGetter(it));
Stream<PropertyDescriptor> ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it))
Stream<PropertyDescriptor> ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it)) //
.orElse(allButDefaultGetters);
Stream<PropertyDescriptor> superTypeDescriptors = metadata.map(this::fromMetadata) //
@@ -177,18 +176,17 @@ class DefaultProjectionInformation implements ProjectionInformation {
* @param metadata must not be {@literal null}.
* @return
*/
private static Stream<PropertyDescriptor> filterAndOrder(Stream<PropertyDescriptor> source,
private Stream<PropertyDescriptor> filterAndOrder(Stream<PropertyDescriptor> source,
AnnotationMetadata metadata) {
Map<String, Integer> orderedMethods = getMethodOrder(metadata);
if (orderedMethods.isEmpty()) {
return source;
}
Stream<PropertyDescriptor> filtered = source.filter(it -> it.getReadMethod() != null)
.filter(it -> it.getReadMethod().getDeclaringClass().equals(type));
return source.filter(descriptor -> descriptor.getReadMethod() != null)
.filter(descriptor -> orderedMethods.containsKey(descriptor.getReadMethod().getName()))
.sorted(Comparator.comparingInt(left -> orderedMethods.get(left.getReadMethod().getName())));
return orderedMethods.isEmpty()
? filtered
: filtered.sorted(Comparator.comparingInt(left -> orderedMethods.get(left.getReadMethod().getName())));
}
/**
@@ -228,7 +226,8 @@ class DefaultProjectionInformation implements ProjectionInformation {
} catch (IOException e) {
logger.info(LogMessage.format("Couldn't read class metadata for %s. Input property calculation might fail", type));
logger.info(
LogMessage.format("Couldn't read class metadata for %s. Input property calculation might fail", type));
return Optional.empty();
}
}