From 75ac90150f5e1f3d2b77f5c82df13bd69d3a96d9 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 20 Jul 2017 12:56:11 +0200 Subject: [PATCH] DATACMNS-1119 - Cache for ClassTypeInformation is now a ConcurrentReferenceHashMap. --- .../data/util/ClassTypeInformation.java | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/springframework/data/util/ClassTypeInformation.java b/src/main/java/org/springframework/data/util/ClassTypeInformation.java index 6924f55cf..452d65cbc 100644 --- a/src/main/java/org/springframework/data/util/ClassTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ClassTypeInformation.java @@ -15,8 +15,6 @@ */ package org.springframework.data.util; -import java.lang.ref.Reference; -import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; @@ -29,11 +27,11 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.WeakHashMap; import org.springframework.core.GenericTypeResolver; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.ConcurrentReferenceHashMap; /** * {@link TypeInformation} for a plain {@link Class}. @@ -50,13 +48,10 @@ public class ClassTypeInformation extends TypeDiscoverer { public static final ClassTypeInformation MAP = new ClassTypeInformation(Map.class); public static final ClassTypeInformation OBJECT = new ClassTypeInformation(Object.class); - private static final Map, Reference>> CACHE = Collections - .synchronizedMap(new WeakHashMap, Reference>>()); + private static final Map, ClassTypeInformation> CACHE = new ConcurrentReferenceHashMap<>(); static { - for (ClassTypeInformation info : Arrays.asList(COLLECTION, LIST, SET, MAP, OBJECT)) { - CACHE.put(info.getType(), new WeakReference<>(info)); - } + Arrays.asList(COLLECTION, LIST, SET, MAP, OBJECT).forEach(it -> CACHE.put(it.getType(), it)); } private final Class type; @@ -72,16 +67,7 @@ public class ClassTypeInformation extends TypeDiscoverer { Assert.notNull(type, "Type must not be null!"); - Reference> cachedReference = CACHE.get(type); - TypeInformation cachedTypeInfo = cachedReference == null ? null : cachedReference.get(); - - if (cachedTypeInfo != null) { - return (ClassTypeInformation) cachedTypeInfo; - } - - ClassTypeInformation result = new ClassTypeInformation<>(type); - CACHE.put(type, new WeakReference<>(result)); - return result; + return (ClassTypeInformation) CACHE.computeIfAbsent(type, it -> new ClassTypeInformation<>(type)); } /**