From b21b2e89343cc06880e2570ea25e68a720db9285 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 22 Mar 2024 13:36:22 +0100 Subject: [PATCH] Align internal caches with core framework patterns. Initialize maps and sets with size where possible. Closes #3067 Original pull request: #3073 --- .../data/auditing/MappingAuditableBeanWrapperFactory.java | 4 ++-- .../convert/SimplePropertyValueConverterRegistry.java | 8 +++++--- .../org/springframework/data/domain/ExampleMatcher.java | 8 +++++--- .../data/geo/format/DistanceFormatter.java | 2 +- .../data/mapping/callback/DefaultEntityCallbacks.java | 4 ++-- .../mapping/callback/DefaultReactiveEntityCallbacks.java | 4 ++-- .../data/mapping/callback/EntityCallbackDiscoverer.java | 3 +-- .../data/mapping/context/AbstractMappingContext.java | 2 +- .../mapping/context/PersistentPropertyPathFactory.java | 4 ++-- .../data/mapping/model/BasicPersistentEntity.java | 7 +++---- .../springframework/data/mapping/model/BeanWrapper.java | 1 - .../model/ClassGeneratingPropertyAccessorFactory.java | 2 +- .../DefaultMethodInvokingMethodInterceptor.java | 5 ++--- .../data/projection/ProxyProjectionFactory.java | 4 ++-- .../data/projection/SpelEvaluatingMethodInterceptor.java | 5 +++-- .../data/querydsl/binding/QuerydslBindingsFactory.java | 4 ++-- .../core/support/MethodInvocationValidator.java | 3 ++- .../core/support/QueryExecutorMethodInterceptor.java | 5 +++-- .../repository/core/support/RepositoryComposition.java | 5 ++--- .../repository/core/support/RepositoryFactorySupport.java | 8 +++----- ...xtensionAwareQueryMethodEvaluationContextProvider.java | 2 +- .../data/repository/util/QueryExecutionConverters.java | 6 +++--- .../springframework/data/util/KotlinBeanInfoFactory.java | 6 ++++-- .../data/util/NullableWrapperConverters.java | 4 ++-- .../java/org/springframework/data/util/TypeCollector.java | 2 +- .../data/web/ProjectingJackson2HttpMessageConverter.java | 4 ++-- .../data/web/XmlBeamHttpMessageConverter.java | 5 ++--- 27 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java b/src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java index 82d73d161..b48659ba6 100644 --- a/src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java +++ b/src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java @@ -19,6 +19,7 @@ import java.lang.annotation.Annotation; import java.time.temporal.TemporalAccessor; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Predicate; import java.util.stream.Stream; @@ -40,7 +41,6 @@ import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.mapping.context.PersistentEntities; import org.springframework.data.util.Lazy; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; /** * {@link AuditableBeanWrapperFactory} that will create am {@link AuditableBeanWrapper} using mapping information @@ -67,7 +67,7 @@ public class MappingAuditableBeanWrapperFactory extends DefaultAuditableBeanWrap Assert.notNull(entities, "PersistentEntities must not be null"); this.entities = entities; - this.metadataCache = new ConcurrentReferenceHashMap<>(); + this.metadataCache = new ConcurrentHashMap<>(); } @Override diff --git a/src/main/java/org/springframework/data/convert/SimplePropertyValueConverterRegistry.java b/src/main/java/org/springframework/data/convert/SimplePropertyValueConverterRegistry.java index 50adf3fba..560b5cdba 100644 --- a/src/main/java/org/springframework/data/convert/SimplePropertyValueConverterRegistry.java +++ b/src/main/java/org/springframework/data/convert/SimplePropertyValueConverterRegistry.java @@ -32,12 +32,14 @@ import org.springframework.util.ObjectUtils; public class SimplePropertyValueConverterRegistry

> implements ValueConverterRegistry

