从枚举到泛型的类型的java映射
我正在编写一个类,该类将数据(按接口定义)写入不同的xml输出格式(不同的JAXB类)。 所有支持的类型都存储在一个枚举(SupportedTypes)中。枚举存储对应的JAXB类。 枚举如下所示:
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不会发布这些类(它们非常具体),而是发布一个“TypeEnum”(TypeOne、TypeTwo、TypeThree),用于存储类的类型(TheirClass、SpecialClass、MyClass)。
在构建ClassForTypes时,它将使用给定的TypeEnum来创建一个List<type saved in enum>
。如何构造这样一个ClassForTypes或它的构造函数
一些示例代码(不起作用): 我希望通过以下方式使用上面的枚举:
public class Blub{
public Blub(Types type){
List<type.getRequestType> typedList = new ArrayList...
}
}
这是行不通的。但是列表的类型在编译时是已知的(因为它存储在枚举中?)。有没有办法静态存储一个类型并使用它来获取一个类型化泛型?我不希望api用户知道关于单个请求类型的一些信息,用户应该只知道通过枚举传递的“支持的类型”
# 1 楼答案
你的问题不太清楚你到底想做什么。您正在尝试向枚举添加功能吗?像这样的