Is it possible to customise the code for a JAXWS class. What I want to do is add some helper methods to the generated class to help extract information from it. I know I can subclass the generated source but this means I have to cast it everywhere I want the info.
Is it possible to have jaxws (or another tool) merge together the generated source code from the WSDL with some custom code containing my methods? (In C# I could do this with a partial class but java doesn't appear to have an equivalent)...
One approach to achieve this would be to introduce those helper methods via aspects. AspectJ would be a potential candidate for this, or if you're using Spring, then Spring AOP could also do the job.
So using AspectJ, you'd leverage the feature called "inter-type declaration", which allows one to introduce new features into existing classes. If your generated JAXWS class is called MyTypeGen
, then you'd introduce new methods using an aspect (arbitrarily named MyTypeAspect
) like this:
// generated class
class MyTypeGen {
int x, y;
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
// introduce two new methods into MyTypeGen
aspect MyTypeAspect {
public int MyTypeGen.multiply() {
return x * y;
}
public int MyTypeGen.sum() {
return x + y;
}
}
At runtime, you can call those two new methods which have been weaved into MyTypeGen
at build-time. No casting needed.
MyTypeGen mytype = new MyTypeGen();
mytype.setX(3);
mytype.setY(4);
int product = mytype.multiply();
// product = 12
If you decide to use Spring AOP instead, you'd create a normal Java class with the @Aspect
annotation, in which you'd leverage the introductions feature.