带有@ExtraTypes的GWT多态列表

时间:2013-03-05 16:39:18

标签: gwt polymorphism requestfactory

我对包含不同类型元素的列表有一点问题,我想看看你们中是否有人遇到过这个问题。这个问题应该通过使用@ExtraTypes来解决,但它对我不起作用,所以我想我没有正确使用它。因此,方案是(为了清楚起见,更改了bean名称):

GENERAL:

  • 我正在使用带有RequestFactory的GWT 2.5。

服务器端:

  • 我有一个RootBean,其中包含
    List <ChildBean>
  • 此ChildBean包含一些原始属性。
  • ChildBean也由一个继承所有父属性的MoreSpecificChildBean扩展,并添加了更多。
  • RootBean的列表中填充了ChildBean和MoreSpecificChildBean类型的元素,具体取决于某些逻辑。

客户端:

  • IRootBeanProxy是一个带有这些注释的ValueProxy:

     @ProxyFor (value = RootBean.class)
     @ExtraTypes ({IMoreSpecificChildBeanProxy.class})
    

并包含一个列表

List <IChildBeanProxy> getChildren ();

  • IChildBeanProxy是一个ValueProxy:
    @ProxyFor (value=ChildBean)
    public interface IChildBeanProxy extends ValueProxy
    
  • IMoreSpecificChildBeanProxy是一个ValueProxy:
    @ProxyFor (value=MoreSpecificChildBean)
    public interface IMoreSpecificChildBeanProxy extends IChildBeanProxy
    
  • Request上下文有一个返回Request的方法,我也在这里添加了@ExtraTypes注释:
    @Service (value = CompareService.class, locator = SpringServiceLocator.class)
    @ExtraTypes ({IChildBeanProxy.class, IMoreSpecificChildBeanProxy.class})
    public interface ICompareRequestContext extends RequestContext {
       Request <IRootBeanProxy> compare (Integer id1, Integer id2);
    

问题

据说有这些注释,RF应该知道多态继承类的存在,但我在客户端得到的只是一个带有IChildBeanProxy元素列表的IRootBeanProxy。此列表包含MoreSpecificChildBean,但是以IChildBeanProxy的形式,因此我无法从其他人那里告诉它。 所以我想知道我做错了什么,如果我将ExtraTypes注释设置在错误的位置或什么的。

任何?

所有帮助的Thx !!

1 个答案:

答案 0 :(得分:1)

我为很多类做了完全相同的事情,但它总是会返回我可以迭代的基类型,并在需要时测试instanceof。您可能必须将对象强制转换为子类。如果您不添加@ExtraTypes,您将会知道,因为在服务器端,您将收到一条消息,指出无法将MoreSpecificChildBean发送到客户端。

我只注释服务而不是代理,我遇到了一些问题,2.4将@ExtraTypes添加到代理中。

/**
 * Base proxy that all other metric proxies extend. It is used mainly for it's
 * inheritence with the RequestFactory. It's concrete implementation is
 * {@link MetricNumber}.
 * 
 * @author chinshaw
 */
@ProxyFor(value = Metric.class, locator = IMetricEntityLocator.class)
public interface MetricProxy extends DatastoreObjectProxy {


    /**
     * Name of this object in the ui. This will commonly be extended by
     * subclasses.
     */
    public String NAME = "Generic Metric";

    /**
     * This is a list of types of outputs that the ui can support. This is
     * typically used for listing types of supported Metrics in the operation
     * output screen.
     * 
     * @author chinshaw
     */
    public enum MetricOutputType {
        MetricNumber, MetricString, MetricCollection, MetricStaticChart, MetricDynamicChart
    }

    /**
     * See {@link MetricNumber#setName(String)}
     * 
     * @param name
     */
    public void setName(String name);

    /**
     * See {@link MetricNumber#setContext(String)}
     * 
     * @return name of the metric.
     */
    public String getName();


    /**
     * Get the list of violations attached to this metric.
     * 
     * @return
     */
    public List<ViolationProxy> getViolations();
}

@ProxyFor(value = MetricNumber.class, locator = IMetricEntityLocator.class)
public interface MetricNumberProxy extends MetricProxy {

    public List<NumberRangeProxy> getRanges();

    public void setRanges(List<NumberRangeProxy> ranges);
}

...

@ProxyFor(value = MetricDouble.class, locator = IMetricEntityLocator.class)
public interface MetricDoubleProxy extends MetricNumberProxy {

    /* Properties when fetching the object for with clause */
    public static String[] PROPERTIES = {"ranges"};

    public Double getValue();
}

...

@ProxyFor(value = MetricPlot.class, locator = IMetricEntityLocator.class)
public interface MetricPlotProxy extends MetricProxy {

    /**
     * UI Name of the object.
     */
    public String NAME = "Static Plot";

    public String getPlotUrl();
}

这是一个组合方法,因为我通常总是返回可能包含指标列表的复合类。话虽如此,这将返回基本类型的指标,然后我可以投射它们。

@ExtraTypes({ MetricProxy.class, MetricNumberProxy.class, MetricDoubleProxy.class, MetricIntegerProxy.class})
@Service(value = AnalyticsOperationDao.class, locator = DaoServiceLocator.class)
public interface AnalyticsOperationRequest extends DaoRequest<AnalyticsOperationProxy> {

    Request<List<<MetricProxy>> getSomeMetrics();

}

不是我使用的确切方法,但可用于获取类型的代理。

context.getSomeMetrics().with(MetricNumber.PROPERTIES).fire(new Receiver<List<MetricProxy>>() {

  public void onSuccess(List<MetricProxy> metrics) {
      for (MetricProxy metric : metrics) {
          if (metric instanceof MetricDoubleProxy) {
              logger.info("Got a class of double " + metric.getValue());
          }
      }          
  }
}

当您收到上述错误时,您将知道是否缺少@ExtraTypes注释。

希望有所帮助