Skip to content

Commit bb280bd

Browse files
committed
DATAMONGO-2200 - Polishing.
Tweak Javadoc. Simplify Fields creation from Stream. Remove final modifier from private static method. Iterate with loop over PersistentEntity. Original pull request: #748.
1 parent e24c5e0 commit bb280bd

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationContext.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
import java.beans.PropertyDescriptor;
1919
import java.lang.reflect.Method;
2020
import java.util.Arrays;
21-
import java.util.List;
22-
import java.util.stream.Collectors;
2321

2422
import org.bson.Document;
23+
2524
import org.springframework.beans.BeanUtils;
2625
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
2726
import org.springframework.lang.Nullable;
@@ -77,17 +76,20 @@ default Document getMappedObject(Document document) {
7776
FieldReference getReference(String name);
7877

7978
/**
80-
* Returns the {@link Fields} exposed by the type. May be a {@literal class} or an {@literal interface}.
79+
* Returns the {@link Fields} exposed by the type. May be a {@literal class} or an {@literal interface}. The default
80+
* implementation uses {@link BeanUtils#getPropertyDescriptors(Class) property descriptors} discover fields from a
81+
* {@link Class}.
8182
*
8283
* @param type must not be {@literal null}.
8384
* @return never {@literal null}.
8485
* @since 2.2
86+
* @see BeanUtils#getPropertyDescriptor(Class, String)
8587
*/
8688
default Fields getFields(Class<?> type) {
8789

8890
Assert.notNull(type, "Type must not be null!");
8991

90-
List<String> fields = Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
92+
return Fields.fields(Arrays.stream(BeanUtils.getPropertyDescriptors(type)) //
9193
.filter(it -> { // object and default methods
9294
Method method = it.getReadMethod();
9395
if (method == null) {
@@ -99,8 +101,6 @@ default Fields getFields(Class<?> type) {
99101
return !method.isDefault();
100102
}) //
101103
.map(PropertyDescriptor::getName) //
102-
.collect(Collectors.toList());
103-
104-
return Fields.fields(fields.toArray(new String[0]));
104+
.toArray(String[]::new));
105105
}
106106
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private Fields(List<Field> fields) {
112112
this.fields = verify(fields);
113113
}
114114

115-
private static final List<Field> verify(List<Field> fields) {
115+
private static List<Field> verify(List<Field> fields) {
116116

117117
Map<String, Field> reference = new HashMap<String, Field>();
118118

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323

2424
import org.bson.Document;
25+
2526
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond;
2627
import org.springframework.data.mongodb.core.aggregation.ConditionalOperators.IfNull;
2728
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -30,7 +31,6 @@
3031
import org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable;
3132
import org.springframework.lang.Nullable;
3233
import org.springframework.util.Assert;
33-
import org.springframework.util.ReflectionUtils;
3434

3535
/**
3636
* Encapsulates the aggregation framework {@code $project}-operation.
@@ -1739,7 +1739,7 @@ public Document toDocument(AggregationOperationContext context) {
17391739
Document projections = new Document();
17401740

17411741
Fields fields = context.getFields(type);
1742-
fields.asList().forEach(it -> projections.append(it.getName(), 1));
1742+
fields.forEach(it -> projections.append(it.getName(), 1));
17431743
return context.getMappedObject(projections, type);
17441744
}
17451745
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContext.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.List;
2222

2323
import org.bson.Document;
24+
2425
import org.springframework.data.mapping.PersistentPropertyPath;
25-
import org.springframework.data.mapping.SimplePropertyHandler;
2626
import org.springframework.data.mapping.context.MappingContext;
2727
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
2828
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -111,14 +111,21 @@ public FieldReference getReference(String name) {
111111
@Override
112112
public Fields getFields(Class<?> type) {
113113

114+
Assert.notNull(type, "Type must not be null!");
115+
114116
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
117+
115118
if (entity == null) {
116119
return AggregationOperationContext.super.getFields(type);
117120
}
118121

119122
List<String> fields = new ArrayList<>();
120-
entity.doWithProperties((SimplePropertyHandler) it -> fields.add(it.getName()));
121-
return Fields.fields(fields.toArray(new String[fields.size()]));
123+
124+
for (MongoPersistentProperty property : entity) {
125+
fields.add(property.getName());
126+
}
127+
128+
return Fields.fields(fields.toArray(new String[0]));
122129
}
123130

124131
private FieldReference getReferenceFor(Field field) {

0 commit comments

Comments
 (0)