Sunday, August 20, 2017

Re: ClassCastException in generics



On Friday, August 18, 2017 at 4:01:47 PM UTC+2, 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 ?


Think about what this means.

@JsType(isNative=true) applied to a com.example.foo.client.Foo class is strictly equivalent to @JsType(isNative=true, namespace="com.example.foo.client", name="Foo"), which means, literally: "this Java class is an "interop mapping" to be able to work with the com.example.foo.client.Foo JS "class/type" from Java code". This implies that you expect instances of that class to be "instanceof com.example.foo.client.Foo" (in JS), and btw such an "instanceof" in Java will literally translate to that same "instanceof" in JS.

If you do not intend to create instances of that Java type, then you can use a Java interface and use namespace=GLOBAL,name="?" (see note in http://www.gwtproject.org/javadoc/latest/jsinterop/annotations/JsType.html)
If you want to use a Java class and/or need to create instances (but do not have to use a specific JS constructor), then you'll want to use namespace=GLOBAL,name="Object".

But as Tony said, you do not want to disable cast-checking. If you think you need to do it, then you're most likely doing it wrong.

--
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