Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 363192

Summary: Enhancement Request: Add support for parameterized factory methods
Product: z_Archived Reporter: Blaise Doughan <blaise.doughan>
Component: EclipselinkAssignee: Project Inbox <eclipselink.oxm-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
URL: http://stackoverflow.com/q/8026502/383861
Whiteboard:

Description Blaise Doughan CLA 2011-11-08 11:09:30 EST
JAXB allows a factory class and method to be specified via the @XmlType annotation:

@XmlType(
    propOrder={"name", "address", "phoneNumbers"},
    factoryClass=CustomerFactory.class,
    factoryMethod="createCustomer")
public class Customer {
}

The factory method is expected to be a 0-arg static or instance method.  This requires the user to have a unique factory method per type.

Enhancement
-----------

In addition to checking for a 0-arg method, check for a method with a singe arg of type Class.  When calling this factory method the domain model class would be passed in as a parameter.  

This would allow you to do:

@XmlType(
    factoryClass=CommonFactory.class,
    factoryMethod="create")
public class Customer {
}

@XmlType(
    factoryClass=CommonFactory.class,
    factoryMethod="create")
public class Address {
}

public class CommonFactory {
    public static Object create(Class clazz) {
         if(Customer.class == clazz) {
            ...
         } else if(Address.class == clazz) {
            ...
         }
    }
}
Comment 2 Eclipse Webmaster CLA 2022-06-09 10:09:33 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink