question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JsArray<String>.slice() always causes ClassCastException

See original GitHub issue

Apologies 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:closed
  • Created 6 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
tbroyercommented, Jan 7, 2018

At a minimum I think JsArray<T> methods should return JsArray<T> (methods on other classes could continue to return arrays); there might be other classes/methods with similar cases (maybe all “generic arrays” should be JsArrays?)

0reactions
gkdncommented, Jun 28, 2019

We are currently leaning towards replacing all arrays with JsArray<T> which will be iterable via #asList and convertible to array via toArray(Class<T> clazz). This needs to finalized before 1.0 release.

(@jDramaix: pls also collect feedback from here as well.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

java: (String[])List.toArray() gives ClassCastException
toArray(). it returns an Object[], which can't be cast to a String[] (even tho the contents are Strings) This is because the toArray...
Read more >
ClassCastException: Arrays$ArrayList cannot be cast to ...
It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not...
Read more >
How to Fix java.lang.classcastexception in Java?
ClassCastException in Java occurs when we try to convert the data type of entry into another. This is related to the type conversion...
Read more >
How to fix java.lang.classcastexception cannot be cast to in Java
ClassCastException in Java by using Generics and writing type-safe parameterized classes and method, its good to know real cause of ClassCastException and ...
Read more >
IJ14255: CLASS CAST EXCEPTION ERROR - IBM
This causes a class cast exception any time the action method is invoked. It is not normally a problem, since Maximo always sets...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found