Re: [Simple-support] Using constructors with arguments
Brought to you by:
niallg
|
From: Kevin D. <for...@tr...> - 2009-01-21 22:56:05
|
I've done some additional work on handling classes that do not have zero-arg constructors. First, I've put together some simple sample code (attached, start with TryIt#main) that shows one of the situations we are facing: we have a number of immutable Tuple objects that we need to serialize/deserialze. The values used in the Tuple class' constructor come from attributes (or elements) from the XML. The attached serializes fine, but we can't deserialize it b/c of the lack of no-argument constructor. We can't add a no-arg constructor because the class is intended to be immutable (in fact, the fields are marked as final). In addition, the sample code shows the Tuple values as holding simple strings - but in our application, the values are going to be complex objects.
I have put together a Strategy and Type implementation that *kind* of works, but I'm running into some issues (may be related to my lack of familiarity with the code base).
The approach that I'm working on now creates a new Strategy that allows us to register special Type objects that can handle object instantiation based on state of the deserialization process (for now, I'm calling this FactoryGeneratedStrategy). Ideally, FactoryGeneratedStrategy would be able to inter-operate with any other Strategy (DefaultStrategy, CycleStrategy, TreeStrategy) - after all, it's only purpose is to help instantiate objects during deserialization - the behavior of the other strategies is still desirable.
I ran into a couple of problems with this type of Strategy chaining approach:
1. DefaultStrategy is not instantiable due to package scope visibility (this is the smaller issue)
2. The other strategies appear to be implemented in a way that assumes use of zero-arg constructors. For example, CycleStrategy#getElement returns an Allocate object depending on the deserialization state. The Allocate object is ultimately backed by an Instance object as it's delegate Type. Instance assumes a zero-arg constructor.
Short of re-implementing all of the *Strategy classes, I can't see how to go about providing a factory to create new object instances (without losing functionality provided by various Strategy classes).
My initial impression based on the above is that Strategy may not be the appropriate place in the hierarchy to add object instantiation behavior. If not, where? The Instantiator class seems a likely candidate, except that it's getInstace() method doesn't have access to deserialization context information.
Maybe I'm just going about this all wrong... If anyone can provide some suggestions, I'd love to hear them!
Thanks,
- K
----------------------- Original Message -----------------------
From: Kevin Day <ke...@tr...>
To: Simple (Java Project) <sim...@li...>
Cc:
Date: Tue, 20 Jan 2009 17:28:05 -0700
Subject: [Simple-support] Using constructors with arguments
For starters, I really like what I see in this project (just started reviewing it this afternoon). I'm hoping that I can get some pointers on a specific use-case.
I would like to use Simple in conjunction with a dependency injection system (probably Guice) and List implementations from the GlazedLists project.
We have to use a constructor on our list implementations that contains arguments (some of the arguments provided by Guice, some from element or attribute data in the XML).
What is the best way to approach this with Simple?
It seems that some sort of Strategy mixed with a custom Type is in order, but I'm not entirely clear on the best approach. The architecture of the framework looks powerful enough to do what we need, but I can't see where to get started just yet.
To give a pseudo-code example of what we are trying to do (in real life, we'll be injecting SomeInjectedObject using Guice or a factory/service provider call):
class SpecialList<E>{
public SpecialList(SomeInjectedObject injected, String valueFromAttribute, SomeObject valueFromElement){
...
}
}
class MyObject{
@ElementList
SpecialList<ElementObject> list = new SpecialList<ElementObject>(SomeInjectedObject.defaultInstance, "test", new SomeObject());
}
When we serialize this with Simple, we'd like to see something approximately like the following:
<myObject valueFromAttribute="test">
<someObject> ... </someObject>
<list>
<elementObject> ... whatever goes into this ... </elementObject>
<elementObject> ... whatever goes into this ... </elementObject>
...
</list>
</myObject>
Note that:
1. The <list> element doesn't have a class attribute
2. When we construct the list during deserialization, we need to be able to pull the 'valueFromAttribute' and 'someObject' values to use during construction of the SpecialList object
3. The SpecialList object needs to have the 3 argument constructor called (not a zero-arg constructor). And it is not possible to call a zero arg constructor, then modify the object afterwards to get the same result.
Any pointers? I may be pushing the framework too hard (some of the above requirements can be relaxed - if it isn't possible to obtain valueFromAttribute or someObject at construction time, we can find workarounds - but we have to be able to create the SpecialList instance using values from a factory/Guice/etc...). For what it's worth, we have this type of behavior working in Betwixt using custom BeanCreator implementations.
Thanks much,
- Kevin
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support |