DATACMNS-1101 - Remove Optional from TypeDiscoverer API.

This commit is contained in:
Mark Paluch
2017-06-29 13:11:10 +02:00
committed by Oliver Gierke
parent 2064c72a85
commit 0493d131e0
13 changed files with 312 additions and 329 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,11 +20,10 @@ import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Map;
import java.util.Optional;
/**
* Special {@link TypeDiscoverer} handling {@link GenericArrayType}s.
*
*
* @author Oliver Gierke
*/
class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
@@ -34,7 +33,7 @@ class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
/**
* Creates a new {@link GenericArrayTypeInformation} for the given {@link GenericArrayTypeInformation} and
* {@link TypeDiscoverer}.
*
*
* @param type must not be {@literal null}.
* @param parent must not be {@literal null}.
* @param typeVariableMap must not be {@literal null}.
@@ -61,13 +60,13 @@ class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
* @see org.springframework.data.util.TypeDiscoverer#doGetComponentType()
*/
@Override
protected Optional<TypeInformation<?>> doGetComponentType() {
protected TypeInformation<?> doGetComponentType() {
Type componentType = type.getGenericComponentType();
return Optional.of(createInfo(componentType));
return createInfo(componentType);
}
/*
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/

View File

@@ -31,7 +31,7 @@ import org.springframework.util.StringUtils;
/**
* Base class for all types that include parameterization of some kind. Crucial as we have to take note of the parent
* class we will have to resolve generic parameters against.
*
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
@@ -43,7 +43,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
/**
* Creates a new {@link ParameterizedTypeInformation} for the given {@link Type} and parent {@link TypeDiscoverer}.
*
*
* @param type must not be {@literal null}
* @param parent must not be {@literal null}
*/
@@ -59,14 +59,14 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
* @see org.springframework.data.util.TypeDiscoverer#doGetMapValueType()
*/
@Override
protected Optional<TypeInformation<?>> doGetMapValueType() {
protected TypeInformation<?> doGetMapValueType() {
if (Map.class.isAssignableFrom(getType())) {
Type[] arguments = type.getActualTypeArguments();
if (arguments.length > 1) {
return Optional.of(createInfo(arguments[1]));
return createInfo(arguments[1]);
}
}
@@ -79,14 +79,14 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
Optional<TypeInformation<?>> result = supertypes.stream()//
.map(it -> Pair.of(it, resolveType(it)))//
.filter(it -> Map.class.isAssignableFrom(it.getSecond()))//
.<TypeInformation<?>>map(it -> {
.<TypeInformation<?>> map(it -> {
ParameterizedType parameterizedSupertype = (ParameterizedType) it.getFirst();
Type[] arguments = parameterizedSupertype.getActualTypeArguments();
return createInfo(arguments[1]);
}).findFirst();
return result.isPresent() ? result : super.doGetMapValueType();
return result.orElseGet(super::doGetMapValueType);
}
/*
@@ -105,7 +105,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
return result;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.util.TypeDiscoverer#isAssignableFrom(org.springframework.data.util.TypeInformation)
*/
@@ -147,11 +147,11 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
* @see org.springframework.data.util.TypeDiscoverer#doGetComponentType()
*/
@Override
protected Optional<TypeInformation<?>> doGetComponentType() {
return Optional.of(createInfo(type.getActualTypeArguments()[0]));
protected TypeInformation<?> doGetComponentType() {
return createInfo(type.getActualTypeArguments()[0]);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.util.ParentTypeAwareTypeInformation#equals(java.lang.Object)
*/
@@ -175,7 +175,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
return super.equals(obj);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.data.util.ParentTypeAwareTypeInformation#hashCode()
*/
@@ -184,7 +184,7 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
return isResolvedCompletely() ? this.type.hashCode() : super.hashCode();
}
/*
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/

View File

@@ -28,7 +28,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;
@@ -43,6 +52,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
*/
class TypeDiscoverer<S> implements TypeInformation<S> {
@@ -68,8 +78,8 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
private final int hashCode;
private final Lazy<Class<S>> resolvedType;
private final Lazy<Optional<TypeInformation<?>>> componentType;
private final Lazy<Optional<TypeInformation<?>>> valueType;
private final Lazy<TypeInformation<?>> componentType;
private final Lazy<TypeInformation<?>> valueType;
/**
* Creates a new {@link TypeDiscoverer} for the given type, type variable map and parent.
@@ -99,19 +109,17 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
return typeVariableMap;
}
private TypeInformation<?> createInfo(Optional<Type> fieldType) {
return fieldType.map(this::createInfo).orElseThrow(IllegalArgumentException::new);
}
/**
* Creates {@link TypeInformation} for the given {@link Type}.
*
* @param fieldType
* @param fieldType must not be {@literal null}.
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
protected TypeInformation<?> createInfo(Type fieldType) {
Assert.notNull(fieldType, "Field type must not be null!");
if (fieldType.equals(this.type)) {
return this;
}
@@ -203,18 +211,22 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
* (non-Javadoc)
* @see org.springframework.data.util.TypeInformation#getProperty(java.lang.String)
*/
public Optional<TypeInformation<?>> getProperty(String fieldname) {
public TypeInformation<?> getProperty(String fieldname) {
int separatorIndex = fieldname.indexOf('.');
if (separatorIndex == -1) {
return fieldTypes.computeIfAbsent(fieldname, this::getPropertyInformation);
return fieldTypes.computeIfAbsent(fieldname, this::getPropertyInformation).orElse(null);
}
String head = fieldname.substring(0, separatorIndex);
Optional<TypeInformation<?>> info = getProperty(head);
TypeInformation<?> info = getProperty(head);
return info.map(it -> it.getProperty(fieldname.substring(separatorIndex + 1))).orElseGet(Optional::empty);
if (info == null) {
return null;
}
return info.getProperty(fieldname.substring(separatorIndex + 1));
}
/**
@@ -235,7 +247,6 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
}
return findPropertyDescriptor(rawType, fieldname).map(it -> createInfo(getGenericType(it)));
}
/**
@@ -269,22 +280,22 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
* @param descriptor must not be {@literal null}
* @return
*/
private static Optional<Type> getGenericType(PropertyDescriptor descriptor) {
private static Type getGenericType(PropertyDescriptor descriptor) {
Method method = descriptor.getReadMethod();
if (method != null) {
return Optional.of(method.getGenericReturnType());
return method.getGenericReturnType();
}
method = descriptor.getWriteMethod();
if (method == null) {
return Optional.empty();
return null;
}
Type[] parameterTypes = method.getGenericParameterTypes();
return Optional.ofNullable(parameterTypes.length == 0 ? null : parameterTypes[0]);
return parameterTypes.length == 0 ? null : parameterTypes[0];
}
/*
@@ -311,11 +322,11 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
public TypeInformation<?> getActualType() {
if (isMap()) {
return getMapValueType().orElse(null);
return getMapValueType();
}
if (isCollectionLike()) {
return getComponentType().orElse(null);
return getComponentType();
}
return this;
@@ -342,12 +353,13 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
* (non-Javadoc)
* @see org.springframework.data.util.TypeInformation#getMapValueType()
*/
public Optional<TypeInformation<?>> getMapValueType() {
public TypeInformation<?> getMapValueType() {
return valueType.get();
}
protected Optional<TypeInformation<?>> doGetMapValueType() {
return isMap() ? getTypeArgument(getBaseType(MAP_TYPES), 1) : getTypeArguments().stream().skip(1).findFirst();
protected TypeInformation<?> doGetMapValueType() {
return isMap() ? getTypeArgument(getBaseType(MAP_TYPES), 1)
: getTypeArguments().stream().skip(1).findFirst().orElse(null);
}
/*
@@ -365,16 +377,16 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
* (non-Javadoc)
* @see org.springframework.data.util.TypeInformation#getComponentType()
*/
public final Optional<TypeInformation<?>> getComponentType() {
public final TypeInformation<?> getComponentType() {
return componentType.get();
}
protected Optional<TypeInformation<?>> doGetComponentType() {
protected TypeInformation<?> doGetComponentType() {
Class<S> rawType = getType();
if (rawType.isArray()) {
return Optional.of(createInfo(rawType.getComponentType()));
return createInfo(rawType.getComponentType());
}
if (isMap()) {
@@ -387,7 +399,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
List<TypeInformation<?>> arguments = getTypeArguments();
return arguments.size() > 0 ? Optional.of(arguments.get(0)) : Optional.empty();
return arguments.size() > 0 ? arguments.get(0) : null;
}
/*
@@ -486,16 +498,16 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
: createInfo(new SyntheticParamterizedType(type, arguments)));
}
private Optional<TypeInformation<?>> getTypeArgument(Class<?> bound, int index) {
private TypeInformation<?> getTypeArgument(Class<?> bound, int index) {
Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(getType(), bound);
if (arguments == null) {
return Optional.ofNullable(
getSuperTypeInformation(bound) instanceof ParameterizedTypeInformation ? ClassTypeInformation.OBJECT : null);
return getSuperTypeInformation(bound) instanceof ParameterizedTypeInformation ? ClassTypeInformation.OBJECT
: null;
}
return Optional.of(createInfo(arguments[index]));
return createInfo(arguments[index]);
}
private Class<?> getBaseType(Class<?>[] candidates) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,19 +18,19 @@ package org.springframework.data.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
/**
* Interface to access property types and resolving generics on the way. Starting with a {@link ClassTypeInformation}
* you can traverse properties using {@link #getProperty(String)} to access type information.
*
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public interface TypeInformation<S> {
/**
* Returns the {@link TypeInformation}s for the parameters of the given {@link Constructor}.
*
*
* @param constructor must not be {@literal null}.
* @return
*/
@@ -39,69 +39,106 @@ public interface TypeInformation<S> {
/**
* Returns the property information for the property with the given name. Supports property traversal through dot
* notation.
*
*
* @param property
* @return
*/
Optional<TypeInformation<?>> getProperty(String property);
TypeInformation<?> getProperty(String property);
/**
* Returns the property information for the property with the given name or throw {@link IllegalArgumentException} if
* the type information cannot be resolved. Supports property traversal through dot notation.
*
* @param property
* @return
* @throws IllegalArgumentException if the type information cannot be resolved.
* @since 2.0
*/
default TypeInformation<?> getRequiredProperty(String property) {
return getProperty(property).orElseThrow(() -> new IllegalArgumentException(
String.format("Could not find required property %s on %s!", property, getType())));
TypeInformation<?> typeInformation = getProperty(property);
if (typeInformation != null) {
return typeInformation;
}
throw new IllegalArgumentException(
String.format("Could not find required property %s on %s!", property, getType()));
}
/**
* Returns whether the type can be considered a collection, which means it's a container of elements, e.g. a
* {@link java.util.Collection} and {@link java.lang.reflect.Array} or anything implementing {@link Iterable}. If this
* returns {@literal true} you can expect {@link #getComponentType()} to return a non-{@literal null} value.
*
*
* @return
*/
boolean isCollectionLike();
/**
* Returns the component type for {@link java.util.Collection}s or the key type for {@link java.util.Map}s.
*
*
* @return
*/
Optional<TypeInformation<?>> getComponentType();
TypeInformation<?> getComponentType();
/**
* Returns the component type for {@link java.util.Collection}s, the key type for {@link java.util.Map}s or the single
* generic type if available throwing an exception if it can't be resolved.
*
* generic type if available. Throws {@link IllegalStateException} if the component value type cannot be resolved.
*
* @return
* @throws IllegalStateException if the component type cannot be resolved, e.g. if a raw type is used or the type is
* not generic in the first place.
* @since 2.0
*/
default TypeInformation<?> getRequiredComponentType() {
return getComponentType().orElseThrow(
() -> new IllegalStateException(String.format("Can't resolve required component type for %s!", getType())));
TypeInformation<?> componentType = getComponentType();
if (componentType != null) {
return getComponentType();
}
throw new IllegalStateException(String.format("Can't resolve required component type for %s!", getType()));
}
/**
* Returns whether the property is a {@link java.util.Map}. If this returns {@literal true} you can expect
* {@link #getComponentType()} as well as {@link #getMapValueType()} to return something not {@literal null}.
*
*
* @return
*/
boolean isMap();
/**
* Will return the type of the value in case the underlying type is a {@link java.util.Map}.
*
*
* @return
*/
Optional<TypeInformation<?>> getMapValueType();
TypeInformation<?> getMapValueType();
/**
* Will return the type of the value in case the underlying type is a {@link java.util.Map} or throw
* {@link IllegalStateException} if the map value type cannot be resolved.
*
* @return
* @throws IllegalStateException if the map value type cannot be resolved.
* @since 2.0
*/
default TypeInformation<?> getRequiredMapValueType() {
return getMapValueType().orElseThrow(
() -> new IllegalStateException(String.format("Can't resolve required map value type for %s!", getType())));
TypeInformation<?> mapValueType = getMapValueType();
if (mapValueType != null) {
return mapValueType;
}
throw new IllegalStateException(String.format("Can't resolve required map value type for %s!", getType()));
}
/**
* Returns the type of the property. Will resolve generics and the generic context of
*
*
* @return
*/
Class<S> getType();
@@ -109,7 +146,7 @@ public interface TypeInformation<S> {
/**
* Returns a {@link ClassTypeInformation} to represent the {@link TypeInformation} of the raw type of the current
* instance.
*
*
* @return
*/
ClassTypeInformation<?> getRawTypeInformation();
@@ -117,7 +154,7 @@ public interface TypeInformation<S> {
/**
* Transparently returns the {@link java.util.Map} value type if the type is a {@link java.util.Map}, returns the
* component type if the type {@link #isCollectionLike()} or the simple type if none of this applies.
*
*
* @return
*/
TypeInformation<?> getActualType();
@@ -125,7 +162,7 @@ public interface TypeInformation<S> {
/**
* Returns a {@link TypeInformation} for the return type of the given {@link Method}. Will potentially resolve
* generics information against the current types type parameter bindings.
*
*
* @param method must not be {@literal null}.
* @return
*/
@@ -133,7 +170,7 @@ public interface TypeInformation<S> {
/**
* Returns the {@link TypeInformation}s for the parameters of the given {@link Method}.
*
*
* @param method must not be {@literal null}.
* @return
*/
@@ -141,7 +178,7 @@ public interface TypeInformation<S> {
/**
* Returns the {@link TypeInformation} for the given raw super type.
*
*
* @param superType must not be {@literal null}.
* @return the {@link TypeInformation} for the given raw super type or {@literal null} in case the current
* {@link TypeInformation} does not implement the given type.
@@ -152,7 +189,7 @@ public interface TypeInformation<S> {
* Returns if the current {@link TypeInformation} can be safely assigned to the given one. Mimics semantics of
* {@link Class#isAssignableFrom(Class)} but takes generics into account. Thus it will allow to detect that a
* {@code List<Long>} is assignable to {@code List<? extends Number>}.
*
*
* @param target
* @return
*/
@@ -160,7 +197,7 @@ public interface TypeInformation<S> {
/**
* Returns the {@link TypeInformation} for the type arguments of the current {@link TypeInformation}.
*
*
* @return
*/
List<TypeInformation<?>> getTypeArguments();
@@ -169,7 +206,7 @@ public interface TypeInformation<S> {
* Specializes the given (raw) {@link ClassTypeInformation} using the context of the current potentially parameterized
* type, basically turning the given raw type into a parameterized one. Will return the given type as is if no
* generics are involved.
*
*
* @param type must not be {@literal null}.
* @return will never be {@literal null}.
*/