Re: [Simple-support] Constructor injection problems
Brought to you by:
niallg
|
From: Simon B. <sim...@if...> - 2012-06-25 19:41:13
|
Ndrew,
Why do you serialize the derived fields like "numberSquared"? You could just
omit the annotations for these fields, then the serialization would not
contain the fields and the constructor could be as you wish.
Best Regards
Simon
Von: Andrew Spinner [mailto:and...@ti...]
Gesendet: Montag, 25. Juni 2012 18:28
An: sim...@li...
Betreff: [Simple-support] Constructor injection problems
Hello, and thanks for this lightweight framework,
I'm having some problems with constructor injection for my immutable class.
My instance variables are all final, and are all serialised by Simple.
However, I don't need all the fields to be deserialised - some of the fields
are derived, being based on mathematical calculations of the other fields.
These derived variables are set in the constructor, based on the parameters
passed to the constructor.
It doesn't make logical sense to pass values of these derived variables into
the constructor, because they're not needed, and will be discarded. However,
I've got to do so, or I'll get a ConstructorException, saying "no
constructor accepts all read only values."
This isn't my production code, just an example:
public class calc
{
@Element(name="number")
private final int number;
@Element(name="numberSquared")
private final int numberSquared;
//extra constructor I have to provide
public Calc(@Element(name="number") int number,
@Element(name="numberSquared") int numberSquared)
{
this(number); //ignores numberSquared parameter, letting chained constructor
calculate value for itself
}
//usual constructor
Calc(int number)
{
this.number = number;
numberSquared = number * number; //ignoring parameter and calculating value
for itself
}
Unfortunately, even when I create this extra constructor, I still get a
PersistenceException: "Constructor not matched for class." As far as I can
see, I've annotated the constructor correctly, providing a parameter for
every possible field.
Short of me providing my actual code, is there anything I could look at that
might fix it?
As a suggestion, it might be useful for PersistenceException to provided
more specific information about how the constructor isn't matching.
Thanks,
Andrew
|