How to add custom code to JAXWS source code

Is it possible to customize the code for the JAXWS class. I want to add some helper methods to the generated class in order to extract information from it. I know that I can subclass the generated source, but that means I have to use it wherever I want information.

Is it possible for jaxws (or another tool) to combine the generated source code from WSDL with some user code containing my methods? (In C #, I could do this with a partial class, but java has no equivalent) ...

+4
source share
2 answers

One approach to achieve this would be to introduce these auxiliary methods through aspects. AspectJ will be a potential candidate for this, or if you use Spring, then Spring AOP can also do the job.

Thus, using AspectJ, you would use the "inter-type declaration" function, which allows you to introduce new functions into existing classes. If your generated JAXWS class is called MyTypeGen, you must introduce new methods using the aspect (optionally named MyTypeAspect) as follows:

// 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 run time, you can invoke these two new methods that were woven at MyTypeGenbuild time. No casting required.

MyTypeGen mytype = new MyTypeGen();
mytype.setX(3);
mytype.setY(4);
int product = mytype.multiply();
// product = 12

Spring AOP , Java- @Aspect, introductions.

+4

, , , .

, , :

public GeneratedClass {

    public void doThis();

}

public ImprovedClass extends GeneratedClass {

    public void doExtraStuff();

}

, , GeneratedClass. GeneratedClass, , , , . , , . . . , .

, - , , -, -, , . , -, .

, - , .

public GeneratedClass implements SomeInterface {

    public void doThis();

}

public ImprovedClass implements SomeInterface {

    public SomeInterface impl;

    public Improvedclass(SomeInterface impl) {
        this.impl = impl;
    }

    public void doThis() {
        this.impl.doThis();
    }

    public void doExtraStuff() {
        // the extra stuff
    }

}
+1

Source: https://habr.com/ru/post/1628237/


All Articles