From cdc4497664ed73290837d04c515579fc80248d43 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 5 Jun 2023 13:57:59 +0200 Subject: [PATCH] Restore creation of plain HashSet/HashMap for direct HashSet/HashMap type Closes gh-30596 --- .../org/springframework/core/CollectionFactory.java | 10 ++++++++-- .../springframework/core/CollectionFactoryTests.java | 12 ++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/CollectionFactory.java b/spring-core/src/main/java/org/springframework/core/CollectionFactory.java index 9d9332dbff..f15620cc7d 100644 --- a/spring-core/src/main/java/org/springframework/core/CollectionFactory.java +++ b/spring-core/src/main/java/org/springframework/core/CollectionFactory.java @@ -179,7 +179,7 @@ public final class CollectionFactory { @SuppressWarnings("unchecked") public static Collection createCollection(Class collectionType, @Nullable Class elementType, int capacity) { Assert.notNull(collectionType, "Collection type must not be null"); - if (LinkedHashSet.class == collectionType || HashSet.class == collectionType || + if (LinkedHashSet.class == collectionType || Set.class == collectionType || Collection.class == collectionType) { return new LinkedHashSet<>(capacity); } @@ -197,6 +197,9 @@ public final class CollectionFactory { Assert.notNull(elementType, "Cannot create EnumSet for unknown element type"); return EnumSet.noneOf(asEnumType(elementType)); } + else if (HashSet.class == collectionType) { + return new HashSet<>(capacity); + } else { if (collectionType.isInterface() || !Collection.class.isAssignableFrom(collectionType)) { throw new IllegalArgumentException("Unsupported Collection type: " + collectionType.getName()); @@ -297,7 +300,7 @@ public final class CollectionFactory { @SuppressWarnings({"rawtypes", "unchecked"}) public static Map createMap(Class mapType, @Nullable Class keyType, int capacity) { Assert.notNull(mapType, "Map type must not be null"); - if (LinkedHashMap.class == mapType || HashMap.class == mapType || Map.class == mapType) { + if (LinkedHashMap.class == mapType || Map.class == mapType) { return new LinkedHashMap<>(capacity); } else if (LinkedMultiValueMap.class == mapType || MultiValueMap.class == mapType) { @@ -310,6 +313,9 @@ public final class CollectionFactory { Assert.notNull(keyType, "Cannot create EnumMap for unknown key type"); return new EnumMap(asEnumType(keyType)); } + else if (HashMap.class == mapType) { + return new HashMap<>(capacity); + } else { if (mapType.isInterface() || !Map.class.isAssignableFrom(mapType)) { throw new IllegalArgumentException("Unsupported Map type: " + mapType.getName()); diff --git a/spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java b/spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java index 32292b3977..e46ca45149 100644 --- a/spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java +++ b/spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java @@ -217,15 +217,15 @@ class CollectionFactoryTests { // concrete types testCollection(ArrayList.class, ArrayList.class); - testCollection(HashSet.class, LinkedHashSet.class); + testCollection(HashSet.class, HashSet.class); testCollection(LinkedHashSet.class, LinkedHashSet.class); testCollection(TreeSet.class, TreeSet.class); } private void testCollection(Class collectionType, Class resultType) { assertThat(CollectionFactory.isApproximableCollectionType(collectionType)).isTrue(); - assertThat(createCollection(collectionType, 0)).isInstanceOf(resultType); - assertThat(createCollection(collectionType, String.class, 0)).isInstanceOf(resultType); + assertThat(createCollection(collectionType, 0)).isExactlyInstanceOf(resultType); + assertThat(createCollection(collectionType, String.class, 0)).isExactlyInstanceOf(resultType); } @Test @@ -266,7 +266,7 @@ class CollectionFactoryTests { testMap(MultiValueMap.class, LinkedMultiValueMap.class); // concrete types - testMap(HashMap.class, LinkedHashMap.class); + testMap(HashMap.class, HashMap.class); testMap(LinkedHashMap.class, LinkedHashMap.class); testMap(TreeMap.class, TreeMap.class); testMap(LinkedMultiValueMap.class, LinkedMultiValueMap.class); @@ -274,8 +274,8 @@ class CollectionFactoryTests { private void testMap(Class mapType, Class resultType) { assertThat(CollectionFactory.isApproximableMapType(mapType)).isTrue(); - assertThat(createMap(mapType, 0)).isInstanceOf(resultType); - assertThat(createMap(mapType, String.class, 0)).isInstanceOf(resultType); + assertThat(createMap(mapType, 0)).isExactlyInstanceOf(resultType); + assertThat(createMap(mapType, String.class, 0)).isExactlyInstanceOf(resultType); } @Test