2

我的代码如下:

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

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

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

和:

Cannot instantiate the type JAXBElement<?>
4

1 回答 1

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

    ...
}

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

如前所述,您应该记录未经检查的强制转换,解释为什么调用list由 a 表示的给定类Class<T>保证返回 a ArrayList<T>,正如您的代码所假设的那样。这种方法在我看来充其量是脆弱的,当你打电话时我已经发现了一个错误invoke

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

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

于 2013-03-01T05:17:53.890 回答