Browse Source

Polishing.

Reduce TypeDiscoverer.isNullableWrapper method visibility to private to avoid exposure in preparation for wider value type support.

Original pull request: #2394.
See #2390.
pull/2410/head
Mark Paluch 5 years ago
parent
commit
ab74551928
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 11
      src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java
  2. 32
      src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java
  3. 17
      src/main/java/org/springframework/data/util/TypeDiscoverer.java
  4. 9
      src/main/java/org/springframework/data/util/TypeInformation.java

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

@ -30,10 +30,10 @@ import java.util.concurrent.locks.Lock; @@ -30,10 +30,10 @@ import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
@ -576,9 +576,12 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<? @@ -576,9 +576,12 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
return;
}
StreamSupport.stream(property.getPersistentEntityTypes().spliterator(), false) //
.filter(AbstractMappingContext.this::shouldCreatePersistentEntityFor) //
.forEach(AbstractMappingContext.this::addPersistentEntity);
property.getPersistentEntityTypes().forEach(it -> {
if (shouldCreatePersistentEntityFor(it)) {
addPersistentEntity(it);
}
});
}
protected boolean shouldSkipOverrideProperty(P property) {

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

@ -21,17 +21,16 @@ import java.lang.reflect.Modifier; @@ -21,17 +21,16 @@ import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.core.GenericTypeResolver;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.NullableWrapperConverters;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
@ -94,18 +93,17 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -94,18 +93,17 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
this.usePropertyAccess = Lazy.of(() -> owner.getType().isInterface() || CAUSE_FIELD.equals(getField()));
this.isAssociation = Lazy.of(() -> ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(rawType));
this.associationTargetType = ASSOCIATION_TYPE == null
? Lazy.empty()
: Lazy.of(() -> Optional.of(getTypeInformation())
.map(it -> it.getSuperTypeInformation(ASSOCIATION_TYPE))
.map(TypeInformation::getComponentType)
this.associationTargetType = ASSOCIATION_TYPE == null //
? Lazy.empty() //
: Lazy.of(() -> Optional.of(getTypeInformation()) //
.map(it -> it.getSuperTypeInformation(ASSOCIATION_TYPE)) //
.map(TypeInformation::getComponentType) //
.orElse(null));
this.entityTypeInformation = Lazy.of(() -> Optional.ofNullable(getAssociationOrActualType())
.filter(it -> !simpleTypeHolder.isSimpleType(it.getType())) //
.filter(it -> !it.isCollectionLike()) //
.filter(it -> !it.isMap())
.orElse(null));
.filter(it -> !it.isMap()).orElse(null));
this.getter = property.getGetter().orElse(null);
this.setter = property.getSetter().orElse(null);
@ -121,32 +119,34 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -121,32 +119,34 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
this.entityTypes = Lazy.of(() -> collectEntityTypes(simpleTypeHolder, information, new LinkedHashSet<>()));
}
protected Set<TypeInformation<?>> collectEntityTypes(SimpleTypeHolder simpleTypeHolder, @Nullable TypeInformation<?> typeInformation, Set<TypeInformation<?>> entityTypes) {
protected Set<TypeInformation<?>> collectEntityTypes(SimpleTypeHolder simpleTypeHolder,
@Nullable TypeInformation<?> typeInformation, Set<TypeInformation<?>> entityTypes) {
if(typeInformation == null || entityTypes.contains(typeInformation) || simpleTypeHolder.isSimpleType(typeInformation.getType())) {
if (typeInformation == null || entityTypes.contains(typeInformation)
|| simpleTypeHolder.isSimpleType(typeInformation.getType())) {
return entityTypes;
}
if(typeInformation.isMap()) {
if (typeInformation.isMap()) {
collectEntityTypes(simpleTypeHolder, typeInformation.getComponentType(), entityTypes);
collectEntityTypes(simpleTypeHolder, typeInformation.getMapValueType(), entityTypes);
return entityTypes;
}
if(typeInformation.isCollectionLike()) {
if (typeInformation.isCollectionLike()) {
collectEntityTypes(simpleTypeHolder, typeInformation.getComponentType(), entityTypes);
return entityTypes;
}
if(typeInformation.isNullableWrapper()) {
if (NullableWrapperConverters.supports(typeInformation.getType())) {
collectEntityTypes(simpleTypeHolder, typeInformation.getActualType(), entityTypes);
return entityTypes;
}
if(ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(typeInformation.getType())) {
if (ASSOCIATION_TYPE != null && ASSOCIATION_TYPE.isAssignableFrom(typeInformation.getType())) {
entityTypes.add(getAssociationOrActualType());
return entityTypes;
@ -210,7 +210,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P> @@ -210,7 +210,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
@Override
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
if(isMap() || isCollectionLike()) {
if (isMap() || isCollectionLike()) {
return entityTypes.get();
}

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

@ -25,7 +25,16 @@ import java.lang.reflect.ParameterizedType; @@ -25,7 +25,16 @@ import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@ -309,6 +318,8 @@ class TypeDiscoverer<S> implements TypeInformation<S> { @@ -309,6 +318,8 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
return getComponentType();
}
// TODO: Consider that we will support value types beyond Optional<T>, such as Json<T>, Foo<T> that should remain
// configurable.
if (isNullableWrapper()) {
return getComponentType();
}
@ -529,6 +540,10 @@ class TypeDiscoverer<S> implements TypeInformation<S> { @@ -529,6 +540,10 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", type, candidates));
}
private boolean isNullableWrapper() {
return NullableWrapperConverters.supports(getType());
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)

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

@ -274,13 +274,4 @@ public interface TypeInformation<S> { @@ -274,13 +274,4 @@ public interface TypeInformation<S> {
return !type.equals(getType()) && type.isAssignableFrom(getType());
}
/**
* Returns whether the current type is considered a {@literal null} value wrapper or not.
*
* @return {@literal true} if the type is considered to be a {@literal null} value wrapper such as {@link java.util.Optional}.
*/
default boolean isNullableWrapper() {
return NullableWrapperConverters.supports(getType());
}
}

Loading…
Cancel
Save