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

Gwt Overlay Compilation error whenever method gets used

我正在尝试摆脱 gwt-rpc 入口点,而是使用基于 JAX-RS/JSON 的入口点。

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

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

我面临的问题是编译器似乎不高兴让我使用任何覆盖 API,即任何没有 java 代码可编译且被标记为原生。

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

如果我这样写我的overlay。

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

class 将无法编译。 而且它不会编译,因为我的人工 toString 正在使用覆盖 APIs,例如( getUserName()).

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

更进一步,如果我尝试按如下方式进行休息呼叫:

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>

我怀疑我正在处理一个编译器错误,或者一个以某种方式被取消支持并且 API 仍然存在的功能。但与此同时,我会感到惊讶,因为我认为覆盖是 GWT 的核心部分,这应该可以正常工作。所以更有可能我在代码中有一些我没有发现的错误。

QUOTE FULL 编译错误:

[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.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 (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 没有像 Java 那样的隐式 this

乍一看,其余的还好,但是没有合法的JSNI,编译器无法接受。