From b2ce4b8f1b0204538ae108011193286f620cb8d8 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 27 Jun 2017 18:25:20 +0200 Subject: [PATCH] DATACMNS-1101 - Use array to store map types in TypeDiscoverer. Prevent iterator creation. --- .../data/util/TypeDiscoverer.java | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index 55a062610..39697c3de 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -28,16 +28,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; -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.*; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -49,13 +40,13 @@ import org.springframework.util.ReflectionUtils; /** * Basic {@link TypeDiscoverer} that contains basic functionality to discover property types. - * + * * @author Oliver Gierke * @author Christoph Strobl */ class TypeDiscoverer implements TypeInformation { - private static final Iterable> MAP_TYPES; + private static final Class[] MAP_TYPES; static { @@ -68,7 +59,7 @@ class TypeDiscoverer implements TypeInformation { mapTypes.add(ClassUtils.forName("javaslang.collection.Map", classLoader)); } catch (ClassNotFoundException o_O) {} - MAP_TYPES = Collections.unmodifiableSet(mapTypes); + MAP_TYPES = mapTypes.toArray(new Class[0]); } private final Type type; @@ -82,7 +73,7 @@ class TypeDiscoverer implements TypeInformation { /** * Creates a new {@link TypeDiscoverer} for the given type, type variable map and parent. - * + * * @param type must not be {@literal null}. * @param typeVariableMap must not be {@literal null}. */ @@ -101,7 +92,7 @@ class TypeDiscoverer implements TypeInformation { /** * Returns the type variable map. - * + * * @return */ protected Map, Type> getTypeVariableMap() { @@ -114,7 +105,7 @@ class TypeDiscoverer implements TypeInformation { /** * Creates {@link TypeInformation} for the given {@link Type}. - * + * * @param fieldType * @return */ @@ -177,7 +168,7 @@ class TypeDiscoverer implements TypeInformation { /** * Resolves the given type into a plain {@link Class}. - * + * * @param type * @return */ @@ -229,7 +220,7 @@ class TypeDiscoverer implements TypeInformation { /** * Returns the {@link TypeInformation} for the given atomic field. Will inspect fields first and return the type of a * field if available. Otherwise it will fall back to a {@link PropertyDescriptor}. - * + * * @see #getGenericType(PropertyDescriptor) * @param fieldname * @return @@ -249,7 +240,7 @@ class TypeDiscoverer implements TypeInformation { /** * Finds the {@link PropertyDescriptor} for the property with the given name on the given type. - * + * * @param type must not be {@literal null}. * @param fieldname must not be {@literal null} or empty. * @return @@ -274,7 +265,7 @@ class TypeDiscoverer implements TypeInformation { /** * Returns the generic type for the given {@link PropertyDescriptor}. Will inspect its read method followed by the * first parameter of the write method. - * + * * @param descriptor must not be {@literal null} * @return */ @@ -304,7 +295,7 @@ class TypeDiscoverer implements TypeInformation { return resolvedType.get(); } - /* + /* * (non-Javadoc) * @see org.springframework.data.util.TypeInformation#getRawTypeInformation() */ @@ -313,7 +304,7 @@ class TypeDiscoverer implements TypeInformation { return ClassTypeInformation.from(getType()).getRawTypeInformation(); } - /* + /* * (non-Javadoc) * @see org.springframework.data.util.TypeInformation#getActualType() */ @@ -336,8 +327,10 @@ class TypeDiscoverer implements TypeInformation { */ public boolean isMap() { + Class type = getType(); + for (Class mapType : MAP_TYPES) { - if (mapType.isAssignableFrom(getType())) { + if (mapType.isAssignableFrom(type)) { return true; } } @@ -397,7 +390,7 @@ class TypeDiscoverer implements TypeInformation { return arguments.size() > 0 ? Optional.of(arguments.get(0)) : Optional.empty(); } - /* + /* * (non-Javadoc) * @see org.springframework.data.util.TypeInformation#getReturnType(java.lang.reflect.Method) */ @@ -407,7 +400,7 @@ class TypeDiscoverer implements TypeInformation { return createInfo(method.getGenericReturnType()); } - /* + /* * (non-Javadoc) * @see org.springframework.data.util.TypeInformation#getMethodParameterTypes(java.lang.reflect.Method) */ @@ -420,7 +413,7 @@ class TypeDiscoverer implements TypeInformation { .collect(Collectors.toList()); } - /* + /* * (non-Javadoc) * @see org.springframework.data.util.TypeInformation#getSuperTypeInformation(java.lang.Class) */ @@ -461,7 +454,7 @@ class TypeDiscoverer implements TypeInformation { return null; } - /* + /* * (non-Javadoc) * @see org.springframework.data.util.TypeInformation#getTypeParameters() */ @@ -484,7 +477,8 @@ class TypeDiscoverer implements TypeInformation { @SuppressWarnings("unchecked") public TypeInformation specialize(ClassTypeInformation type) { - Assert.isTrue(getType().isAssignableFrom(type.getType()), String.format("%s must be assignable from %s", getType(), type.getType())); + Assert.isTrue(getType().isAssignableFrom(type.getType()), + String.format("%s must be assignable from %s", getType(), type.getType())); List> arguments = getTypeArguments(); @@ -504,15 +498,16 @@ class TypeDiscoverer implements TypeInformation { return Optional.of(createInfo(arguments[index])); } - private Class getBaseType(Iterable> candidates) { + private Class getBaseType(Class[] candidates) { + Class type = getType(); for (Class candidate : candidates) { - if (candidate.isAssignableFrom(getType())) { + if (candidate.isAssignableFrom(type)) { return candidate; } } - throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", getType(), candidates)); + throw new IllegalArgumentException(String.format("Type %s not contained in candidates %s!", type, candidates)); } /*