Browse Source

Consistently apply method ordering in DefaultProjectionInformation.

Fixes #2718.

$ Conflicts:
$	src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java
pull/2772/head
Oliver Drotbohm 3 years ago
parent
commit
0eeaa63eed
No known key found for this signature in database
GPG Key ID: C25FBFA0DA493A1D
  1. 20
      src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java

20
src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java

@ -29,7 +29,6 @@ import java.util.stream.Stream;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
import org.springframework.core.type.MethodMetadata; import org.springframework.core.type.MethodMetadata;
@ -171,7 +170,7 @@ class DefaultProjectionInformation implements ProjectionInformation {
Stream<PropertyDescriptor> allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) // Stream<PropertyDescriptor> allButDefaultGetters = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
.filter(it -> !hasDefaultGetter(it)); .filter(it -> !hasDefaultGetter(it));
Stream<PropertyDescriptor> ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it)) Stream<PropertyDescriptor> ownDescriptors = metadata.map(it -> filterAndOrder(allButDefaultGetters, it)) //
.orElse(allButDefaultGetters); .orElse(allButDefaultGetters);
Stream<PropertyDescriptor> superTypeDescriptors = metadata.map(this::fromMetadata) // Stream<PropertyDescriptor> superTypeDescriptors = metadata.map(this::fromMetadata) //
@ -189,18 +188,16 @@ class DefaultProjectionInformation implements ProjectionInformation {
* @param metadata must not be {@literal null}. * @param metadata must not be {@literal null}.
* @return * @return
*/ */
private static Stream<PropertyDescriptor> filterAndOrder(Stream<PropertyDescriptor> source, private Stream<PropertyDescriptor> filterAndOrder(Stream<PropertyDescriptor> source, MethodsMetadata metadata) {
MethodsMetadata metadata) {
Map<String, Integer> orderedMethods = getMethodOrder(metadata); Map<String, Integer> orderedMethods = getMethodOrder(metadata);
if (orderedMethods.isEmpty()) { Stream<PropertyDescriptor> filtered = source.filter(it -> it.getReadMethod() != null)
return source; .filter(it -> it.getReadMethod().getDeclaringClass().equals(type));
}
return source.filter(descriptor -> descriptor.getReadMethod() != null) return orderedMethods.isEmpty()
.filter(descriptor -> orderedMethods.containsKey(descriptor.getReadMethod().getName())) ? filtered
.sorted(Comparator.comparingInt(left -> orderedMethods.get(left.getReadMethod().getName()))); : filtered.sorted(Comparator.comparingInt(left -> orderedMethods.get(left.getReadMethod().getName())));
} }
/** /**
@ -240,7 +237,8 @@ class DefaultProjectionInformation implements ProjectionInformation {
} catch (IOException e) { } 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(); return Optional.empty();
} }
} }

Loading…
Cancel
Save