You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
The issue with this is that it is not clear exactly what to do when a null key and/or value is encountered.
Clearly null does not have a type, and therefore step 1 does not apply, but in step 2 this is not the case:
If one of the key or value type is assignable to the target type, then this is used. If both are assignable the key is used.
The "assignability" of null is unclear. Any object reference may be set to null, so in some senses a null key should "win" at this stage. On the other hand an implementation which uses targetClass.isInstance(key) will be told that null is not an instance that can be assigned. Furthermore it isn't possible to use targetClass.isAssignableFrom(...) because there is no source class for null.
From practical experience with the converter (and discussions with others) I would strongly recommend an erratum indicating that null is not considered assignable in step 2. This prevents odd cases like the following:
Map<Integer, Short> map = Collections.singletonMap(null, 5);
Number number = standardConverter.convert(map).to(Number.class);
// This will print null if null is considered assignable, or 5 otherwise
System.out.println(number);
Note that it is now, and still will be, possible for Map -> List conversions to end up with a mixture of keys and values. From a slack discussion with David Bosschaert:
What if you have a map with 3 key-value pairs. (1,“hi”), (2,null),
(3,“ho”) and you convert that to a list of String.
Answer: ["hi", "2", "ho"]
Because null is bad and will cause you pain sometimes!
It is possible to construct examples with non-null keys and values that also cause mixed lists/arrays. The main guarantee is that the list/array will have the same size as the map, and will fail if any conversion fails.
Original bug ID: BZ#3120
From: @timothyjward
Reported version: R7
The text was updated successfully, but these errors were encountered: