将JAXB,Generics和Reflection结合到XML序列化我的所有Java类

时间:2013-03-01 04:56:40

标签: java generics reflection jaxb

我的代码如下:

    System.out.println("This will save a table to XML data sheet.");
    System.out.println("Please pick a table to save: " + listOfTables.toString());
    command = scan.nextLine();
    if(listOfTables.contains(command))
    {
        System.out.println("successfuly found table to save: " + command);
        try  //Java reflection
        {
            Class<?> myClass = Class.forName(command); // get the class named after their input
            Method listMethod = myClass.getDeclaredMethod("list"); // get the list method from the class
            Object returnType = listMethod.invoke(myClass, new Object[]{}); // run the list method
            ArrayList<Object> objectList = (ArrayList)returnType; // get the arraylist of objects to send to XML
            try 
            {
                JAXBContext jaxbContext = JAXBContext.newInstance(myClass);
                Marshaller marshaller = jaxbContext.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                JAXBElement<?> jaxbElement = new JAXBElement<?>(new QName("jaxbdemo", "generated"), myClass, objectList.get(0));
                marshaller.marshal(jaxbElement, System.out);

            } catch (JAXBException e) {}
        }
        catch (ClassNotFoundException | SecurityException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { }

我的问题是:

JAXBElement<?> jaxbElement = new JAXBElement<?>(new QName("jaxbdemo", "generated"), myClass, objectList.get(0));

也:

JAXBElement<myClass> jaxbElement = new JAXBElement<myClass>(new QName("jaxbdemo", "generated"), myClass, objectList.get(0));

将编译。那么我需要在&lt;&gt;之间放置什么?对于我的JAXBElement类型?顺便说一句,我得到了:

The constructor JAXBElement<myClass>(QName, Class<myClass>, myClass) refers to the missing type myClass

Cannot instantiate the type JAXBElement<?>

1 个答案:

答案 0 :(得分:1)

您需要使用辅助方法来执行此操作。这是一个粗略的例子:

static <T> void helper(Class<T> myClass) {

    Method listMethod = myClass.getDeclaredMethod("list");
    Object returnType = listMethod.invoke(myClass, new Object[]{});
    @SuppressWarnings("unchecked") // [carefully document why this is okay here]
    ArrayList<T> objectList = (ArrayList<T>)returnType;

    ...

    JAXBElement<T> jaxbElement = new JAXBElement<T>(
            new QName("jaxbdemo", "generated"),
            myClass,
            objectList.get(0)
    );

    ...
}

从那时起,您可以自由地返回JAXBElement<?>或者完成帮助者中的剩余工作。

如上所述,您应该记录未经检查的强制转换,解释为什么对list所代表的类调用Class<T>保证返回ArrayList<T>,如您的代码所假设的那样。这种方法对我来说似乎很脆弱,当你致电invoke时我已经发现了一个错误:

listMethod.invoke(myClass, new Object[]{});

该方法将调用该方法的实例作为其第一个参数(如果它是静态方法,则采用null),但是您传入myClass - 这可能不是右。