JsArray<String>.slice() always causes ClassCastException
See original GitHub issueApologies if this should not be a bug (or should be filed against GWT itself), but it was a surprise to discover (and a pain to debug) - JsArray<T>.slice()
returns T[]
, which always causes a class cast exception, at least where T is java.lang.String
. Workaround: use JsString or Object.
Steps to reproduce:
JsArray<String> strings = new JsArray<>("a", "b", "c");
String[] sliced = strings.slice();
DomGlobal.window.alert(sliced[0]);
Replace String
with either JsString
or Object
above, and it will work. Note that Double
fails in the same way, despite ostensibly also being a value that can be passed interchangeably in and out of other JS methods. In fact, it appears that even elemental2 expects that this will work correctly: elemental2.core.ITemplateArray
apparently extends JsArray<String>
.
I’m not sure how this issue can be reasonably mitigated, aside from baking checks into GWT that this sort of thing isn’t safe. Perhaps those methods should be annotated with @UncheckedCast
?
Issue Analytics
- State:
- Created 6 years ago
- Comments:15 (13 by maintainers)
Top GitHub Comments
At a minimum I think
JsArray<T>
methods should returnJsArray<T>
(methods on other classes could continue to return arrays); there might be other classes/methods with similar cases (maybe all “generic arrays” should beJsArray
s?)We are currently leaning towards replacing all arrays with JsArray<T> which will be iterable via
#asList
and convertible to array viatoArray(Class<T> clazz)
. This needs to finalized before 1.0 release.(@jDramaix: pls also collect feedback from here as well.)