Skip to content

Commit ab74551

Browse files
committed
Polishing.
Reduce TypeDiscoverer.isNullableWrapper method visibility to private to avoid exposure in preparation for wider value type support. Original pull request: #2394. See #2390.
1 parent 3277467 commit ab74551

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import java.util.concurrent.locks.ReentrantReadWriteLock;
3131
import java.util.function.Predicate;
3232
import java.util.stream.Collectors;
33-
import java.util.stream.StreamSupport;
3433

3534
import org.slf4j.Logger;
3635
import org.slf4j.LoggerFactory;
36+
3737
import org.springframework.beans.BeanUtils;
3838
import org.springframework.beans.BeansException;
3939
import org.springframework.beans.factory.InitializingBean;
@@ -576,9 +576,12 @@ private void createAndRegisterProperty(Property input) {
576576
return;
577577
}
578578

579-
StreamSupport.stream(property.getPersistentEntityTypes().spliterator(), false) //
580-
.filter(AbstractMappingContext.this::shouldCreatePersistentEntityFor) //
581-
.forEach(AbstractMappingContext.this::addPersistentEntity);
579+
property.getPersistentEntityTypes().forEach(it -> {
580+
581+
if (shouldCreatePersistentEntityFor(it)) {
582+
addPersistentEntity(it);
583+
}
584+
});
582585
}
583586

