Re: [Simple-support] Using constructors with arguments
Brought to you by:
niallg
|
From: <Nia...@ub...> - 2009-01-22 16:45:46
|
Hi,
There is no need to do this. You can do the following.
@Root
public class Tuple {
final private String from;
final private String to;
public Tuple(String from, String to){
this.from = from;
this.to = to;
}
@Replace
private TupleSubstitute getSubstitute() {
return new TupleSubstitute(from, to);
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public String toString() {
return from + " -> " + to;
}
}
@Root
private class TupleSubstitute {
@Element
private String from;
@Element
private String to;
public TupleSubstitute() {
super();
}
public TupleSubstitute(String from, String to) {
this.from = from;
this.to = to;
}
@Resolve
public Tuple getTuple() {
return new Tuple();
}
}
This should work, for you. The @Resolve annotation resolves the
deserialized object before assigning it to the field or method. The
@Replace object replaces the object in the stream. Just like java object
serialization. Let me know how this works out for you.
Hope this helps,
Niall
-----Original Message-----
From: Kevin Day [mailto:for...@tr...]
Sent: 21 January 2009 22:56
To: Simple (Java Project)
Subject: Re: [Simple-support] Using constructors with arguments
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
Visit our website at http://www.ubs.com
This message contains confidential information and is intended only
for the individual named. If you are not the named addressee you
should not disseminate, distribute or copy this e-mail. Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.
E-mails are not encrypted and cannot be guaranteed to be secure or
error-free as information could be intercepted, corrupted, lost,
destroyed, arrive late or incomplete, or contain viruses. The sender
therefore does not accept liability for any errors or omissions in the
contents of this message which arise as a result of e-mail transmission.
If verification is required please request a hard-copy version. This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities
or related financial instruments.
UBS Limited is a company registered in England & Wales under company
number 2035362, whose registered office is at 1 Finsbury Avenue,
London, EC2M 2PP, United Kingdom.
UBS AG (London Branch) is registered as a branch of a foreign company
under number BR004507, whose registered office is at
1 Finsbury Avenue, London, EC2M 2PP, United Kingdom.
UBS Clearing and Execution Services Limited is a company registered
in England & Wales under company number 03123037, whose registered
office is at 1 Finsbury Avenue, London, EC2M 2PP, United Kingdom.
|