3 Replies Latest reply: Nov 6, 2011 4:25 AM by se*328623*78 RSS

    JAXB factory method - for multiple objects

      looking at the documentation i see that the only option for using a factory method in order to create an object is providing an objectFactory class and method. However this method is with no-args.
      is there a way to work around it?

      The reason i'm asking is because i already have a factory for the items in my project, which has a single "create" method that recieves a class name and return a new instance of that class (with some wiring). I cannot use this factory since i need to pass the class name as an argument, which is something i cannot do in JAXB.
      i want to refrain from creating a method for each different item and use this method in @XmlType since i have hundreds items like this.

      Any idea?

        • 1. Re: JAXB factory method - for multiple objects
          you should be able to pretty easily auto-generate the factory class. just create the template factory class:
          public class MyFactory {
            // auto-gen these methods for all instances of "Foo"
            public static Foo createFoo() {
              return create(Foo.class);
            public static <T> T create(Class<T> clazz) {
              // do your custom magic here
          then, create a simple input-file with the jaxb class names (or use some filesystem or reflection magic to find them at compile time) and create a little pre-build step which spits out the factory class for you.
          • 2. Re: JAXB factory method - for multiple objects
            Thanks for your reply!

            I thought about auto-generation, however it will require to go over all the classes and set the exact factory method in @XmlType.
            Also, it would requires everyone how write new class to set the factory method for it.
            Since there are lots of objects, and i'm afraid ppl will forget to put factory-method, i'm looking for something which would be more transparent (if possible).

            Thanks again!
            • 3. Re: JAXB factory method - for multiple objects
              I have exactly the same problem. Did you manage to find a solution ? if so please publish it !.