Friday, August 9, 2013

Generics, JSNI and Integer problem

I have declared the following class:

public class JsniTest<T> {
    private Map<T, String> map;

    public JsniTest(Map<T, String> map) {
        this.map = map;
    }

    public String get(T rowKey) {
        return map.get(rowKey);
    }
}
 
and the following "main" class:

public class TheApp implements EntryPoint {

  public void onModuleLoad() {
    final Map<Integer, String> map = new HashMap<Integer, String>();
    map.put(0, "test");
    final JsniTest<Integer> test = new JsniTest<Integer>(map);
    final String result = testJsni(test, 0);
    l.info("testJsni:" + result);
  }

  public native String testJsni(JsniTest x, int key) /*-{
      return x.@com.test.client.JsniTest::get(Ljava/lang/Object;)(key);
  }-*/;
}

When I run this code on Gwt 2.5.1 in debug mode I get the following exception:
SEVERE: invoke arguments: JS value of type number, expected java.lang.Object
java.lang.IllegalArgumentException: invoke arguments: JS value of type number, expected java.lang.Object
    at com.google.gwt.dev.shell.JsValueGlue.get(JsValueGlue.java:178)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:65)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:338)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:219)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)

Is it possible to invoke the get method of JsniTest and receive the proper result? What are my options? The JsniTest class is just a replacement for the real class I need to use, over which I have no control as it is a third party library class. I could possibly wrap it with a wrapper class that would work for any T. I have full control over TheApp class and the testJsni method. Thanks for your help.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" 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 http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment