From 65bfd5b33a35e6619398e59571fb01bc1404900b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 1 Jun 2016 18:17:47 +0200 Subject: [PATCH] =?UTF-8?q?DATACMNS-743=20-=20Optimizations=20in=20TypeDis?= =?UTF-8?q?coverer=20getProperty(=E2=80=A6)=20in=20case=20if=20invalid=20p?= =?UTF-8?q?roperty=20names.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now also cache a failed property lookup so that we can return null on a subsequent call for the same invalid property name right away and don't have to reattempt the lookup unnecessarily. --- .../data/util/TypeDiscoverer.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index f13ffd84d..a7050ff60 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -15,9 +15,11 @@ */ package org.springframework.data.util; +import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.NonNull; import lombok.RequiredArgsConstructor; +import lombok.Value; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; @@ -51,7 +53,7 @@ class TypeDiscoverer implements TypeInformation { private final Type type; private final Map, Type> typeVariableMap; - private final Map> fieldTypes = new ConcurrentHashMap>(); + private final Map fieldTypes = new ConcurrentHashMap(); private final int hashCode; private boolean componentTypeResolved = false; @@ -192,14 +194,14 @@ class TypeDiscoverer implements TypeInformation { int separatorIndex = fieldname.indexOf('.'); if (separatorIndex == -1) { + if (fieldTypes.containsKey(fieldname)) { - return fieldTypes.get(fieldname); + return fieldTypes.get(fieldname).getType(); } TypeInformation propertyInformation = getPropertyInformation(fieldname); - if (propertyInformation != null) { - fieldTypes.put(fieldname, propertyInformation); - } + fieldTypes.put(fieldname, ValueHolder.of(propertyInformation)); + return propertyInformation; } @@ -603,4 +605,22 @@ class TypeDiscoverer implements TypeInformation { return result; } } + + /** + * Simple wrapper to be able to store {@literal null} values in a {@link ConcurrentHashMap}. + * + * @author Oliver Gierke + */ + @Value + @RequiredArgsConstructor(access = AccessLevel.PRIVATE) + private static class ValueHolder { + + static ValueHolder NULL_HOLDER = new ValueHolder(null); + + TypeInformation type; + + public static ValueHolder of(TypeInformation type) { + return null == type ? NULL_HOLDER : new ValueHolder(type); + } + } }