Gwt覆盖每当方法得到使用时编译错误

问题描述:

我想测试摆脱gwt-rpc入口点,而是使用基于JAX-RS/JSON的入口点。Gwt覆盖每当方法得到使用时编译错误

为此,我只使用本机GWT RequestBuilder API。

根据下面引用的文档。 http://www.gwtproject.org/doc/latest/tutorial/JSON.html

我面临的问题是,编译器似乎不满意让我使用任何覆盖API,即任何没有Java代码编译和标记为本机的方法。

我当然使用最新的,最好的gwt 2.8编译器。

如果我按照以下方式编写我的覆盖图。

public class UserLoginGwtRpcMessageOverlay extends JavaScriptObject { 

    /** 
    * Mandatory PROTECTED no arguments constructor. 
    */ 
    protected UserLoginGwtRpcMessageOverlay() { 
     super(); 
    } 


    public final native String getUserName(); /* 
               * { return userName; } 
               */ 


    public final native String getHashedPassword(); /* 
                * { return hashedPassword; 
                * } 
                */ 


    public final String toStringOverlay() { 
     return getUserName() + "-" + getHashedPassword(); 
    } 

该类不会编译。 它不会编译,因为我的人工toString正在使用覆盖API,例如, (getUserName())。

如果我将这些调用从类中取出,编译器不会中断处理类。

的进一步深入,如果我试图做一个REST调用如下:

private void invokeRestService() { 
     try { 
      // (a) prepare the JSON request to the server 
      RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, JSON_URL); 

      // (b) send an HTTP Json request 
      Request request = builder.sendRequest(null, new RequestCallback() { 

       // (i) callback handler when there is an error 
       public void onError(Request request, Throwable exception) { 
        LOGGER.log(Level.SEVERE, "Couldn't retrieve JSON", exception); 
       } 

       // (ii) callback result on success 
       public void onResponseReceived(Request request, Response response) { 
        if (200 == response.getStatusCode()) { 
         UserLoginGwtRpcMessageOverlay responseOverlay = JsonUtils 
           .<UserLoginGwtRpcMessageOverlay>safeEval(response.getText()); 
         LOGGER.info("responseOverlay: " + responseOverlay.getUserName()); 
        } else { 
         LOGGER.log(Level.SEVERE, "Couldn't retrieve JSON (" + response.getStatusText() + ")"); 
        } 
       } 
      }); 
     } catch (RequestException e) { 
      LOGGER.log(Level.SEVERE, "Couldn't execute request ", e); 
     } 
    } 

再次,编制,应当失败。 再次,这是我尝试使用getUserName()的结果。

特别是,下面的代码行破坏了编译器。

LOGGER.info("responseOverlay: " + responseOverlay.getUserName()); 

由于编译器运行的空指针异常给没有其他提示,除了:

<no source info>: <source info not available> 

我怀疑我处理或者与编译器bug,或者一个功能,不知怎么去支持,其API仍然流连忘返。但是与此同时,我会感到惊讶,因为我认为覆盖是GWT的核心部分,这应该是正常的。所以我更有可能在我没有发现的代码中存在一些错误。

QUOTE完整的编译错误:

