2011-07-06 23 views
2

我有這個模式,我使用JAXB生成java存根文件。JAXB綁定 - 定義列表的返回類型<T>方法

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:c="http://www.a.com/f/models/types/common" 
    targetNamespace="http://www.a.com/f/models/types/common" 
    elementFormDefault="qualified"> 

    <xs:complexType name="constants"> 
     <xs:sequence> 
      <xs:element name="constant" type="c:constant" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="constant"> 
     <xs:sequence> 
      <xs:element name="reference" type="c:reference"/> 
     </xs:sequence> 
     <xs:attribute name="name" use="required" type="xs:string"/> 
     <xs:attribute name="type" use="required" type="c:data-type"/> 
    </xs:complexType> 

默認的Java包的名稱是「com.afmodels.types.common」

我也有「常量」和「常量」定義的包「com.afmodel.common現有的接口'我想要生成的類使用哪個 。我使用的JAXB綁定文件,以確保生成的Java類實現 所需的接口

<jxb:bindings schemaLocation="./commonmodel.xsd" node="/xs:schema"> 
    <jxb:bindings node="xs:complexType[@name='constants']"> 
     <jxb:class/> 
     <inheritance:implements>com.a.f.model.common.Constants</inheritance:implements> 
    </jxb:bindings> 

下生成的類並實現了正確的接口

package com.a.f.models.types.common; 
.. 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "constants", propOrder = { 
    "constant" 
}) 
public class Constants 
    implements com.a.f.model.common.Constants 
{ 

    @XmlElement(required = true) 
    protected List<Constant> constant; 

    public List<Constant> getConstant() { 

但返回類型清單<> getConstant()方法不正確。我需要這是

public List<com.a.f.model.common.Constant> getConstant() { 

是否有通過jaxb綁定文件做到這一點?

回答

2

我工作圍繞這通過使用Java泛型,使現有的接口在他們的返回類型

package com.a.f.m.common; 

import java.util.List; 

public interface Constants { 

    public List<? extends Constant> getConstant(); 
} 

更靈活由於JAXB生成的類常量確實實現了現有的接口常數,對方法的返回類型爲允許。使用JAXB綁定文件聲明返回類型似乎是不可能的。

相關問題