Elemental2 1.0.0-beta-1 and casting behaves unexpectedly

127 views
Skip to first unread message

Daniel Harezlak

unread,
May 31, 2017, 2:23:20 AM5/31/17
to GWT Contributors
Hi, I have the following code using elemental2-dom:

HTMLIFrameElement frame = (HTMLIFrameElement) DomGlobal.document.createElement("iframe");
HTMLDocument doc = (HTMLDocument) frame.contentDocument; //throws java.lang.ClassCastException

The second line throws an exception although I am quite sure that the returned element is an HTMLDocument because when I log it in the JS console I get
"[object HTMLDocument]". The first line works fine. What is strange the exception is only thrown in Chrome both in SDM and production modes. In Firefox it works just fine. Any thoughts?

Daniel Harezlak

unread,
May 31, 2017, 3:25:53 AM5/31/17
to GWT Contributors
The way to work around it is to use a native method:

private static native HTMLDocument getHtmlDocument(HTMLIFrameElement frame) /*-{
    return frame.contentDocument;
}-*/
;

Is this required?

gwtt...@gmail.com

unread,
May 31, 2017, 6:06:08 AM5/31/17
to GWT Contributors
How about using jsinterop.base.Js.cast() for the casing instead? - see: https://groups.google.com/forum/#!topic/Google-Web-Toolkit/0XGv7EhDFmc

Kirill Prazdnikov

unread,
May 31, 2017, 7:02:46 AM5/31/17
to GWT Contributors
Hi, I have the same issue and use the same JSNI hack
It is interesting why
GWT does runtime type checks for @JsType(isNative = true) ?
Why is it reasonable ?

Daniel Harezlak

unread,
May 31, 2017, 1:21:38 PM5/31/17
to GWT Contributors
Js.cast() also throws but Js.uncheckedCast() does not. Can anyone elaborate?

Daniel Harezlak

unread,
May 31, 2017, 1:30:50 PM5/31/17
to GWT Contributors
OK, this this particular use case was explained here: https://groups.google.com/forum/#!topic/google-web-toolkit-contributors/8lESs0WMHDM. Thank you Arnaud.
Reply all
Reply to author
Forward
0 new messages