私のコードは次のとおりです。
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<?>