使用 JSNI 将 JavaScript 集成到 GWT 时出现问题

发布于 2024-11-17 19:58:12 字数 1965 浏览 7 评论 0 原文

我正在尝试使用 JSNI 为 GWT 的 WebSockets 创建一个简单的绑定,但每次调用 JSNI 方法时我都会遇到异常。简化的类定义如下:

public class Socket extends JavaScriptObject{
    protected Socket() {}

    public static native Socket connect(String url) /*-{
        return new WebSocket(url);
    }-*/;
}

在尝试实例化 Socket 对象时,使用以下行:

Socket socket = Socket.connect("http://www.google.com");

我收到以下异常,但我不知道为什么:

   java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
    at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:193)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:510)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassFormatError: Illegal method name "$" in class edu/catalindumitru/gwt/socket/Socket
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
    at com.google.gwt.dev.shell.CompilingClassLoader.findClass(CompilingClassLoader.java:1078)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at edu.catalindumitru.gwt.steel.client.GameCore.onModuleLoad(GameCore.java:32)
    ... 9 more

我之前尝试过为类型化数组和 Web 创建类似的绑定工作人员,但我遇到了完全相同的错误,因此我决定暂停这些绑定的开发并尝试更简单的方法,直到找到此异常的原因。

I am trying to create a simple binding for WebSockets for GWT using JSNI, but I keep getting an exception every time a JSNI method is invoked. The simplified class definition is as fallows:

public class Socket extends JavaScriptObject{
    protected Socket() {}

    public static native Socket connect(String url) /*-{
        return new WebSocket(url);
    }-*/;
}

While trying to instantiate a Socket object, using the line :

Socket socket = Socket.connect("http://www.google.com");

i get the fallowing exception and I don't know why :

   java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)
    at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:193)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:510)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ClassFormatError: Illegal method name "$" in class edu/catalindumitru/gwt/socket/Socket
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
    at com.google.gwt.dev.shell.CompilingClassLoader.findClass(CompilingClassLoader.java:1078)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at edu.catalindumitru.gwt.steel.client.GameCore.onModuleLoad(GameCore.java:32)
    ... 9 more

I have tried previously to create a similar binding for typed arrays and web workers, but I got the exact same error, so I decided to suspend development for those bindings and try something simpler until I can find the reason for this exception.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弄潮 2024-11-24 19:58:12

当我按照惯例使用 JavaScriptObject 时,我从 GWT 得到了相同的错误报告。

我的问题很简单,所有 JSO 方法都必须声明为final,而我错过了一个。

I got the same error report from GWT when using JavaScriptObject's quite conventionally.

My problem was simply that all JSO methods must be declared final, and I had missed one.

左岸枫 2024-11-24 19:58:12

您缺少 $wnd 前缀

return new $wnd.WebSocket(url);

You are missing the $wnd prefix

return new $wnd.WebSocket(url);
暮年慕年 2024-11-24 19:58:12

只是猜测,但据我所知,覆盖层更多地用于 JSON 类型数据。在这里,您尝试对浏览器 API 对象执行封送处理。我不确定它是否会这样工作。特别是 JSNI 对传递的类型有限制。

也许您可以尝试将 WebSocket 存储为本机部分中的字段,并将方法委托给该字段。但这只是一种盲目的猜测。

Just a guess, but as far as I seen the overlays were used rather for JSON type data. Here your try to perform marshaling for browser API object. I'm not sure if it is going to work this way. Especially that JSNI have restrictions of types being passed.

Maybe you could try storing WebSocket as a field in native part, and delegate methods to that field. But this is just a blind guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文