JAXB constructor injection
Asked Answered
K

3

6

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?

Kip answered 23/7, 2010 at 8:56 Comment(4)
Please give us examples, including the sort of schema components you're talking about, and how you want them represented in java.Vinous
original post edited. is it possible?Kip
I don't think it is, no. To my knowledge, constructor injection was never added to JAXB2, although I think it was planned at one point.Vinous
Kohsuke explained why its not possible here: weblogs.java.net/blog/kohsuke/archive/2005/02/… . Personally I think this is Bull$%#$ when everone already uses Unsafe anyway. Also there is a JDK annotation for constructor injection: docs.oracle.com/javase/6/docs/api/java/beans/…Quid
K
0

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

Kip answered 28/7, 2010 at 15:21 Comment(0)
R
0

There isn't a standard JAXB way to generate a class that looks like that, because the JAXB spec doesn't support mapping to a class like that.

You could use the following code to avoid having a set method. You can add additional constructors if you want as long as there is a public default one.

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.FIELD)
public class mycomplex    
{    
        protected final boolean myboolean;    

        public boolean getMyboolean() { return myboolean; }    
}    

If you want to use the class that you described above with a non-default constructor and a field marked final you will need to create an XmlAdapter:

Remuneration answered 26/7, 2010 at 17:57 Comment(3)
Thank you very much for this but it is not entirely what I am looking for. I will be unmarshalling hundreds of classes, meaning that doing something like this is going to be quite tedious as I have to define everything myself and these classes will all vary from time to time --> very time consuming if I have to re-define things all the time. Is there a way of accommodating this type of behavior in a more automated process across hundreds of different classes? Thanks again.Kip
@BlaiseDoughan any chance you'll add docs.oracle.com/javase/6/docs/api/java/beans/… to your Moxy?Quid
@AdamGent - Would you mind providing this as a comment on the following enhancement request?: bugs.eclipse.org/328951Remuneration
K
0

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

Kip answered 28/7, 2010 at 15:21 Comment(0)
K
0

I have similar problem and I found the following library: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#immutable

And everything seems so simple now ;)

Kesley answered 18/8, 2010 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.