Generics, JSNI and Integer problem

238 views
Skip to first unread message

stole

unread,
Aug 9, 2013, 10:33:08 AM8/9/13
to google-we...@googlegroups.com
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.

Thomas Broyer

unread,
Aug 12, 2013, 1:55:57 PM8/12/13
to google-we...@googlegroups.com
You need to wrap your int into a java.lang.Integer using its valueOf or new(). JSNI doesn't do auto-boxing/unboxing.

stole

unread,
Aug 16, 2013, 8:26:25 AM8/16/13
to google-we...@googlegroups.com
Thank you very much for your help. This, indeed, solved the problem.
Reply all
Reply to author
Forward
0 new messages