0

I have a schema where I would like to customize the resulting getters from its unmarshalling.

What kind of custom binding would allow me to do that? An example would be a getter where I initialize the object with some default values before returning it, in the case that the object is NULL.

eg

public smapleType getSampleObject() { (if sampleObject == NULL) sampleObject.setField(0); return sampleObject; }

Is there a way to get JAXB to generate a customized getter like that?

Justin A
  • 3,891
  • 2
  • 18
  • 22

2 Answers2

1

You can do this by creating subclasses of your domain classes and ObjectFactory - more details here https://jaxb.java.net/guide/Adding_behaviors.html

Tomasz W
  • 2,263
  • 1
  • 17
  • 22
  • Provided link is broken, check [this one](https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#compiling-xml-schema-adding-behaviors) – Victor Jul 16 '18 at 06:48
0

Since you name this NULL thingy an example, I assume you're interested in a more general approach.

As cases like in your example are very specific, there's no universal customization for that. The closed would probably be the -Xinject-code plugin.

If you want deep customization of the generated code, then a hardcore way of writing an XJC plugin is probably the only one generic enough. But it's not quite easy.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221