我最近一直在关注即将推出的js(和谐)弱图支持,它可以解决我目前遇到的一些复杂问题。幸运的是,有一个垫片(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)
此外:
有什么想法吗?这对GWT来说是一个很好的补充!
答案 0 :(得分:0)
以这种方式使用GWT JSNI中的this
。对GWT JSNI中this
的所有匹配项执行相同操作。
native void setup()/*-{
var theInstance = this;
theInstance.@com.jorsek.util.client.WeakMap::nativeWeakMap = new $wnd.WeakMap();
}-*/;
应该是new $wnd.WeakMap()
而不是new $wnd.WeakMap