当我使用ArrayList时,如何让GWT避免包含每个可序列化的类 [英] How can I keep GWT from trying to include every serializable class when I use ArrayList

查看:197
本文介绍了当我使用ArrayList时,如何让GWT避免包含每个可序列化的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GWT中有一个需要返回List的RPC服务。该列表可以填充各种类型的对象,所有这些对象都是可序列化的,并且所有对象都在我的服务的其他地方引用,因此它们应该可用于GWT RPC。但是,除非我使用泛型类型参数(例如 ArrayList< String> ),否则GWT会给我警告:

 
返回类型:java.util.ArrayList
java.util.ArrayList
验证实例化性
java.util.ArrayList
[WARN]全部检查符合序列化条件的Object的子类型
添加'465'新生成的单元

基本上,我只想要一种方法来声明没有GWT的列表或ArrayList试图为类路径上的每个可序列化对象生成代码。是不是有什么办法可以告诉GWT我知道自己在做什么,不要发疯?

让我扩大大卫·诺尔斯所说的话。 GWT编译器无法读懂你的思想,所以当你不能指定返回类型时,GWT会认为它可以是任何东西,并且必须做额外的工作以确保可以在Javascript客户端发生。

你真的应该指定哪些类型能够返回。这样做只有一个好处 - 因为编译器会生成更优化的代码,而不是生成代码来处理'465 genreated units',所以你的下载速度会更快。



<我建议创建一个名为BaseResult的空接口,然后让你返回的对象都实现该接口。

  / ** 
*标记界面
* /
public interface BaseResult {
}

然后指定rpc方法的返回类型是ArrayList:

  public interface MyRpcService extends RemoteService {
public ArrayList< BaseResult> doRpc();
}

然后确保你的返回对象都实现了这个接口。

  public class UserInfo implements BaseResult {} 
public class Order implements BaseResult {}

现在,GWT编译器会为您的代码优化更简单。


I have an RPC service in GWT that needs to return a List. The List can be filled with various types of objects, all of which are serializable and all of are referenced elsewhere in my service so they should be available to GWT RPC. However, unless I put on a generic type parameter (e.g. ArrayList<String>), GWT gives me the warning:

Return type: java.util.ArrayList
    java.util.ArrayList
      Verifying instantiability
         java.util.ArrayList
            [WARN] Checking all subtypes of Object which qualify for serialization`
Adding '465' new generated units

Essentially, I just want a way to declare List or ArrayList without GWT trying to generate code for every serializable object on the class path. Isn't there some way to tell GWT that I know what I'm doing and not to go crazy?

解决方案

Let me expand on what David Nouls said. The GWT compiler can't read your mind, so when you fail to specify what the return types can be, GWT assumes that it can be anything, and has to do extra work to make sure that can happen on the Javascript client side.

You really should specify what types are able to be returned. There is only upside to doing this--as the compiler will produce more optimized code, rather than generating code to handle '465 genreated units', so your downloads will be faster.

I would suggest creating an empty interface called "BaseResult" and then having the objects you return all implement that that interface.

/**
 * Marker interface 
 */
public interface BaseResult {
}

Then you specify that the return type of your rpc method is ArrayList:

public interface MyRpcService extends RemoteService {
  public ArrayList<BaseResult> doRpc();
}

Then make sure your return objects all implement that interface.

public class UserInfo implements BaseResult {}
public class Order implements BaseResult {}

Now the GWT compiler will have a much easier time optimizing for your code.

这篇关于当我使用ArrayList时,如何让GWT避免包含每个可序列化的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