Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CollectionFactory API is not type-safe [SPR-12537] #17142

Closed
2 tasks
spring-projects-issues opened this issue Dec 11, 2014 · 2 comments
Closed
2 tasks

CollectionFactory API is not type-safe [SPR-12537] #17142

spring-projects-issues opened this issue Dec 11, 2014 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 11, 2014

Sam Brannen opened SPR-12537 and commented

Status Quo

The changes made to CollectionFactory in conjunction with #15691 resulted in method signatures that are not type-safe, and the changes made in conjunction with #17089 further exacerbated the issue.

For example, the parameterized type E is not bound to the type of elements contained in the collection argument passed to createApproximateCollection(). Thus casting the value returned by EnumSet#copyOf(EnumSet) to (Collection<E>) can never guarantee that the returned collection actually contains elements of type E.


Example Tests

Each of the following tests (adapted from CollectionFactoryTests) compiles but throws a ClassCastException due to the lacking type safety of the method signatures.

@Test
public void createApproximateCollectionIsNotTypeSafeForEnumSet() {
	Collection<Integer> ints = createApproximateCollection(EnumSet.of(Color.BLUE), 3);
	ints.add(42);
}

@Test
public void createCollectionIsNotTypeSafeForEnumSet() {
	Collection<Integer> ints = createCollection(EnumSet.class, Color.class, 3);
	ints.add(42);
}

@Test
public void createApproximateMapIsNotTypeSafeForEnumMap() {
	EnumMap<Color, Integer> enumMap = new EnumMap<>(Color.class);
	enumMap.put(Color.RED, 1);
	enumMap.put(Color.BLUE, 2);
	Map<String, Integer> map = createApproximateMap(enumMap, 3);
	map.put("foo", 1);
}

@Test
public void createMapIsNotTypeSafeForEnumMap() {
	Map<String, Integer> map = createMap(EnumMap.class, Color.class, 3);
	map.put("foo", 1);
}

@Test
public void createMapIsNotTypeSafeForLinkedMultiValueMap() {
	Map<String, Integer> map = createMap(MultiValueMap.class, null, 3);
	map.put("foo", 1);
}

Deliverables

  1. Ensure that all methods in CollectionFactory are type-safe.
    • Consider reverting to raw types, or alternatively to Collection<Object> and Map<Object, Object> as was done by the Spring Data Team.
  2. Refactor CollectionFactoryTests accordingly.

Affects: 4.0 GA

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

While I do see the point, I don't think that this is a bug per se but rather just a tradeoff: The caller is responsible for specifying corresponding arguments and element types. Being able to assign the return value to a typed collection is a convenience which brings CollectionFactory use syntactically closer to direct collection instantiation; otherwise, the caller would have to explicitly cast to a collection of a specific element type. The caller is free to treat the return value as a raw collection or collection of Object if he chooses to avoid this convenience.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jan 4, 2015

Sam Brannen commented

Sounds reasonable, and I've updated the documentation to reflect that (see #17197).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants