2013-08-07 7 views
2

최근에 현재 가지고있는 복잡한 문제를 해결할 js (harmony) weakmap 지원을 보았습니다. luckly 꽤 심한 브라우저 지원을 제공하는 shim (https://github.com/Benvie/WeakMap)이 GWT 래퍼 구현으로 도약했습니다.GWT에서 WeakMap 래퍼 구현 만들기 - 오류 발생

나는 이것이 매우 단순해야한다고 느꼈지만, 나의 단위 테스트는 오류를 던지고있다.

다음

내 GWT 클래스입니다 : 여기

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; 
     } 
    } 
} 

오류 I입니다 : 여기
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); 
    }-*/; 
} 

내 테스트한다 나는 JSNI에 아주 새로운 오전 있기 때문에 나는 사소한 뭔가를 실종됐다 어쩌면 생각 나는 점점 : 또한

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: [email protected]]): 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) 

:

  • 내가 자바 스크립트가 올바르게 포함되고 확신 셋업() 메소드가 제대로 weakmap 객체를 만들고 자바 스크립트 객체입니다 nativeWeakMap에 할당
  • (나는 충분히 알고 그 부분을 완료했습니다). 나는 이것이 null이 아니라는 것을 알기 때문에 이것을 표준 출력으로 출력 할 수있다.

아이디어가 있으십니까? 이것은 GWT에 큰 도움이 될 것입니다!

답변

0

GWT JSNI에서 this에 액세스하려면이 방법을 사용하십시오. GWT JSNI에서 this의 모든 항목에 대해 동일한 작업을 수행합니다.

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

new $wnd.WeakMap() 대신 new $wnd.WeakMap

해야한다