Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6810167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:10:25+00:00 2026-05-26T20:10:25+00:00

According to the documentation JAXB factory methods do not have arguments. Is there a

  • 0

According to the documentation JAXB factory methods do not have arguments. Is there a JAXB implementation that allow me to create a factory method that receives as a parameter the class of the object I need to create ?
It happens that all my JAXB objects follow the same creation pattern (a particular byte code instrumentation), therefore I would like to encapsulate this in one single factory method having as a parameter the class of the JAXB object to create, avoiding in this way the creation of different factory methods for each JAXB class that basically do exactly the same thing.

I found someone asking the same question in an OTN forum: https://forums.oracle.com/forums/thread.jspa?messageID=9969927#9969927, but not a real answer has been proposed yet.

Thanks for any help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:10:26+00:00Added an answer on May 26, 2026 at 8:10 pm

    This is currently not possible using the standard JAXB APIs. I have entered the following enhancement request to have this behaviour added to EclipseLink JAXB (MOXy):

    • https://bugs.eclipse.org/363192

    MOXy Specific Solution

    You could leverage the @XmlCustomizer extension in EclipseLink JAXB (MOXy) to customize how the objects are instantiated. This mechanism is leveraged to tweak MOXy’s underlying metadata.

    CommonFactory

    import java.util.Date;
    
    public class CommonFactory {
    
        public static Object create(Class<?> clazz) {
            if(Foo.class == clazz) {
                return new Foo(new Date());
            } else if(Bar.class == clazz) {
                return new Bar(new Date());
            }
            return null;
        }
    
    }
    

    Foo.class

    The Foo class is annotated normally except that we will use the @XmlCustomizer annotation to specify a DescriptorCustomizer that we are going to use to tweak MOXy’s metadata.

    import java.util.Date;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
    
    @XmlRootElement
    @XmlType(factoryClass=CommonFactory.class, factoryMethod="create")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlCustomizer(FactoryCustomizer.class)
    public class Foo {
    
        private Date creationDate;
        private Bar bar;
    
        // Non-default constructor
        public Foo(Date creationDate) {
            this.creationDate = creationDate;
        }
    
    }
    

    Bar

    Again we will use the @XmlCustomizer annotation to reference the same DescriptorCustomizer that we did in the Foo class.

    import java.util.Date;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
    
    @XmlType(factoryClass=CommonFactory.class, factoryMethod="create")
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlCustomizer(FactoryCustomizer.class)
    public class Bar {
    
        private Date creationDate;
    
        // Non-default constructor
        public Bar(Date creationDate) {
            this.creationDate = creationDate;
        }
    
    }
    

    FactoryCustomizer

    MOXy has the concept of an InstantiationPolicy to build new objects. In this example we will swap in our own instance InstantiationPolicy that can use parameterized factory methods:

    import org.eclipse.persistence.config.DescriptorCustomizer;
    import org.eclipse.persistence.descriptors.ClassDescriptor;
    import org.eclipse.persistence.exceptions.DescriptorException;
    import org.eclipse.persistence.internal.descriptors.InstantiationPolicy;
    import org.eclipse.persistence.internal.security.PrivilegedAccessHelper;
    import org.eclipse.persistence.internal.sessions.AbstractSession;
    
    public class FactoryCustomizer implements DescriptorCustomizer{
    
        @Override
        public void customize(ClassDescriptor descriptor) throws Exception {
            descriptor.setInstantiationPolicy(new MyInstantiationPolicy(descriptor));
        }
    
        private static class MyInstantiationPolicy extends InstantiationPolicy {
    
            public MyInstantiationPolicy(ClassDescriptor descriptor) {
                InstantiationPolicy defaultInstantiationPolicy = descriptor.getInstantiationPolicy();
                this.factoryClassName = defaultInstantiationPolicy.getFactoryClassName();
                this.factoryClass = defaultInstantiationPolicy.getFactoryClass();
                this.methodName = defaultInstantiationPolicy.getMethodName();
            }
    
            @Override
            public void initialize(AbstractSession session) throws DescriptorException {
                super.initialize(session);
            }
    
            @Override
            protected void initializeMethod() throws DescriptorException {
                Class<?>[] methodParameterTypes = new Class[] {Class.class};
                try {
                    this.method = PrivilegedAccessHelper.getMethod(factoryClass, methodName, methodParameterTypes, true);
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
            }
    
            @Override
            public Object buildNewInstance() throws DescriptorException {
                Object[] parameters = new Object[] {this.descriptor.getJavaClass()};
                try {
                    return PrivilegedAccessHelper.invokeMethod(method, factory, parameters);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
    
        }
    
    }
    

    Demo

    import java.io.StringReader;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Foo.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Foo foo = (Foo) unmarshaller.unmarshal(new StringReader("<foo><bar/></foo>"));
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(foo, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <foo>
       <creationDate>2011-11-08T12:35:43.198</creationDate>
       <bar>
          <creationDate>2011-11-08T12:35:43.198</creationDate>
       </bar>
    </foo>
    

    For More Information

    • http://blog.bdoughan.com/2011/06/jaxb-and-factory-methods.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the documentation, the decimal.Round method uses a round-to-even algorithm which is not
According to documentation Default implementation does nothing. But... I throw exception from drawRect method
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to the documentation : There is no way to block signals temporarily from
According to MSDN Documentation for partial classes : Partial methods are implicitly private So
According to the documentation for NSString's method -UTF8String : The returned C string is
I have written code ORM::factory('cds')->find_all(1, 2); It is returning all row . But according
According to the documentation : An app is a Web application that does something
There's setDoOutput() in URLConnection . According to documentation I should Set the DoOutput flag
I think none subroutine from List::MoreUtils does not act as described. According to documentation,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.