Skip to content

Commit 31a2472

Browse files
committed
Consistent support for EnumSet subclasses in CollectionFactory
Issue: SPR-17619
1 parent 7a7958f commit 31a2472

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

spring-core/src/main/java/org/springframework/core/CollectionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -195,7 +195,7 @@ else if (SortedSet.class == collectionType || NavigableSet.class == collectionTy
195195
throw new IllegalArgumentException("Unsupported Collection interface: " + collectionType.getName());
196196
}
197197
}
198-
else if (EnumSet.class == collectionType) {
198+
else if (EnumSet.class.isAssignableFrom(collectionType)) {
199199
Assert.notNull(elementType, "Cannot create EnumSet for unknown element type");
200200
// Cast is necessary for compilation in Eclipse 4.4.1.
201201
return (Collection<E>) EnumSet.noneOf(asEnumType(elementType));

spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,11 +35,11 @@
3535
import java.util.TreeSet;
3636

3737
import org.junit.Test;
38+
3839
import org.springframework.util.LinkedMultiValueMap;
3940
import org.springframework.util.MultiValueMap;
4041

41-
import static org.hamcrest.CoreMatchers.*;
42-
import static org.hamcrest.Matchers.empty;
42+
import static org.hamcrest.Matchers.*;
4343
import static org.junit.Assert.*;
4444
import static org.springframework.core.CollectionFactory.*;
4545

@@ -104,7 +104,7 @@ public void createCollectionIsNotTypeSafeForEnumSet() {
104104
* {@link CollectionFactory#createApproximateMap(Object, int)}
105105
* is not type-safe.
106106
* <p>The reasoning is similar that described in
107-
* {@link #createApproximateCollectionIsNotTypeSafe()}.
107+
* {@link #createApproximateCollectionIsNotTypeSafeForEnumSet}.
108108
*/
109109
@Test
110110
public void createApproximateMapIsNotTypeSafeForEnumMap() {
@@ -242,6 +242,12 @@ public void createsEnumSet() {
242242
assertThat(createCollection(EnumSet.class, Color.class, 0), is(instanceOf(EnumSet.class)));
243243
}
244244

245+
@Test // SPR-17619
246+
public void createsEnumSetSubclass() {
247+
EnumSet<Color> enumSet = EnumSet.noneOf(Color.class);
248+
assertThat(createCollection(enumSet.getClass(), Color.class, 0), is(instanceOf(enumSet.getClass())));
249+
}
250+
245251
@Test(expected = IllegalArgumentException.class)
246252
public void rejectsInvalidElementTypeForEnumSet() {
247253
createCollection(EnumSet.class, Object.class, 0);
@@ -297,7 +303,8 @@ public void rejectsNullMapType() {
297303
}
298304

299305

300-
static enum Color {
306+
enum Color {
301307
RED, BLUE;
302308
}
309+
303310
}

0 commit comments

Comments
 (0)