GWT多态列表与@ExtraTypes [英] GWT polymorphic lists with @ExtraTypes

查看:73
本文介绍了GWT多态列表与@ExtraTypes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表包含不同类型的元素有点问题,我想看看你之前是否有人遇到过这个问题。这个问题应该使用@ExtraTypes解决,但它不适合我,所以我想我没有正确使用它。所以,情况是(为了清楚起见,bean名称被改变了):

概述:




  • 使用GWT 2.5与RequestFactory。



SERVER-SIDE:




  • 我有一个RootBean,其中包含

     List< ChildBean> 


  • 这个ChildBean包含一些原始属性。

  • ChildBean也由一个继承所有父属性并添加更多内容的MoreSpecificChildBean扩展。根据一些逻辑,RootBean根据类型为ChildBean和MoreSpecificChildBean的元素获取列表。



CLIENT-SIDE:




  • IRootBeanProxy是一个带有以下注解的ValueProxy:

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




并包含列表



列表< IChi ldBeanProxy> getChildren(); 




  • IChildBeanProxy是一个ValueProxy:

     
    @ProxyFor(value = ChildBean)
    public interface IChildBeanProxy extends ValueProxy


  • IMoreSpecificChildBeanProxy是一个ValueProxy:

     
    @ProxyFor(value = MoreSpecificChildBean)
    public interface IMoreSpecificChildBeanProxy extends IChildBeanProxy


  • 请求上下文有一个返回Request的方法,我也在这里添加了@ExtraTypes注释:

     
    @Service(value = CompareService.class,locator = SpringServiceLocator.class)
    @ ExtraTypes({IChildBeanProxy.class,IMoreSpecificChildBeanProxy.class})
    public interface ICompareRequestContext extends RequestContext {
    Request< IRootBeanProxy>比较(Integer id1,Integer id2);




问题



假设有了这些注释,RF应该知道多态继承类的存在,但我在客户端获得的是一个IRootBeanProxy,其中包含IChildBeanProxy元素的列表。此列表包含MoreSpecificChildBean,但是采用IChildBeanProxy的形式,所以我无法从其他人那里得知它。
所以我想知道我做错了什么,如果我在错误的地方或其他地方设置了ExtraTypes注释。



任何人?



所有的帮助!!

解决方案

几个类,但它总是会返回我可以迭代的基本类型,并在需要时测试instanceof。您可能必须将对象投射到子类。如果您不添加@ExtraTypes,您将会知道,因为在服务器端,您将收到一条消息,指出MoreSpecificChildBean无法发送到客户端。



我只注释了服务而不是代理,我遇到了一些怪异的情况,并在代理中添加了@ExtraTypes。

  / ** 
*所有其他指标代理扩展的基本代理。它主要用于
*继承RequestFactory。具体实现是
* {@link MetricNumber}。
*
* @author chinshaw
* /
@ProxyFor(value = Metric.class,locator = IMetricEntityLocator.class)
public interface MetricProxy extends DatastoreObjectProxy {


/ **
*此对象的名称在ui中。这通常会被
*子类扩展。
* /
public String NAME =Generic Metric;

/ **
*这是ui可以支持的输出类型列表。这是
*通常用于在操作
*输出屏幕中列出支持的度量标准类型。
*
* @author chinshaw
* /
public enum MetricOutputType {
MetricNumber,MetricString,MetricCollection,MetricStaticChart,MetricDynamicChart
}

/ **
*请参阅{@link MetricNumber#setName(String)}
*
* @param name
* /
public void setName(String名称);

/ **
*请参阅MetricNumber#setContext(String)}
*
* @return指标的名称。
* /
public String getName();


/ **
*获取附加到该指标的违规列表。
*
* @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 {
$ b $ / * *用于子句的对象* /
public static String [] PROPERTIES = {ranges};

public Double getValue();

...

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

/ **
* UI对象的名称。
* /
public String NAME =Static Plot;

public String getPlotUrl();



$ b $ p
$ b这是一种编制方法,因为我通常总是返回复合类,包含一系列指标。这就是说,这将返回我基础类型的指标,然后我可以投它们。

$ p $ @ExtraTypes({MetricProxy。类,MetricNumberProxy.class,MetricDoubleProxy.class,MetricIntegerProxy.class})
@Service(value = AnalyticsOperationDao.class,locator = DaoServiceLocator.class)
public interface AnalyticsOperationRequest扩展DaoRequest< AnalyticsOperationProxy> {

请求<列表<< MetricProxy>> getSomeMetrics();

}



不是一个确切的方法I使用,但将用于获取类型的代理。

  context.getSomeMetrics()。with(MetricNumber.PROPERTIES).fire (MetricProxy度量标准){
if(metric){

$ b public void onSuccess(List< MetricProxy> metrics){
(MetricDoubleProxy){
logger.info(有一双double+ metric.getValue());
}
}
}
}

您会知道在出现上述错误时是否缺少@ExtraTypes注释。



希望有帮助


I have a little problem with a list that contains different types of elements and i would like to see if anyone of you have met the problem before. The issue should be solved with the use of @ExtraTypes, but it is not working for me, so i guess i am not using it correctly. So, the scenario is (bean names are changed for clarity):

GENERAL:

  • I am using GWT 2.5 with RequestFactory.

SERVER-SIDE:

  • I have a RootBean that contains, among other stuff, a

    List <ChildBean>

    .

  • This ChildBean contains some primitive attributes.
  • ChildBean is also extended by a MoreSpecificChildBean that inherits all the parent attributes and adds a few more.
  • The RootBean gets its list filled up with elements of type ChildBean and MoreSpecificChildBean depending on some logic.

CLIENT-SIDE:

  • IRootBeanProxy is a ValueProxy with these annotations:

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

and contains a list

List <IChildBeanProxy> getChildren ();

  • IChildBeanProxy is a ValueProxy:

    @ProxyFor (value=ChildBean)
    public interface IChildBeanProxy extends ValueProxy
    

  • IMoreSpecificChildBeanProxy is a ValueProxy:

    @ProxyFor (value=MoreSpecificChildBean)
    public interface IMoreSpecificChildBeanProxy extends IChildBeanProxy
    

  • the Request context has a method that returns Request and i added the @ExtraTypes annotation here too:

    @Service (value = CompareService.class, locator = SpringServiceLocator.class)
    @ExtraTypes ({IChildBeanProxy.class, IMoreSpecificChildBeanProxy.class})
    public interface ICompareRequestContext extends RequestContext {
       Request <IRootBeanProxy> compare (Integer id1, Integer id2);
    

Question

Supposedly with those annotations, RF should be aware of the existence of polymorphic inherited classes, but all i get in the client is an IRootBeanProxy with a list of IChildBeanProxy elements. This list includes the MoreSpecificChildBean, but in the shape of a IChildBeanProxy, so that i cannot tell it from the others. So i am wondering what i am doing wrong, if i am setting the ExtraTypes annotation at the wrong place or something.

Anyone?

Thx for all the help!!

解决方案

I do the exact same thing for quite a few classes but it will always return me the base type which I can iterate through and test for instanceof if needed. You will probably have to cast the object to the subclass. If you do not add the @ExtraTypes you will know because on the server side you will get a message stating that MoreSpecificChildBean cannot be sent to the client.

I only annotate the service and not the proxy, I ran into some quirks with 2.4 adding @ExtraTypes to the proxy.

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

This is a made up method from because I usually always return composite classes that may contain a list of metrics. That being said this will return me the base type of metrics, and then I can cast them.

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

}

Not an exact method I use but will work for getting a proxy of type.

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

You will know if you are missing an @ExtraTypes annotation when you get the error stated above.

Hope that helps

这篇关于GWT多态列表与@ExtraTypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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