Search code examples
javajaxbcustomizationconstructor-injection

JAXB constructor injection


I would like to know how I can make the JAXB compiler make certain elements in my XML schema be declared as final in the java class definition and I would also like to be able to control the different constructors, as in I would like a constructor that could create the object with a full list of parameters contained in the class as well as the default private constructor needed by JAXB.

Any help?

Thanks.

Here is an example of what I am trying to do:

<xs:complexType name="mycomplex">
    <xs:all>
        <xs:element name="myboolean" type="xs:boolean"/>
    </xs:all>
</xs:complexType>

now the generated code will look something like

public class mycomplex
{
        protected boolean myboolean;

        public boolean getMyboolean() { return myboolean; }
        public void setMyboolean(boolean b) { this.myboolean = b; }
}

but I would like to edit the schema to make it look like:

public class mycomplex
{
        protected final boolean myboolean;

        public mycomplex(boolean b) { this.myboolean = b; }

        public boolean getMyboolean() { return myboolean; }
}

Can this be achieved?


Solution

  • I have resolved this in another way. I am creating a tool that will edit the java source code files produced and will add in/remove what I need. Refer to this for more information: Using the Eclipse AST