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?

4
  • Please give us examples, including the sort of schema components you're talking about, and how you want them represented in java. Commented Jul 23, 2010 at 8:57
  • original post edited. is it possible? Commented Jul 23, 2010 at 9:17
  • 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. Commented Jul 23, 2010 at 16:51
  • 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/… Commented Oct 24, 2012 at 16:30

3 Answers 3

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:

Sign up to request clarification or add additional context in comments.

3 Comments

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.
@BlaiseDoughan any chance you'll add docs.oracle.com/javase/6/docs/api/java/beans/… to your Moxy?
@AdamGent - Would you mind providing this as a comment on the following enhancement request?: bugs.eclipse.org/328951
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

Comments

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.