I have a method which I wrote like this:
void updateMyJaxb(final JAXBElement<?> jaxbElement) {
addElementtoJaxb(jaxbElement)
}
void addElementTJaxb(jaxbElement)
{
//have to cast to myown type
((JAXBElement<MyType>) jaxbElement).getValue().setSomeValue(somevalue);
}
so in essence I add some staff to JAXBElement.
The question is : is it a good design, take a reference and change the content, ?
Will it be better to return the reference to a updated object?
Otherwise the method may not be straightforward to understand ?
Setters should not have any effects on the parameter they take directly.
Personally I would consider that as bad design.
If you stil want to do that I would strongly recommend you to document this behavior, because the behavior might be unexpected.
Returning the objects is also not required, since Java passes parameters (objects in general) by reference.
I would use the Adapter pattern, which takes an JAXBElement as a parameter and returns a JAXBElement.
Instead of updating the JAXBElement the adapter clones it and changes the clone and returns it.
This way no unexpected side-effect are introduced.
This pattern is also called defensive-copying.
This should do the job.