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.
private JAXBElement clone(JAXBElement o) {
JAXBElement clone = new JAXBElement();
clone.setName(o.getName());
...
return clone;
}
This should do the job.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.