Java 使用xmlelement defaultvalue注释指定默认值的简单方法

Java 使用xmlelement defaultvalue注释指定默认值的简单方法,java,jaxb,annotations,Java,Jaxb,Annotations,我通过JAXB有一个简单的pojo注释类,如下所示: public class MyPojo implements Serializable { private final static long serialVersionUID = 1234L; @XmlElement(name = "Type", required = true, defaultValue = "none") @NotNull protected SeismicDataAcq

我通过JAXB有一个简单的pojo注释类,如下所示:

    public class MyPojo
    implements Serializable
{

    private final static long serialVersionUID = 1234L;
    @XmlElement(name = "Type", required = true, defaultValue = "none")
    @NotNull
    protected SeismicDataAcquisitionSystemType type;
    @XmlElement(name = "IpAddress", required = true)
    @NotNull
    @Pattern(regexp = "((1?[0-9]?[0-9]|2[0-4]|[0-9]|25[0-5]).){3}(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])")
    protected String ipAddress;
    @XmlElement(name = "SealServerTcpPort", defaultValue = "1477")
    @NotNull
    protected int sealServerTcpPort;
    @XmlElement(name = "PamServerTcpPort", defaultValue = "1485")
    @NotNull
    protected int pamServerTcpPort;

    /**
     * Obtient la valeur de la propriété type.
     * 
     * @return
     *     possible object is
     *     {@link SeismicDataAcquisitionSystemType }
     *     
     */
    public SeismicDataAcquisitionSystemType getType() {
        return type;
    }

    /**
     * Définit la valeur de la propriété type.
     * 
     * @param value
     *     allowed object is
     *     {@link SeismicDataAcquisitionSystemType }
     *     
     */
    public void setType(SeismicDataAcquisitionSystemType value) {
        this.type = value;
    }

    public boolean isSetType() {
        return (this.type!= null);
    }

    /**
     * Obtient la valeur de la propriété ipAddress.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getIpAddress() {
        return ipAddress;
    }

    /**
     * Définit la valeur de la propriété ipAddress.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setIpAddress(String value) {
        this.ipAddress = value;
    }

    public boolean isSetIpAddress() {
        return (this.ipAddress!= null);
    }

    /**
     * Obtient la valeur de la propriété sealServerTcpPort.
     * 
     */
    public int getSealServerTcpPort() {
        return sealServerTcpPort;
    }

    /**
     * Définit la valeur de la propriété sealServerTcpPort.
     * 
     */
    public void setSealServerTcpPort(int value) {
        this.sealServerTcpPort = value;
    }

    public boolean isSetSealServerTcpPort() {
        return true;
    }

    /**
     * Obtient la valeur de la propriété pamServerTcpPort.
     * 
     */
    public int getPamServerTcpPort() {
        return pamServerTcpPort;
    }

    /**
     * Définit la valeur de la propriété pamServerTcpPort.
     * 
     */
    public void setPamServerTcpPort(int value) {
        this.pamServerTcpPort = value;
    }
}
我尝试用默认值初始化pojo 那样

    MyPojo myPojo = new MyPojo();
myPojo.getPamServerTcpPort(); // return 0
setDefaultValues(myPojo); // assign attributes with annotated default values
myPojo.getPamServerTcpPort(); // return 1485
我正在尝试使用setDefaultValues(MyPojo loMyPojo)方法编程,该方法使用java.lang.annotation API和java.lang.reflect API解析类,但我的代码很难看,不能使用我自己的枚举默认值

我必须提到,我不能修改原始类MyPojo,因为它本身是通过JAXB通过XSD解析生成的


有什么想法吗?

快速实现缺少的函数可能如下所示:

    public static void setDefaultValues(Object o) throws Exception {
        if (o == null)
            return;
        for (Class<?> clazz = o.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) {
            for (final Field f : clazz.getDeclaredFields()) {
                final XmlElement xe = f.getAnnotation(XmlElement.class);
                if (xe != null && xe.defaultValue() != null) {
                    final Class<?> t = f.getType();
                    if (t.isAssignableFrom(String.class)) {
                        f.setAccessible(true);
                        f.set(o, xe.defaultValue());
                    } else if (t.isAssignableFrom(Integer.class) || clazz.getSimpleName().equals("int")) {
                        f.setAccessible(true);
                        f.set(o, Integer.parseInt(xe.defaultValue()));
                    } else if (t.isAssignableFrom(Long.class) || clazz.getSimpleName().equals("long")) {
                        f.setAccessible(true);
                        f.set(o, Long.parseLong(xe.defaultValue()));
                    }
                    // else {
                    // TODO: add futher conversions
                    // }
                }
            }
        }
    }
publicstaticvoidsetdefaultvalues(objecto)抛出异常{
如果(o==null)
返回;
对于(类clazz=o.getClass();clazz!=Object.Class;clazz=clazz.getSuperclass()){
for(最后一个字段f:clazz.getDeclaredFields()){
最终的xmlement xe=f.getAnnotation(xmlement.class);
if(xe!=null&&xe.defaultValue()!=null){
最终类t=f.getType();
if(t.isAssignableFrom(String.class)){
f、 setAccessible(true);
f、 set(o,xe.defaultValue());
}else if(t.isAssignableFrom(Integer.class)| | clazz.getSimpleName().equals(“int”)){
f、 setAccessible(true);
f、 set(o,Integer.parseInt(xe.defaultValue());
}else if(t.isAssignableFrom(Long.class)| | clazz.getSimpleName().equals(“Long”)){
f、 setAccessible(true);
f、 set(o,Long.parseLong(xe.defaultValue());
}
//否则{
//TODO:添加进一步的转换
// }
}
}
}
}

您使用什么框架生成类(例如Axiom 2)?我使用jaxb api和xjc命令un插件(krasa)