Sunday, August 20, 2017

Re: ClassCastException in generics

My, understanding, because I have noticed the same many times in the past few weeks.

@JsType may (and that is a very weak may) work for a native type as long as you do not use it as you do not use it as a generic type argument. As soon as you do, for example, adding it to List<SomeType>, , cast checking kicks in and you end up with a class cast exception if SomeType is only annotated with @JsType. There is another case when it kicks in also. I have noticed also a couple times that code with just @JsType worked fine in superdev mode when not used in a generic context, but failed with a class cast exception, once compiled for release. As Vassillis mentioned, I am now conditioned to always specify the type name and package when isNative=true. Object is most commonly the name, but it could be different. However, Object will always be correct. The casting code in GWT is in Cast.java and the failure typically happens in the jsintanceof method, called from castToNative, with one of the parameters being null because the type name specified in the @JsType annotation is incorrect or missing. In your case since ArrayBuffer is an instance of Object, either Object or ArrayBuffer should work for the type name.

You can disable cast checking, but only when compiling for production, and not in superdev mode, by using the -XdisableCastChecking flag. For most applications that get data from the network and display it on the page, this will have no noticeable effect whatsoever, so it is best avoided. There is also the @UnecheckedCast annotation, which can be applied to selected methods in the critical path of performance sensitive code, and again, only takes effect in code compile for release, not superdev mode.

You would not want to disable cast checking in superdev mode anyways. I just finished porting an AngularJS/TypeScript application to GWT, and found a number of silent bugs in the old typescript (bugs that doe generate any output in the dev console) thanks to GWT cast checking. Typescript gives the illusion of types, but there is no runtime type checking when you cast something, GWT really does check, and that is a major advantage you would not want to lose.




On Friday, August 18, 2017 at 9:01:47 AM UTC-5, Kirill Prazdnikov wrote:
Hi, I`m always getting ClassCastException if I use @JsType(isNative = true) object as a generic template argument.

Is it possbile to do something with that ?
Is it possbile to avoid check-cast generation if the generic is used with @JsType type ?

Thanks

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment