1

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!

CNDyson
  • 1,687
  • 7
  • 28
  • 63

1 Answers1

1

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 "

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks, but do you know how I would rewrite me recursive method in a utility class? – CNDyson Mar 07 '18 at 18:20
  • @user1660256 You recursively call the method of the utility class. This is a different question, please ask it separately. – lexicore Mar 07 '18 at 18:24