I've a proyect that call various webservices using an external library. This library give me objects like this:
public static class ObjA {
@XmlElement(name = "counter", required = true)
protected BigInteger counter;
@XmlElement(name = "data", required = true)
protected String data;
[...]
}
and this:
public static class ObjB {
@XmlElement(name = "counter", required = true)
protected BigInteger counter;
@XmlElement(name = "data", required = true)
protected String data;
[...]
}
as you can see objA and objB have same properties so, if I have to use both, I've to duplicate code:
public class myClass {
[...]
private ObjA a;
private ObjB b;
[...]
public void myClass() {
[...]
this.a = new ObjectFactory().createObjA();
this.b = new ObjectFactory().createObjB();
[...]
}
public void init() {
this.initA();
this.initB();
}
private void initA() {
this.a.setCounter(BigInteger.ZERO);
this.a.setData = "";
}
private void initB() {
this.b.setCounter(BigInteger.ZERO);
this.b.setData = "";
}
[...]
}
initA and initB are identical, I cannot access the library code so I can't create a common interface, in which way can I avoid duplicated code? I mean, it's possible to have something like this?
private void initObj([ObjA|ObjB] obj) {
obj.setCounter(BigInteger.ZERO);
obj.setData = "";
}
Thank you! Muchas Gracias!
addendum
Please note I have no access to the underlying library, so I can't add modify classes, interfaces, wsdl or xsd in any way. Also in my opinion is not important if I'm using a ws or not, jaxb or other library: you can imagine ObjA and ObjB without annotations, like this:
public static class ObjA {
protected BigInteger counter;
protected String data;
[...]
}
public static class ObjB {
protected BigInteger counter;
protected String data;
[...]
}
and the crux of the matter doesn't change.
ObjAand typeObjBno matter what you do.