using Generics in GWT JsInterop: Uncaught java.lang.ClassCastException

312 views
Skip to first unread message

zakaria amine

unread,
Jul 26, 2016, 10:45:03 AM7/26/16
to GWT Users

I have an interface annotated with @JsFunction that I use as a callback for events. As there are different types of events, I have added a type parameter:


@JsFunction
public interface Function<T extends Event>{

  public JavaScriptObject call(T event);

}


From the entry point I do something like :


//...

map.on("click", new Function<MouseEvent>(){
        @Override
        public JavaScriptObject call(MouseEvent event) {

    });


When I run the project, everything compiles and run fine. But when the code above is executed, I get the following exception in the console:

Uncaught java.lang.ClassCastException                                        InternalPreconditions.java:45


This seems like a GWT internal error. Is this a known bug in GWT 2.8 ? Are generics not allowed with JsInterop types?

Thomas Broyer

unread,
Jul 26, 2016, 11:07:00 AM7/26/16
to GWT Users
Many things have changed since 2.8.0-beta1, have you tried with 2.8.0-SNAPSHOT? (I'm not saying it will work, I'm saying one shouldn't draw conclusions based on beta1)

zakaria amine

unread,
Jul 27, 2016, 10:28:29 AM7/27/16
to GWT Users
Thanks for your suggestion. SNAPHOT did resolve this error, but caused others. I ended up using JSNI and the cast() method of the JavaScriptObject. 

@JsFunction
public interface Function{

  public JavaScriptObject call(JavaScriptObject event);

}


@JsType
public class MouseEvent extends JavaScriptObject {
	
    protected MouseEvent(){}

   public final native  String getType() /*-{
	return this.target;
    }-*/;

}

and then: 
map.on("click", new Function(){
        @Override
        public JavaScriptObject call(JavaScriptObject event) {

          MouseEvent msEvent = event.cast();

    });


This is the only workaround I found for now, although I would have prefered to use JsInterop only. Hoping that the stable release will correct this issue. 

Jens

unread,
Jul 27, 2016, 10:42:12 AM7/27/16
to GWT Users

Thanks for your suggestion. SNAPHOT did resolve this error, but caused others.

Which other errors?

-- J. 
 

Thomas Broyer

unread,
Jul 27, 2016, 11:28:17 AM7/27/16
to GWT Users


On Wednesday, July 27, 2016 at 4:28:29 PM UTC+2, zakaria amine wrote:
Hoping that the stable release will correct this issue. 

The final release will likely be the same as the current SNAPSHOT, unless blockers are found in RC1 (RC1 should be the same as the current SNAPSHOT, when it comes out, real soon now)
This may have to wait for a 2.8.1.

Kirill Prazdnikov

unread,
Jul 27, 2016, 11:44:47 AM7/27/16
to GWT Users
And after the release it will be possible to remove old deprecated stuff like legacy devmode and old IE support, right ? 

zakaria amine

unread,
Jul 29, 2016, 11:19:31 AM7/29/16
to GWT Users
It resolved the compilation problem, but it did throw something like a javascript error  "cannot resolve property gwidgets of undefined" which I could not resolve, even by adding namespaces. 
Reply all
Reply to author
Forward
0 new messages