[INFO] --- gwt-maven-plugin:2.8.0:compile (gwt-compile) @ jntl-expenses-frontend --- [INFO] Compiling module org.gwtproject.tutorial.TodoList [INFO] Compiling 1 permutation [INFO] Compiling permutation 0... [INFO] [ERROR] An internal compiler exception occurred [INFO] com.google.gwt.dev.jjs.InternalCompilerException: Unexpected error during visit. [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.translateException(JVisitor.java:111) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:276) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:265) [INFO] at com.google.gwt.dev.jjs.impl.MakeCallsStatic$CreateStaticImplsVisitor.visit(MakeCallsStatic.java:222) [INFO] at com.google.gwt.dev.jjs.ast.JMethod.traverse(JMethod.java:777) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:127) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:122) [INFO] at com.google.gwt.dev.jjs.impl.MakeCallsStatic$CreateStaticImplsVisitor.getOrCreateStaticImpl(MakeCallsStatic.java:240) [INFO] at com.google.gwt.dev.jjs.impl.Devirtualizer$RewriteVirtualDispatches.ensureDevirtualVersionExists(Devirtualizer.java:271) [INFO] at com.google.gwt.dev.jjs.impl.Devirtualizer$RewriteVirtualDispatches.endVisit(Devirtualizer.java:160) [INFO] at com.google.gwt.dev.jjs.ast.JMethodCall.traverse(JMethodCall.java:268) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:265) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:118) [INFO] at com.google.gwt.dev.jjs.ast.JBinaryOperation.traverse(JBinaryOperation.java:89) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:265) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:118) [INFO] at com.google.gwt.dev.jjs.ast.JExpressionStatement.traverse(JExpressionStatement.java:42) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor$ListContext.traverse(JModVisitor.java:88) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.acceptWithInsertRemove(JModVisitor.java:331) [INFO] at com.google.gwt.dev.jjs.ast.JBlock.traverse(JBlock.java:92) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:139) [INFO] at com.google.gwt.dev.jjs.ast.JIfStatement.traverse(JIfStatement.java:53) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor$ListContext.traverse(JModVisitor.java:88) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.acceptWithInsertRemove(JModVisitor.java:331) [INFO] at com.google.gwt.dev.jjs.ast.JBlock.traverse(JBlock.java:92) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:139) [INFO] at com.google.gwt.dev.jjs.ast.JVisitor.accept(JVisitor.java:135) [INFO] at com.google.gwt.dev.jjs.ast.JMethodBody.traverse(JMethodBody.java:83) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:265) [INFO] at com.google.gwt.dev.jjs.ast.JMethod.visitChildren(JMethod.java:786) [INFO] at com.google.gwt.dev.jjs.ast.JMethod.traverse(JMethod.java:778) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor$ListContextImmutable.traverse(JModVisitor.java:169) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.acceptWithInsertRemoveImmutable(JModVisitor.java:336) [INFO] at com.google.gwt.dev.jjs.ast.JClassType.traverse(JClassType.java:147) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:265) [INFO] at com.google.gwt.dev.jjs.ast.JProgram.visitModuleTypes(JProgram.java:1284) [INFO] at com.google.gwt.dev.jjs.ast.JProgram.traverse(JProgram.java:1249) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:265) [INFO] at com.google.gwt.dev.jjs.impl.Devirtualizer.execImpl(Devirtualizer.java:409) [INFO] at com.google.gwt.dev.jjs.impl.Devirtualizer.exec(Devirtualizer.java:324) [INFO] at com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.normalizeSemantics(JavaToJavaScriptCompiler.java:489) [INFO] at com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.compilePermutation(JavaToJavaScriptCompiler.java:364) [INFO] at com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.compilePermutation(JavaToJavaScriptCompiler.java:272) [INFO] at com.google.gwt.dev.CompilePerms.compile(CompilePerms.java:198) [INFO] at com.google.gwt.dev.ThreadedPermutationWorkerFactory$ThreadedPermutationWorker.compile(ThreadedPermutationWorkerFactory.java:50) [INFO] at com.google.gwt.dev.PermutationWorkerFactory$Manager$WorkerThread.run(PermutationWorkerFactory.java:74) [INFO] at java.lang.Thread.run(Thread.java:745) [INFO] Caused by: java.lang.NullPointerException [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.traverse(JModVisitor.java:361) [INFO] at com.google.gwt.dev.jjs.ast.JModVisitor.accept(JModVisitor.java:273) [INFO] ... 59 more [INFO] [ERROR] : [INFO] [ERROR] at UserLoginGwtRpcMessageOverlay.java(23): org.gwtproject.tutorial.client.overlay.UserLoginGwtRpcMessageOverlay.getUserName()Ljava/lang/String; [INFO] com.google.gwt.dev.jjs.ast.JMethod [INFO]
[ERROR] at TodoList.java(148): responseOverlay.getUserName() [INFO]
com.google.gwt.dev.jjs.ast.JMethodCall [INFO] [ERROR] at TodoList.java(148): "responseOverlay: " + responseOverlay.getUserName() [INFO]
com.google.gwt.dev.jjs.ast.JBinaryOperation [INFO] [ERROR] at TodoList.java(148): "responseOverlay: " + responseOverlay.getUserName() [INFO]
com.google.gwt.dev.jjs.ast.JExpressionStatement [INFO]
[ERROR] at TodoList.java(145): { [INFO] final UserLoginGwtRpcMessageOverlay responseOverlay = (UserLoginGwtRpcMessageOverlay) JsonUtils.safeEval(response.getText()); [INFO] TodoList.$clinit(); [INFO] "responseOverlay: " + responseOverlay.getUserName(); [INFO] } [INFO] com.google.gwt.dev.jjs.ast.JBlock [INFO]
[ERROR] at TodoList.java(145): if (200 == response.getStatusCode()) { [INFO] final UserLoginGwtRpcMessageOverlay responseOverlay = (UserLoginGwtRpcMessageOverlay) JsonUtils.safeEval(response.getText()); [INFO] TodoList.$clinit(); [INFO] "responseOverlay: " + responseOverlay.getUserName(); [INFO] } else { [INFO] TodoList.$clinit(); [INFO] Level.$clinit(); [INFO]
"Couldn\'t retrieve JSON (" + response.getStatusText() + ")"; [INFO] } [INFO] com.google.gwt.dev.jjs.ast.JIfStatement [INFO]
[ERROR] at TodoList.java(144): { [INFO] if (200 == response.getStatusCode()) { [INFO] final UserLoginGwtRpcMessageOverlay responseOverlay = (UserLoginGwtRpcMessageOverlay) JsonUtils.safeEval(response.getText()); [INFO] TodoList.$clinit(); [INFO] "responseOverlay: " + responseOverlay.getUserName(); [INFO] } else { [INFO] TodoList.$clinit(); [INFO] Level.$clinit(); [INFO] "Couldn\'t retrieve JSON (" + response.getStatusText() + ")"; [INFO] } [INFO] } [INFO]
com.google.gwt.dev.jjs.ast.JBlock [INFO] [ERROR] at TodoList.java(144): { [INFO] if (200 == response.getStatusCode()) { [INFO] final UserLoginGwtRpcMessageOverlay responseOverlay = (UserLoginGwtRpcMessageOverlay) JsonUtils.safeEval(response.getText()); [INFO] TodoList.$clinit(); [INFO] "responseOverlay: " + responseOverlay.getUserName(); [INFO] } else { [INFO] TodoList.$clinit(); [INFO] Level.$clinit(); [INFO] "Couldn\'t retrieve JSON (" + response.getStatusText() + ")"; [INFO] } [INFO] } [INFO]
com.google.gwt.dev.jjs.ast.JMethodBody [INFO] [ERROR] at TodoList.java(144): org.gwtproject.tutorial.client.TodoList$3.onResponseReceived(Lcom/google/gwt/http/client/Request;Lcom/google/gwt/http/client/Response;)V [INFO] com.google.gwt.dev.jjs.ast.JMethod [INFO]
[ERROR] at TodoList.java(136): org.gwtproject.tutorial.client.TodoList$3 (final extends Object implements RequestCallback) [INFO]
com.google.gwt.dev.jjs.ast.JClassType [INFO] [ERROR] at Unknown(0): [INFO]
com.google.gwt.dev.jjs.ast.JProgram

是任何人都经历GWT 2.8问题叠加,还是我做某种错误的,其中我不知道。

亲切的问候, 任何好的指针表示赞赏。

public final native String getUserName(); /* 
              * { return userName; } 
              */ 

这不是有效的JSNI(在getHashedPassword()中有相同的问题)。正确的方式来写,这将是

public final native String getUserName() /*-{ 
    return userName; 
}-*/; 

您必须/*-{开始和结束}-*/;,并没有*小号像插图中的Javadoc的威力。

但是,作为JS,这没有任何意义,所以虽然它会编译,但它不是你想要的。该方法的主体应该读

return this.userName; 

,因为JS没有一个隐含的this像Java一样。

乍一看,其余的看起来没问题,但没有合法的JSNI,编译器无法接受它。

+0

完美。 今天晚上我会试试。 并执行我对这个问题的无知。 我从一个普通的POJO开始,并开始从eclipse中使用注释切片自动生成的gettters。现在我发现分号出现在最后并不是任意的。 许多thnks。 – 99Sono

+0

工作正常。谢谢。 – 99Sono