Creating WeakMap wrapper implementation in GWT -- getting errors

215 views Asked by At

I have been recently looking at the upcoming js (harmony) weakmap support that would solve a number of complex problems I currently have. Luckly there is a shim (https://github.com/Benvie/WeakMap) with pretty good browser support which led me to take the leap into implementing a GWT wrapper.

I felt like this should be pretty simple, but my unit tests are throwing errors. Since I am pretty new to JSNI I thought maybe it was missing something trivial:

Here is my GWT class:

public class WeakMap<K, V> {

    protected JavaScriptObject nativeWeakMap;

    public WeakMap(){
        setup();
    }

    native void setup()/*-{
            [email protected]::nativeWeakMap = new $wnd.WeakMap;
    }-*/;

    public void set(K key, V value){

        nativeSet(key, value);
    }

    native void nativeSet(K key, V value)/*-{
        var nativeMap = [email protected]::nativeWeakMap;

        nativeMap.set(key, value);

    }-*/;

    public V get(K key){
        return nativeGet(key);
    }

    native V nativeGet(K key)/*-{
        var nativeMap = [email protected]::nativeWeakMap;

        nativeMap.get(key);
    }-*/;
}

Here is my test:

public class WeakMapTest extends GWTTestCase {

    @Override
    public String getModuleName() {

        return "com.jorsek.util.Util";
    }

    @Test
    public void testWeakMap(){
        WeakMap<SomeObject, SomeObject> map = new WeakMap<SomeObject, SomeObject>();
        SomeObject key = new SomeObject("KEY");
        SomeObject value = new SomeObject("VALUE");

        map.set(key, value);

        assertEquals(map.get(key).getValue(), value.getValue());
    }

    private class SomeObject {
        private final String value;

        public SomeObject(String value){
            this.value = value;
        }

        private String getValue() {
            return value;
        }
    }
}

Here is the error I am getting:

Unknown property name in get ewkjl1e0u6m

java.lang.RuntimeException: Remote test failed at 192.168.1.136 / Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19
    at com.google.gwt.junit.JUnitShell.processTestResult(JUnitShell.java:1283)
    at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1403)
    at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1304)
    at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:652)
    at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:441)
    at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:296)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.Exception: com.google.gwt.core.client.JavaScriptException: (null) @com.jorsek.util.client.WeakMap::nativeGet(Ljava/lang/Object;)([Java object: com.jorsek.util.client.WeakMapTest$SomeObject@1077321824]): null
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    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)
    at com.jorsek.util.client.WeakMap.nativeGet(WeakMap.java)
    at com.jorsek.util.client.WeakMap.get(WeakMap.java:35)
    at com.jorsek.util.client.WeakMapTest.testWeakMap(WeakMapTest.java:22)
    at com.jorsek.util.client.__WeakMapTest_unitTestImpl.doRunTest(__WeakMapTest_unitTestImpl.java:7)
    at com.google.gwt.junit.client.GWTTestCase.runBare(GWTTestCase.java:188)
    at com.google.gwt.junit.client.GWTTestCase.__doRunTest(GWTTestCase.java:129)
    at com.google.gwt.junit.client.impl.GWTRunner.runTest(GWTRunner.java:389)
    at com.google.gwt.junit.client.impl.GWTRunner.doRunTest(GWTRunner.java:318)
    at com.google.gwt.junit.client.impl.GWTRunner.access$9(GWTRunner.java:312)
    at com.google.gwt.junit.client.impl.GWTRunner$TestBlockListener.onSuccess(GWTRunner.java:107)
    at com.google.gwt.junit.client.impl.GWTRunner$InitialResponseListener.onSuccess(GWTRunner.java:63)
    at com.google.gwt.junit.client.impl.GWTRunner$InitialResponseListener.onSuccess(GWTRunner.java:1)
    at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:232)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    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)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:249)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Thread.java:680)

Also:

  • I am sure the javascript is being included correctly (I've done that part enough to know)
  • The setup() method correctly creates a weakmap object and assigns it to the nativeWeakMap javascript object. I can print this to stdout so I know it's not null.

Any ideas? This would be a great addition to GWT!

1

There are 1 answers

0
Braj On

Use in this way to access this in GWT JSNI. Do same for all the occurrences of this in GWT JSNI.

native void setup()/*-{
 var theInstance = this;
 [email protected]::nativeWeakMap = new $wnd.WeakMap();
}-*/;

It should be new $wnd.WeakMap() instead of new $wnd.WeakMap