2

问题

有没有办法使用 AutoBean 框架反序列化 JSON,以便生成的 bean 具有影响其一个或多个成员的类型的类型参数?

背景

带有 JSON 结果的 RPC

我正在使用 GWT ( RequestBuilder) 来执行 RPC 请求。返回的 JSON 有效负载具有以下形式:

{
  "resultSet": [{...}, {...}, ...], // items requested; say, items 150-160
  "totalCount": 15330               // total matching items in DB
}

对象的resultSet类型取决于我调用的特定 RPC。

AutoBean 接口

我想使用 AutoBean 反序列化这个 JSON。我试图表示这个对象如下:

interface RpcResults<T> {

  List<T> getResultSet();
  void setResultSet(List<T> resultSet);

  int getTotalCount();
  void setTotalCount(int totalCount);

}

我还创建了适当的接口来表示可能存在于resultSet. 最后,我设置了对AutoBeanCodex.decode.

运行代码

尝试在开发模式下运行此代码会导致控制台中出现以下堆栈跟踪:

19:44:23.791 [ERROR] [xcbackend] Uncaught exception escaped
java.lang.IllegalArgumentException: The AutoBeanFactory cannot create a java.lang.Object
    at com.google.gwt.autobean.shared.AutoBeanCodex$Decoder.push(AutoBeanCodex.java:240)
    at com.google.gwt.autobean.shared.AutoBeanCodex$Decoder.decode(AutoBeanCodex.java:50)
    at com.google.gwt.autobean.shared.AutoBeanCodex$Decoder.visitCollectionProperty(AutoBeanCodex.java:83)
    at com.citrix.xenclient.backend.client.json.RpcResultsAutoBean.traverseProperties(RpcResultsAutoBean.java:100)
    at com.google.gwt.autobean.shared.impl.AbstractAutoBean.traverse(AbstractAutoBean.java:153)
    at com.google.gwt.autobean.shared.impl.AbstractAutoBean.accept(AbstractAutoBean.java:112)
    at com.google.gwt.autobean.shared.AutoBeanCodex$Decoder.decode(AutoBeanCodex.java:51)
    at com.google.gwt.autobean.shared.AutoBeanCodex.decode(AutoBeanCodex.java:505)
    at com.google.gwt.autobean.shared.AutoBeanCodex.decode(AutoBeanCodex.java:521)
    at com.citrix.xenclient.backend.client.services.JSONResponseResultSetHandler.onResponseReceived(JSONResponseResultSetHandler.java:51)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:126)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:214)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:281)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:531)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
    at java.lang.Thread.run(Thread.java:636)

基于此堆栈跟踪,我的预感如下:

  1. 类型擦除使它看起来RpcResults.getResultSet()返回一个 raw List
  2. AutoBean 反序列化器尝试ObjectresultSet.
  3. 失败

再次提问

我是否在 AutoBean API 中遗漏了一些可以让我轻松做到这一点的东西?如果没有,我应该研究一个明显的攻击点吗?对于我正在做的事情(除了我已经在使用的 JSONParser 和 JavaScriptObject 之外),是否有更明智的选择?

4

2 回答 2

2

由于 Java 类型擦除,这并不简单。该类型T在运行时不存在,已被擦除以Object代替任何其他下限。AutoBeanCodex 需要类型信息才能具体化传入 json 有效负载的元素。这种类型信息通常由 AutoBean 实现提供,但由于T擦除,它只知道它包含一个List<Object>.

如果您可以在运行时提供类文字,则可以将 getter 声明为Splittable getResultSet()并通过调用来具体化列表的各个元素AutoBeanCodex.decode(autoBeanFactory, SomeInterfaceType.class, getResultSet().get(index))。通过使用Category,您可以<T> T getResultAs(Class<T> clazz, int index)向 AutoBean 接口添加方法。这看起来像:

@Category(MyCategory.class)
interface MyFactory extends AutoBeanFactory {
  AutoBean<ResultContainer> resultContainer();
}
interface ResultContainer<T> {
  Splittable getResultSet();
  // It's the class literal that makes it work
  T getResultAs(Class<T> clazz, int index);
}
class MyCategory {
  public static <T> T getResultAs(Autobean<ResultContainer> bean,
      Class<T> clazz, int index) {
    return AutoBeanCodex.decode(bean.getFactory(), clazz,
      bean.as().getResultSet().get(index)).as();
  }
}
于 2011-03-15T13:21:34.097 回答
1

尝试在特定于对象的接口中覆盖 .getResultSet() 和 .setResultSet() 方法:

interface FooRpcResults extends RpcResults<Foo> {
    @Override
    List<Foo> getResultSet();
    @Override
    void setResultSet(List<Foo> value);
}

以下测试适用于我(GWT 2.3.0):

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;
import com.google.web.bindery.autobean.vm.AutoBeanFactorySource;

public class AutoBeanTest {
 public static interface Page<T> {
  int getDataSize();

  List<T> getPage();

  int getStartIndex();

  void setDataSize(int value);

  void setPage(List<T> value);

  void setStartIndex(int value);
 }

 public static interface Thing {
  String getName();

  void setName(String value);
 }

 public static interface ThingFactory extends AutoBeanFactory {
  AutoBean<Thing> createThing();

  AutoBean<ThingPage> createThingPage();
 }

 public static interface ThingPage extends Page<Thing> {
  @Override
  List<Thing> getPage();

  @Override
  void setPage(List<Thing> value);
 }

 @Test
 public void testAutoBean() {
  final ThingFactory factory = AutoBeanFactorySource
    .create(ThingFactory.class);

  final Thing thing1 = factory.createThing().as();
  thing1.setName("One");

  final Thing thing2 = factory.createThing().as();
  thing2.setName("Two");

  final List<Thing> things = new ArrayList<Thing>();
  things.add(thing1);
  things.add(thing2);

  final Page<Thing> page = factory.createThingPage().as();
  page.setStartIndex(50);
  page.setDataSize(1000);
  page.setPage(things);

  final String json = AutoBeanCodex.encode(
    AutoBeanUtils.getAutoBean(page)).getPayload();

  final Page<Thing> receivedPage = AutoBeanCodex.decode(factory,
    ThingPage.class, json).as();

  assertEquals(receivedPage.getStartIndex(), page.getStartIndex());
  assertEquals(receivedPage.getDataSize(), page.getDataSize());
  assertNotNull(receivedPage.getPage());
  assertEquals(receivedPage.getPage().size(), page.getPage().size());
  for (int i = 0; i < receivedPage.getPage().size(); i++) {
   assertNotNull(receivedPage.getPage().get(i));
   assertEquals(receivedPage.getPage().get(i).getName(), page
     .getPage().get(i).getName());
  }
 }
}

删除 ThingPage 界面中的覆盖会破坏它。

于 2011-05-04T19:53:02.143 回答