1

Is there a built-in method - or can someone point me to a good pattern - for determining whether an object that implements Serializable is actually serializable at construction time? I need something that doesn't have side effects, so I can't just call the methods in my application server that actually serialize the objects.

I'd prefer something that I can take advantage of in the constructor without changing the constructor signature. However, I don't mind building a factory for these objects if that would make it easier.

EDIT

According to this question's accepted answer it looks like I can just create my own ObjectOutputStream -> ObjectInputStream pipe to test serializability of objects at creation time.

Community
  • 1
  • 1
Matt Mills
  • 8,692
  • 6
  • 40
  • 64
  • Why would (normal) serialization cause side-effects? (New objects might be created, but such environment-detail side-effects are overlooked even in functional programming.) If side-effects *do* occur, it sounds like that should be addressed ... then it's just a matter of catching the exception. –  May 01 '12 at 16:23
  • How is a unit test going to show me that whatever implementation of an interface, or subclass of a superclass that happens to have been passed in and held on to by reference is serializable? – Matt Mills May 01 '12 at 16:24
  • @pst - in my case, the only time these objects are currently being serialized is when they're sent across the wire, which may either not even be wired up when the object is created, or is likely to cause side effects when the message is received. – Matt Mills May 01 '12 at 16:25
  • @arootbeer If serialization fails, how are the messages sent? –  May 01 '12 at 16:26
  • @pst - They're not, but other things may have been done in the local environment in the interim. In this particular case, I'm dealing with distributed cache keys, so if serialization fails the remote caches are not invalidated. – Matt Mills May 01 '12 at 16:27
  • So what does the object need to have (besides implementing the `Serializable` interface) to fit your requirements? – Daniel Gabriel May 01 '12 at 20:43
  • @DanielGabriel - I'm specifically looking for a generalized test; I don't want it tied to any other requirement. – Matt Mills May 01 '12 at 21:26
  • Can you elaborate on what kind of test? Because the fact that it implements `Serializable` already makes it serializable - but you are saying that's not enough. – Daniel Gabriel May 02 '12 at 13:47

0 Answers0