{ - private final Map>> converterRegistrationMap = new LinkedHashMap<>(); + private final Map>> converterRegistrationMap; - public SimplePropertyValueConverterRegistry() {} + public SimplePropertyValueConverterRegistry() { + this.converterRegistrationMap = new LinkedHashMap<>(); + } SimplePropertyValueConverterRegistry(SimplePropertyValueConverterRegistry

source) { - this.converterRegistrationMap.putAll(source.converterRegistrationMap); + this.converterRegistrationMap = new LinkedHashMap<>(source.converterRegistrationMap); } @Override diff --git a/src/main/java/org/springframework/data/domain/ExampleMatcher.java b/src/main/java/org/springframework/data/domain/ExampleMatcher.java index f152db25c..3d716fc11 100644 --- a/src/main/java/org/springframework/data/domain/ExampleMatcher.java +++ b/src/main/java/org/springframework/data/domain/ExampleMatcher.java @@ -775,12 +775,14 @@ public interface ExampleMatcher { */ class PropertySpecifiers { - private final Map propertySpecifiers = new LinkedHashMap<>(); + private final Map propertySpecifiers; - PropertySpecifiers() {} + PropertySpecifiers() { + this. propertySpecifiers = new LinkedHashMap<>(); + } PropertySpecifiers(PropertySpecifiers propertySpecifiers) { - this.propertySpecifiers.putAll(propertySpecifiers.propertySpecifiers); + this.propertySpecifiers = new LinkedHashMap<>(propertySpecifiers.propertySpecifiers); } public void add(PropertySpecifier specifier) { diff --git a/src/main/java/org/springframework/data/geo/format/DistanceFormatter.java b/src/main/java/org/springframework/data/geo/format/DistanceFormatter.java index 7246b8c5b..a28626bd5 100644 --- a/src/main/java/org/springframework/data/geo/format/DistanceFormatter.java +++ b/src/main/java/org/springframework/data/geo/format/DistanceFormatter.java @@ -47,7 +47,7 @@ public enum DistanceFormatter implements Converter, Formatter< static { - Map metrics = new LinkedHashMap<>(); + Map metrics = new LinkedHashMap<>(Metrics.values().length); for (Metric metric : Metrics.values()) { metrics.put(metric.getAbbreviation(), metric); diff --git a/src/main/java/org/springframework/data/mapping/callback/DefaultEntityCallbacks.java b/src/main/java/org/springframework/data/mapping/callback/DefaultEntityCallbacks.java index 8bf06520b..cfa54d182 100644 --- a/src/main/java/org/springframework/data/mapping/callback/DefaultEntityCallbacks.java +++ b/src/main/java/org/springframework/data/mapping/callback/DefaultEntityCallbacks.java @@ -17,6 +17,7 @@ package org.springframework.data.mapping.callback; import java.lang.reflect.Method; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import org.apache.commons.logging.Log; @@ -26,7 +27,6 @@ import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.ResolvableType; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ReflectionUtils; /** @@ -40,7 +40,7 @@ import org.springframework.util.ReflectionUtils; */ class DefaultEntityCallbacks implements EntityCallbacks { - private final Map, Method> callbackMethodCache = new ConcurrentReferenceHashMap<>(64); + private final Map, Method> callbackMethodCache = new ConcurrentHashMap<>(64); private final SimpleEntityCallbackInvoker callbackInvoker = new SimpleEntityCallbackInvoker(); private final EntityCallbackDiscoverer callbackDiscoverer; diff --git a/src/main/java/org/springframework/data/mapping/callback/DefaultReactiveEntityCallbacks.java b/src/main/java/org/springframework/data/mapping/callback/DefaultReactiveEntityCallbacks.java index d4becf307..e61c5fac2 100644 --- a/src/main/java/org/springframework/data/mapping/callback/DefaultReactiveEntityCallbacks.java +++ b/src/main/java/org/springframework/data/mapping/callback/DefaultReactiveEntityCallbacks.java @@ -19,6 +19,7 @@ import reactor.core.publisher.Mono; import java.lang.reflect.Method; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiFunction; import org.apache.commons.logging.Log; @@ -28,7 +29,6 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.core.ResolvableType; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ReflectionUtils; /** @@ -41,7 +41,7 @@ import org.springframework.util.ReflectionUtils; */ class DefaultReactiveEntityCallbacks implements ReactiveEntityCallbacks { - private final Map, Method> callbackMethodCache = new ConcurrentReferenceHashMap<>(64); + private final Map, Method> callbackMethodCache = new ConcurrentHashMap<>(64); private final ReactiveEntityCallbackInvoker callbackInvoker = new DefaultReactiveEntityCallbackInvoker(); private final EntityCallbackDiscoverer callbackDiscoverer; diff --git a/src/main/java/org/springframework/data/mapping/callback/EntityCallbackDiscoverer.java b/src/main/java/org/springframework/data/mapping/callback/EntityCallbackDiscoverer.java index 3e0907684..2706a4541 100644 --- a/src/main/java/org/springframework/data/mapping/callback/EntityCallbackDiscoverer.java +++ b/src/main/java/org/springframework/data/mapping/callback/EntityCallbackDiscoverer.java @@ -38,7 +38,6 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ReflectionUtils; import org.springframework.util.comparator.Comparators; @@ -54,7 +53,7 @@ class EntityCallbackDiscoverer { private final CallbackRetriever defaultRetriever = new CallbackRetriever(); private final Map retrieverCache = new ConcurrentHashMap<>(64); - private final Map, ResolvableType> entityTypeCache = new ConcurrentReferenceHashMap<>(64); + private final Map, ResolvableType> entityTypeCache = new ConcurrentHashMap<>(64); @Nullable private ClassLoader beanClassLoader; diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index d6f2a2b6d..4755f165c 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -461,7 +461,7 @@ public abstract class AbstractMappingContext descriptors = new HashMap<>(); + Map descriptors = new HashMap<>(pds.length); for (PropertyDescriptor descriptor : pds) { descriptors.put(descriptor.getName(), descriptor); diff --git a/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java b/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java index 586a832ae..2e0ae920a 100644 --- a/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java +++ b/src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java @@ -16,6 +16,7 @@ package org.springframework.data.mapping.context; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -33,7 +34,6 @@ import org.springframework.data.util.StreamUtils; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.StringUtils; /** @@ -49,7 +49,7 @@ class PersistentPropertyPathFactory, P extends private static final Predicate>> IS_ENTITY = PersistentProperty::isEntity; - private final Map propertyPaths = new ConcurrentReferenceHashMap<>(); + private final Map propertyPaths = new ConcurrentHashMap<>(); private final MappingContext context; public PersistentPropertyPathFactory(MappingContext context) { diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index edf091722..9bf1b7676 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.springframework.core.annotation.AnnotatedElementUtils; @@ -46,8 +47,6 @@ import org.springframework.expression.EvaluationContext; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; -import org.springframework.util.ConcurrentReferenceHashMap; -import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; @@ -117,9 +116,9 @@ public class BasicPersistentEntity> implement this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator)); this.propertyCache = new HashMap<>(16, 1f); - this.annotationCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK); + this.annotationCache = new ConcurrentHashMap<>(16); this.propertyAnnotationCache = CollectionUtils - .toMultiValueMap(new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK)); + .toMultiValueMap(new ConcurrentHashMap<>(16)); this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE; this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType())); this.isNewStrategy = Lazy.of(() -> Persistable.class.isAssignableFrom(information.getType()) // diff --git a/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java b/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java index 9d41eade5..4514f23a2 100644 --- a/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java +++ b/src/main/java/org/springframework/data/mapping/model/BeanWrapper.java @@ -32,7 +32,6 @@ import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.util.KotlinReflectionUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ReflectionUtils; /** diff --git a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java index eefbed43b..3ab3e0411 100644 --- a/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java +++ b/src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java @@ -1395,7 +1395,7 @@ public class ClassGeneratingPropertyAccessorFactory implements PersistentPropert private static Map createPropertyStackMap( List> persistentProperties) { - Map stackmap = new HashMap<>(); + Map stackmap = new HashMap<>(persistentProperties.size()); for (PersistentProperty property : persistentProperties) { stackmap.put(property.getName(), new PropertyStackAddress(new Label(), property.getName().hashCode())); diff --git a/src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java b/src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java index 457ed5888..ec73eaf83 100644 --- a/src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java +++ b/src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java @@ -22,14 +22,13 @@ import java.lang.invoke.MethodType; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.ProxyMethodInvocation; import org.springframework.lang.Nullable; -import org.springframework.util.ConcurrentReferenceHashMap; -import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType; import org.springframework.util.ReflectionUtils; /** @@ -43,7 +42,7 @@ import org.springframework.util.ReflectionUtils; public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor { private static final Lookup LOOKUP = MethodHandles.lookup(); - private final Map methodHandleCache = new ConcurrentReferenceHashMap<>(10, ReferenceType.WEAK); + private final Map methodHandleCache = new ConcurrentHashMap<>(); /** * Returns whether the {@code interfaceClass} declares {@link Method#isDefault() default methods}. diff --git a/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java b/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java index 39cab140c..1f5271c16 100644 --- a/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java +++ b/src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; @@ -34,7 +35,6 @@ import org.springframework.data.util.NullableWrapperConverters; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; /** * A {@link ProjectionFactory} to create JDK proxies to back interfaces and handle method invocations on them. By @@ -60,7 +60,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware } private final List factories; - private final Map, ProjectionMetadata> projectionInformationCache = new ConcurrentReferenceHashMap<>(); + private final Map, ProjectionMetadata> projectionInformationCache = new ConcurrentHashMap<>(); private @Nullable ClassLoader classLoader; private final Lazy defaultMethodInvokingMethodInterceptor = Lazy diff --git a/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java b/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java index c5d5fc364..52be16cc7 100644 --- a/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java +++ b/src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java @@ -107,9 +107,10 @@ class SpelEvaluatingMethodInterceptor implements MethodInterceptor { private static Map potentiallyCreateExpressionsForMethodsOnTargetInterface( ExpressionParser parser, Class targetInterface) { - Map expressions = new HashMap<>(); + Method[] methods = targetInterface.getMethods(); + Map expressions = new HashMap<>(methods.length); - for (Method method : targetInterface.getMethods()) { + for (Method method : methods) { Value value = AnnotationUtils.findAnnotation(method, Value.class); if (value == null) { diff --git a/src/main/java/org/springframework/data/querydsl/binding/QuerydslBindingsFactory.java b/src/main/java/org/springframework/data/querydsl/binding/QuerydslBindingsFactory.java index 9b29f6ac3..a811a4095 100644 --- a/src/main/java/org/springframework/data/querydsl/binding/QuerydslBindingsFactory.java +++ b/src/main/java/org/springframework/data/querydsl/binding/QuerydslBindingsFactory.java @@ -18,6 +18,7 @@ package org.springframework.data.querydsl.binding; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.springframework.beans.BeanUtils; @@ -30,7 +31,6 @@ import org.springframework.data.querydsl.EntityPathResolver; import org.springframework.data.repository.support.Repositories; import org.springframework.data.util.TypeInformation; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; import com.querydsl.core.types.EntityPath; @@ -63,7 +63,7 @@ public class QuerydslBindingsFactory implements ApplicationContextAware { Assert.notNull(entityPathResolver, "EntityPathResolver must not be null"); this.entityPathResolver = entityPathResolver; - this.entityPaths = new ConcurrentReferenceHashMap<>(); + this.entityPaths = new ConcurrentHashMap<>(); this.beanFactory = Optional.empty(); this.repositories = Optional.empty(); this.defaultCustomizer = NoOpCustomizer.INSTANCE; diff --git a/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java b/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java index a41e64d5a..64fbae3c0 100644 --- a/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java +++ b/src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java @@ -18,6 +18,7 @@ package org.springframework.data.repository.core.support; import java.lang.annotation.ElementType; import java.lang.reflect.Method; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; @@ -49,7 +50,7 @@ import org.springframework.util.ObjectUtils; public class MethodInvocationValidator implements MethodInterceptor { private final ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); - private final Map nullabilityCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK); + private final Map nullabilityCache = new ConcurrentHashMap<>(16); /** * Returns {@literal true} if the {@code repositoryInterface} is supported by this interceptor. diff --git a/src/main/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptor.java b/src/main/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptor.java index d61785a2b..4b3f609da 100644 --- a/src/main/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptor.java +++ b/src/main/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptor.java @@ -92,9 +92,10 @@ class QueryExecutorMethodInterceptor implements MethodInterceptor { private Map mapMethodsToQuery(RepositoryInformation repositoryInformation, QueryLookupStrategy lookupStrategy, ProjectionFactory projectionFactory) { - Map result = new HashMap<>(); + List queryMethods = repositoryInformation.getQueryMethods().toList(); + Map result = new HashMap<>(queryMethods.size()); - for (Method method : repositoryInformation.getQueryMethods()) { + for (Method method : queryMethods) { Pair pair = lookupQuery(method, repositoryInformation, lookupStrategy, projectionFactory); diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryComposition.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryComposition.java index e5c3a6837..d26dedf25 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryComposition.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryComposition.java @@ -38,7 +38,6 @@ import org.springframework.data.util.Streamable; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; @@ -101,7 +100,7 @@ public class RepositoryComposition { private static final RepositoryComposition EMPTY = new RepositoryComposition(null, RepositoryFragments.empty(), MethodLookups.direct(), PASSTHRU_ARG_CONVERTER); - private final Map methodCache = new ConcurrentReferenceHashMap<>(); + private final Map methodCache = new ConcurrentHashMap<>(); private final RepositoryFragments fragments; private final MethodLookup methodLookup; private final BiFunction argumentConverter; @@ -365,7 +364,7 @@ public class RepositoryComposition { static final RepositoryFragments EMPTY = new RepositoryFragments(Collections.emptyList()); - private final Map> fragmentCache = new ConcurrentReferenceHashMap<>(); + private final Map> fragmentCache = new ConcurrentHashMap<>(); private final Map invocationMetadataCache = new ConcurrentHashMap<>(); private final List> fragments; diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index f792c1a9c..13274805a 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -23,13 +23,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.beans.BeanUtils; @@ -66,8 +66,6 @@ import org.springframework.lang.Nullable; import org.springframework.transaction.interceptor.TransactionalProxy; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; -import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType; import org.springframework.util.ObjectUtils; /** @@ -110,7 +108,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, @SuppressWarnings("null") public RepositoryFactorySupport() { - this.repositoryInformationCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK); + this.repositoryInformationCache = new ConcurrentHashMap<>(16); this.postProcessors = new ArrayList<>(); this.repositoryBaseClass = Optional.empty(); @@ -727,7 +725,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware, */ static class RepositoryValidator { - static Map, String> WELL_KNOWN_EXECUTORS = new HashMap<>(); + static Map, String> WELL_KNOWN_EXECUTORS = new HashMap<>(4, 1f); static { diff --git a/src/main/java/org/springframework/data/repository/query/ExtensionAwareQueryMethodEvaluationContextProvider.java b/src/main/java/org/springframework/data/repository/query/ExtensionAwareQueryMethodEvaluationContextProvider.java index f4adae72e..44cd3f655 100644 --- a/src/main/java/org/springframework/data/repository/query/ExtensionAwareQueryMethodEvaluationContextProvider.java +++ b/src/main/java/org/springframework/data/repository/query/ExtensionAwareQueryMethodEvaluationContextProvider.java @@ -100,7 +100,7 @@ public class ExtensionAwareQueryMethodEvaluationContextProvider implements Query */ static Map collectVariables(Parameters parameters, Object[] arguments) { - Map variables = new HashMap<>(); + Map variables = new HashMap<>(parameters.getNumberOfParameters()); parameters.stream()// .filter(Parameter::isSpecialParameter)// diff --git a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java index 514fd1541..17aaf5647 100644 --- a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java +++ b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java @@ -79,11 +79,11 @@ public abstract class QueryExecutionConverters { private static final boolean VAVR_PRESENT = ClassUtils.isPresent("io.vavr.control.Try", QueryExecutionConverters.class.getClassLoader()); - private static final Set WRAPPER_TYPES = new HashSet<>(); - private static final Set UNWRAPPER_TYPES = new HashSet(); + private static final Set WRAPPER_TYPES = new HashSet<>(10, 1f); + private static final Set UNWRAPPER_TYPES = new HashSet(10, 1f); private static final Set> UNWRAPPERS = new HashSet<>(); private static final Set> ALLOWED_PAGEABLE_TYPES = new HashSet<>(); - private static final Map, ExecutionAdapter> EXECUTION_ADAPTER = new HashMap<>(); + private static final Map, ExecutionAdapter> EXECUTION_ADAPTER = new HashMap<>(3, 1f); private static final Map, Boolean> supportsCache = new ConcurrentReferenceHashMap<>(); private static final TypeInformation VOID_INFORMATION = TypeInformation.of(Void.class); diff --git a/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java b/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java index eab80c9bc..f7d00fc09 100644 --- a/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java +++ b/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java @@ -29,6 +29,7 @@ import java.beans.PropertyDescriptor; import java.beans.SimpleBeanInfo; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.Collection; import java.util.LinkedHashSet; import java.util.Set; @@ -59,9 +60,10 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered { } KClass kotlinClass = JvmClassMappingKt.getKotlinClass(beanClass); - Set pds = new LinkedHashSet<>(); + Collection> members = kotlinClass.getMembers(); + Set pds = new LinkedHashSet<>(members.size()); - for (KCallable member : kotlinClass.getMembers()) { + for (KCallable member : members) { if (member instanceof KProperty property) { diff --git a/src/main/java/org/springframework/data/util/NullableWrapperConverters.java b/src/main/java/org/springframework/data/util/NullableWrapperConverters.java index 044198c90..f83b877e4 100644 --- a/src/main/java/org/springframework/data/util/NullableWrapperConverters.java +++ b/src/main/java/org/springframework/data/util/NullableWrapperConverters.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; import org.springframework.core.convert.TypeDescriptor; @@ -32,7 +33,6 @@ import org.springframework.core.convert.converter.GenericConverter; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.util.ObjectUtils; import com.google.common.base.Optional; @@ -67,7 +67,7 @@ public abstract class NullableWrapperConverters { private static final Set WRAPPER_TYPES = new HashSet(); private static final Set UNWRAPPER_TYPES = new HashSet(); private static final Set> UNWRAPPERS = new HashSet>(); - private static final Map, Boolean> supportsCache = new ConcurrentReferenceHashMap<>(); + private static final Map, Boolean> supportsCache = new ConcurrentHashMap<>(); static { diff --git a/src/main/java/org/springframework/data/util/TypeCollector.java b/src/main/java/org/springframework/data/util/TypeCollector.java index fb42eb045..6d179d88a 100644 --- a/src/main/java/org/springframework/data/util/TypeCollector.java +++ b/src/main/java/org/springframework/data/util/TypeCollector.java @@ -242,7 +242,7 @@ public class TypeCollector { static class InspectionCache { - private final Map mutableCache = new LinkedHashMap<>(); + private final Map mutableCache = new HashMap<>(); public void add(ResolvableType resolvableType) { mutableCache.put(resolvableType.toString(), resolvableType); diff --git a/src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java b/src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java index 2b5fb5de2..c818b6afc 100644 --- a/src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java +++ b/src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java @@ -18,6 +18,7 @@ package org.springframework.data.web; import java.io.IOException; import java.lang.reflect.Type; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanClassLoaderAware; @@ -33,7 +34,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; import com.fasterxml.jackson.databind.ObjectMapper; import com.jayway.jsonpath.spi.json.JacksonJsonProvider; @@ -52,7 +52,7 @@ public class ProjectingJackson2HttpMessageConverter extends MappingJackson2HttpM implements BeanClassLoaderAware, BeanFactoryAware { private final SpelAwareProxyProjectionFactory projectionFactory; - private final Map, Boolean> supportedTypesCache = new ConcurrentReferenceHashMap<>(); + private final Map, Boolean> supportedTypesCache = new ConcurrentHashMap<>(); /** * Creates a new {@link ProjectingJackson2HttpMessageConverter} using a default {@link ObjectMapper}. diff --git a/src/main/java/org/springframework/data/web/XmlBeamHttpMessageConverter.java b/src/main/java/org/springframework/data/web/XmlBeamHttpMessageConverter.java index 9167836bd..7db27e674 100644 --- a/src/main/java/org/springframework/data/web/XmlBeamHttpMessageConverter.java +++ b/src/main/java/org/springframework/data/web/XmlBeamHttpMessageConverter.java @@ -17,6 +17,7 @@ package org.springframework.data.web; import java.io.IOException; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import javax.xml.parsers.DocumentBuilderFactory; @@ -31,8 +32,6 @@ import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.util.ConcurrentReferenceHashMap; - import org.xml.sax.SAXParseException; import org.xmlbeam.XBProjector; import org.xmlbeam.config.DefaultXMLFactoriesConfig; @@ -49,7 +48,7 @@ import org.xmlbeam.config.DefaultXMLFactoriesConfig; public class XmlBeamHttpMessageConverter extends AbstractHttpMessageConverter { private final XBProjector projectionFactory; - private final Map, Boolean> supportedTypesCache = new ConcurrentReferenceHashMap<>(); + private final Map, Boolean> supportedTypesCache = new ConcurrentHashMap<>(); /** * Creates a new {@link XmlBeamHttpMessageConverter}.