2013-05-02 108 views
1

我正在寫一個類,它將數據(定義每個接口)寫入不同的xml-輸出格式(不同的JAXB-Classes)。 所有支持的類型都存儲在Enum(SupportedTypes)中。 Enum存儲相應的JAXB-Class。 枚舉看起來是這樣的:從枚舉映射到類型的通用

public enum Types { 
/** 
* Invoice from hospitals. 
*/ 
Type1(...generatedClasses.invoice.hospital400.request.RequestType.class), 
/** 
* Invoice from hospital but with MediData's quality extensions. The 
* response has no extensions. 
*/ 
Type2(...generatedClasses.invoice.hospital400_QO.request.RequestType.class); 

/** 
* Class for request. represents the root element in corresponding xml. 
*/ 
private Class<?> rType; 

/** 
* 
* @param requestType 
*   class of corresponding request type 
*/ 
private InvoiceTypes(final Class<?> requestType) { 
    this.requestType = requestType; 
} 

/** 
* @return the requestType 
*/ 
public final Class<?> getRequestType() { 
    return requestType; 
} 

} 

我的問題是如何使用此類型實例化泛型類型類似的JAXBElement。 typeEnum作爲參數給出,我想創建JAXBElement,但是這顯然不起作用。 現在,我卡住了。如何構建這樣的構造器或方法。提前

THX

編輯澄清:

讓我們假設你創建一個類( 「ClassForTypes」),支持不同類型的 - 不管它與他們(TheirClass,SpecialClass,MyClass的)一樣。 api不會發布這些類(它們非常具體),而是會發佈一個存儲類的類型(TheirClass,SpecialClass,MyClass)的「TypeEnum」(TypeOne,TypeTwo,TypeThree)。 在ClassForTypes的構造時間,它將使用給定的TypeEnum來創建我們說的List<type saved in enum>。如何構造這樣的ClassForTypes或它的構造函數?

一些示例代碼(不工作): 從上面我想用這種方式枚舉:

public class Blub{ 

    public Blub(Types type){ 
     List<type.getRequestType> typedList = new ArrayList... 
    } 

} 

這是行不通的。但是列表的類型在編譯時是已知的(因爲它存儲在一個枚舉中?)。有什麼方法可以靜態存儲一個類型並使用它來獲得一個類型化的泛型?我不希望api用戶知道關於單一請求類型的一些信息,用戶應該只知道通過枚舉提供的「支持類型」。

+0

對不起,但我仍然不清楚你想達到什麼 - 也許如果你發佈了一些不工作的代碼將點擊。請儘量像我一樣減少代碼。 – OldCurmudgeon 2013-05-03 20:44:14

回答

0

你的問題並不太清楚你想要做什麼。你是否試圖爲你的枚舉添加功能?像這樣?

public enum Types { 
    Type1(String.class) { 
    @Override 
    public Object make() { 
     return new String(); 
    } 
    }, 
    Type2(Integer.class) { 
    @Override 
    public Object make() { 
     return new Integer(0); 
    } 
    }; 

    private Class<?> rType; 

    Types(final Class<?> requestType) { 
    this.rType = requestType; 
    } 

    public final Class<?> getRequestType() { 
    return rType; 
    } 

    // All types must have a make method. 
    public abstract Object make(); 
} 
+0

沒有枚舉沒有功能 - 我會編輯我的問題... – dermoritz 2013-05-02 10:33:00

+0

編輯我的文章 - 希望它現在更清楚(但我認爲這沒有乾淨的方式在這個Java?!) – dermoritz 2013-05-13 08:24:41