我最近一直在研究即将推出的 js(harmony)weakmap 支持,它将解决我目前遇到的许多复杂问题。幸运的是,有一个 shim ( https://github.com/Benvie/WeakMap ) 具有很好的浏览器支持,这让我开始实现 GWT 包装器。
我觉得这应该很简单,但是我的单元测试抛出错误。由于我对 JSNI 很陌生,我想它可能遗漏了一些微不足道的东西:
这是我的 GWT 课程:
public class WeakMap<K, V> {
protected JavaScriptObject nativeWeakMap;
public WeakMap(){
setup();
}
native void setup()/*-{
this.@com.jorsek.util.client.WeakMap::nativeWeakMap = new $wnd.WeakMap;
}-*/;
public void set(K key, V value){
nativeSet(key, value);
}
native void nativeSet(K key, V value)/*-{
var nativeMap = this.@com.jorsek.util.client.WeakMap::nativeWeakMap;
nativeMap.set(key, value);
}-*/;
public V get(K key){
return nativeGet(key);
}
native V nativeGet(K key)/*-{
var nativeMap = this.@com.jorsek.util.client.WeakMap::nativeWeakMap;
nativeMap.get(key);
}-*/;
}
这是我的测试:
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;
}
}
}
这是我得到的错误:
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)
还:
- 我确信 javascript 被正确包含(我已经完成了足以知道的部分)
- setup() 方法正确地创建了一个weakmap 对象并将其分配给nativeWeakMap javascript 对象。我可以将它打印到标准输出,所以我知道它不为空。
有任何想法吗?这将是 GWT 的一个很好的补充!