javajava-8jaxbwadl

Need to add a method to JAXB class. How? Or should I?


I use JAXB to generate POJOs from a WADL xsd. I downloaded the xsd directly from W3C. However, I'd like one of my POJOs to have a helper method that allows me to recurse through it, like so:

public Stream<WadlResource> flattenPath() {
    return Stream.concat(
        Stream.of(this), 
        this.methodOrResource.stream()
                .filter(WadlResource.class::isInstance)
                .map(WadlResource.class::cast)
                .flatMap(WadlResource::flattenPath) // recursion here
            );
}

Notice the use of this. This method is the only way I've seen of recursing with a stream.

So my question is: How do I add this method so that it is generated along with the xsd? Is that good practice? Or should I just generate the code once, add the method, and check it into my repo (which DOESN'T seem like good practice). TIA!


Solution

  • Is that good practice?

    This is subjective. I normally do not do this. I prefer to have the schema-derived classes as simple DTO without any business logic.

    How do I add this method so that it is generated along with the xsd?

    You can inject code with the Code Injector Plugin. Please see the following question:

    Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "