开发者

Spring/JUnit serialization of double brace initialized ArrayList in Java

While writing a simple remoting test I've come up against a suprising situation involving DBI (double braces initialization) which I've been unable to fully understand, so I would ask for some assistance.

Consider the following code:

public class RemotingTest {

@Autowired
private RemotingTestController remotingTestController;

//DBI
List<SomeBean> mockList = new ArrayList<SomeBean>(){{
        add(MockFactoryBean.getMockBean());
}};

@Test
public void testRemoting() {
    try {
        // controller code is not relevant - it simply passes this list 
        // via Spring's HttpInvokerProxyFactoryBean to a session facade which then 
        // passes the list further down the SOA stack...
        String result = remotingTestController.createBeanRemotely(mockList); 
        log.debug(result);
    } catch (Exception e) {
        fail();
        e.printStackTrace();
    }
}

}

This code bombs in run-time with the following error which makes no sense to me:

java.io.NotSerializableException: org.stackoverflow.RemotingIntegrationTest
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
  at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
  <<stacktrace snipped>>
  at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:174)
  at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:142)
... 33 more

If, however I am to simply omit DBI and use vanilla-style pattern to add an element to a list, as in:

    public class RemotingTest {

@Autowired
private RemotingTestController remotingTestController;

List<SomeBean> mockList = new ArrayList<SomeBean>();

@Test
public void testRemoting() {
    mockList.add(MockFactoryBean.getEcopStatusMock());

    try {
    //this block stays the same
        } catch (Exception e) {
        //.....
    }
}

}

everything works properly and serializes without a hitch. I've even tried to make the test Serializable (shudders) but that yielded exactly nothing, as the test died with a even more interesting error - that the HttpInvokerServiceExporter should implement Serializable :)

The question is - why is that so? Researching DBI a bit led me to believe that by addin开发者_开发百科g an element to a list this way actually made two objects - the expected ArrayList<T>() and a new subclassed ArrayList<T>() object which contained the added element; this somehow confuses Spring's Remoting and dies a gruesome death with a NotSerializableException.

I am not sure, however, if that is what happens behind the scene, so any help in explaining this would be appreciated.

The environment uses the following:

  • Spring 3.0.4
  • JDK 1.6.0 Update 23
  • IceFaces 1.8.2


This is happening because the double-brace initialization syntax creates an anonymous subclass of ArrayList, and like all anonymous classes, it contains an implicit reference to their parent object (in this case, your test).

In order to be serializable, the parent object (i.e. your test) would also have to be serializable. This is obviously not what you want here. You'll just have to avoid that syntax, convenient thought it is.

An equally convenient alternative for lists is:

List<SomeBean> mockList = Arrays.asList(
    bean1, bean2, bean3
);

The DBI syntax is only really compelling when building Maps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