584587
protected boolean shouldSkipOverrideProperty(P property) {

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.LinkedHashSet;
24-
import java.util.List;
2524
import java.util.Map;
2625
import java.util.Optional;
2726
import java.util.Set;
2827
import java.util.function.Supplier;
2928

30-
import org.springframework.core.GenericTypeResolver;
3129
import org.springframework.data.mapping.Association;
3230
import org.springframework.data.mapping.PersistentEntity;
3331
import org.springframework.data.mapping.PersistentProperty;
3432
import org.springframework.data.util.Lazy;
33+
import org.springframework.data.util.NullableWrapperConverters;
3534
import org.springframework.data.util.ReflectionUtils;
3635
import org.springframework.data.util.TypeInformation;
3736
import org.springframework.lang.Nullable;
@@ -94,18 +93,17 @@ public AbstractPersistentProperty(Property property, PersistentEntity<?, P> owne
9493
this.usePropertyAccess = Lazy.of(() -> owner.getType().isInterface() || CAUSE_FIELD.equals(getField()));
9594

9695
this.isAssociation = Lazy.of(() -> ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(rawType));
97-
this.associationTargetType = ASSOCIATION_TYPE == null
98-
? Lazy.empty()
99-
: Lazy.of(() -> Optional.of(getTypeInformation())
100-
.map(it -> it.getSuperTypeInformation(ASSOCIATION_TYPE))
101-
.map(TypeInformation::getComponentType)
96+
this.associationTargetType = ASSOCIATION_TYPE == null //
97+
? Lazy.empty() //
98+
: Lazy.of(() -> Optional.of(getTypeInformation()) //
99+
.map(it -> it.getSuperTypeInformation(ASSOCIATION_TYPE)) //
100+
.map(TypeInformation::getComponentType) //
102101
.orElse(null));
103102

104103
this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(getAssociationOrActualType())
105104
.filter(it -> !simpleTypeHolder.isSimpleType(it.getType())) //
106105
.filter(it -> !it.isCollectionLike()) //
107-
.filter(it -> !it.isMap())
108-
.orElse(null));
106+
.filter(it -> !it.isMap()).orElse(null));
109107

110108
this.getter = property.getGetter().orElse(null);
111109
this.setter = property.getSetter().orElse(null);
@@ -121,32 +119,34 @@ public AbstractPersistentProperty(Property property, PersistentEntity<?, P> owne
121119
this.entityTypes = Lazy.of(() -> collectEntityTypes(simpleTypeHolder, information, new LinkedHashSet<>()));
122120
}
123121

124-
protected Set<TypeInformation<?>> collectEntityTypes(SimpleTypeHolder simpleTypeHolder, @Nullable TypeInformation<?> typeInformation, Set<TypeInformation<?>> entityTypes) {
122+
protected Set<TypeInformation<?>> collectEntityTypes(SimpleTypeHolder simpleTypeHolder,
123+
@Nullable TypeInformation<?> typeInformation, Set<TypeInformation<?>> entityTypes) {
125124

126-
if(typeInformation == null || entityTypes.contains(typeInformation) || simpleTypeHolder.isSimpleType(typeInformation.getType())) {
125+
if (typeInformation == null || entityTypes.contains(typeInformation)
126+
|| simpleTypeHolder.isSimpleType(typeInformation.getType())) {
127127
return entityTypes;
128128
}
129129

130-
if(typeInformation.isMap()) {
130+
if (typeInformation.isMap()) {
131131

132132
collectEntityTypes(simpleTypeHolder, typeInformation.getComponentType(), entityTypes);
133133
collectEntityTypes(simpleTypeHolder, typeInformation.getMapValueType(), entityTypes);
134134
return entityTypes;
135135
}
136136

137-
if(typeInformation.isCollectionLike()) {
137+
if (typeInformation.isCollectionLike()) {
138138

139139
collectEntityTypes(simpleTypeHolder, typeInformation.getComponentType(), entityTypes);
140140
return entityTypes;
141141
}
142142

143-
if(typeInformation.isNullableWrapper()) {
143+
if (NullableWrapperConverters.supports(typeInformation.getType())) {
144144

145145
collectEntityTypes(simpleTypeHolder, typeInformation.getActualType(), entityTypes);
146146
return entityTypes;
147147
}
148148

149-
if(ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(typeInformation.getType())) {
149+
if (ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(typeInformation.getType())) {
150150

151151
entityTypes.add(getAssociationOrActualType());
152152
return entityTypes;
@@ -210,7 +210,7 @@ public TypeInformation<?> getTypeInformation() {
210210
@Override
211211
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
212212

213-
if(isMap() || isCollectionLike()) {
213+
if (isMap() || isCollectionLike()) {
214214
return entityTypes.get();
215215
}
216216

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@
2525
import java.lang.reflect.Type;
2626
import java.lang.reflect.TypeVariable;
2727
import java.lang.reflect.WildcardType;
28-
import java.util.*;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Collection;
31+
import java.util.Collections;
32+
import java.util.HashMap;
33+
import java.util.HashSet;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Optional;
37+
import java.util.Set;
2938
import java.util.concurrent.ConcurrentHashMap;
3039
import java.util.stream.Collectors;
3140

@@ -309,6 +318,8 @@ public TypeInformation<?> getActualType() {
309318
return getComponentType();
310319
}
311320

321+
// TODO: Consider that we will support value types beyond Optional<T>, such as Json<T>, Foo<T> that should remain
322+
// configurable.
312323
if (isNullableWrapper()) {
313324
return getComponentType();
314325
}
@@ -529,6 +540,10 @@ private Class<?> getBaseType(Class<?>[] candidates) {
529540
throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", type, candidates));
530541
}
531542

543+
private boolean isNullableWrapper() {
544+
return NullableWrapperConverters.supports(getType());
545+
}
546+
532547
/*
533548
* (non-Javadoc)
534549
* @see java.lang.Object#equals(java.lang.Object)

src/main/java/org/springframework/data/util/TypeInformation.java

-9
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,4 @@ default boolean isSubTypeOf(Class<?> type) {
274274
return !type.equals(getType()) && type.isAssignableFrom(getType());
275275
}
276276

277-
/**
278-
* Returns whether the current type is considered a {@literal null} value wrapper or not.
279-
*
280-
* @return {@literal true} if the type is considered to be a {@literal null} value wrapper such as {@link java.util.Optional}.
281-
*/
282-
default boolean isNullableWrapper() {
283-
return NullableWrapperConverters.supports(getType());
284-
}
285-
286277
}

0 commit comments

Comments
 (0)