混合 Java POJO 和 Java POJO GWT 叠加类型
在我的应用程序中,我广泛使用 JsArray 来存储我的覆盖类型。我使用 java.util.List 来存储客户端 Java POJO。
出于性能原因并为了统一访问模型的方式,我计划消除列表并仅使用 JSO 包装器。给定一个可以存储任何 Java 对象的本机数组的包装器:
public class JsArrayObject<T> extends JavaScriptObject {
protected JsArrayObject() {}
public final native T get(int index) /*-{
return this[index];
}-*/;
public final native void push(T value) /*-{
this[this.length] = value;
}-*/;
}
以这种方式存储 Java 对象安全吗?该文档表示,当您将 Java 对象传递给 JavaScript 时,结果是“通过特殊语法可访问的不透明值”。这听起来让我很困惑。例如,如果我推送一个整数并尝试获取它,则会抛出异常,因为发现了与对象不同的东西(至少在开发模式下)。其余的 Java 原始包装器也会发生同样的情况。除了 Java 原始 Wrappers 的问题之外,还有其他需要注意的问题吗?
非常感谢
In my app I use JsArray extensively to store my overlay types. I use java.util.List to store my client-side Java POJOs.
For performance reasons and to unify the way I access my model I planned to eliminate the Lists and use only JSO wrappers. Given a wrapper around a native array that can store any Java Object:
public class JsArrayObject<T> extends JavaScriptObject {
protected JsArrayObject() {}
public final native T get(int index) /*-{
return this[index];
}-*/;
public final native void push(T value) /*-{
this[this.length] = value;
}-*/;
}
Is it safe to store Java Objects this way? The doc says that when you pass a Java Object into JavaScript the result is "an opaque value accessible through special syntax". This sounds confussing to me. For instance if I push an Integer and try to get it an exception will be thrown because something different than an Object was found (at least in dev mode). The same happens with the rest of Java primitive Wrappers. Apart from the problems with Java primitive Wrappers, are there other concerns to be aware?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指的是哪位医生? 此页面上的那个?
他们谈论的是将 Java 对象传递到 JavaScript 中,目的是让 JavaScript 代码使用对象中的方法或字段。这是可以做到的,但是在 JavaScript 端必须使用的语法有点尴尬。如果您做过 JSNI,您就会看到它。
如果您不打算从 JavaScript 端访问 Java 对象,您可以忽略有关特殊语法的事务。所以是的,它是安全的。我很想知道它是否真的有助于提高性能。
Which doc are you referring to? The one on this page?
They're talking in terms of passing Java objects into JavaScript with the intent of having JavaScript code use the methods or fields in the object. It is possible to do that, but the syntax you have to use on the JavaScript side is a bit awkward. If you've done any JSNI, you've seen it.
If you don't intend to access the Java objects from the JavaScript side you can ignore the business about the special syntax. So yes, it is safe. I'd be interested to know if it actually turns out to help performance.