使用类来创建JAXB工厂方法的参数化

时间:2011-11-06 10:44:19

标签: java jaxb

根据文档,JAXB工厂方法没有参数。是否有一个JAXB实现允许我创建一个工厂方法,该方法接收我需要创建的对象的类作为参数? 碰巧我的所有JAXB对象都遵循相同的创建模式(特定的字节代码检测),因此我想将其封装在一个单独的工厂方法中,该方法将要创建的JAXB对象的类作为参数,从而避免以这种方式为每个JAXB类创建不同的工厂方法,基本上完全相同。

我发现有人在OTN论坛上提出同样的问题:https://forums.oracle.com/forums/thread.jspa?messageID=9969927#9969927,但尚未提出真正的答案。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

目前使用标准JAXB API无法做到这一点。我已输入以下增强请求,以便将此行为添加到EclipseLink JAXB (MOXy)

MOXy特定解决方案

您可以利用EclipseLink JAXB (MOXy)中的@XmlCustomizer扩展名来自定义对象的实例化方式。利用这种机制来调整MOXy的底层元数据。

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

Foo类通常会被注释,除了我们将使用@XmlCustomizer注释来指定我们将用于调整MOXy元数据的DescriptorCustomizer

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

}

酒吧

我们将再次使用@XmlCustomizer注释来引用我们在DescriptorCustomizer类中所做的Foo

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具有构建新对象的InstantiationPolicy概念。在这个例子中,我们将交换我们自己的实例InstantiationPolicy,它可以使用参数化的工厂方法:

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

    }

}

演示

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

}

输出

<?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>

了解更多信息