我正在编写一个类,将数据(每个接口定义)写入不同的xml输出格式(不同的JAXB类)。
所有受支持的类型都存储在一个Enum(SupportedTypes)中。枚举存储相应的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,但这显然不起作用。
现在我卡住了。如何构造这样的构造器或方法。

提前

编辑以澄清:

假设您创建了一个支持不同类型的类(“ 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用户了解有关用户应仅了解通过枚举传递的“受支持的类型”的单个请求类型的知识。

最佳答案

您对要做什么的问题不太清楚。您是否要向枚举添加功能?像这样吗

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

10-06 05:11
查看更多