TrapII
TrapII

Reputation: 2275

How to instantiate a java generic class in a Rhino script?

I try to marshall some JAXB objects in Rhino Javascript. Those JAXB Objects (top root is MyClass) are created from WSDL with wsimport. The Java side of the application doesn't know about MyClass.

My rhino script looks like this :

importPackage(Packages.javax.xml.bind);
importPackage(Packages.javax.xml.namespace);
...
  var myObj = new MyClass(); // MyClass has been generated from WSDL with wsimport
  var jaxbContext = JAXBContext.newInstance(myObj.getClass().getPackage().getName());
  var marshaller = jaxbContext.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  var strWriter = new StringWriter();
  var qName = new QName("xxx", "MyClass");
  var jaxbElement = new JAXBElement(qName, myObj.getClass(), myObj);
  marshaller.marshal(jaxbElement, strWriter);

Unfortunately it gives a error saying the constructor of javax.xml.bind.JAXBElement is not found for arguments object, java.lang.class, MyClass.

I also tried without constructing the jaxbElement but marshaller launch an exception saying it cannot marshall due to lack of XMLRootElement is missing.

Is there a way either to indicate to rhino the type of JAXBElement or maybe a Java code that can be called WITHOUT knowing MyClass ?

The line causing the problem is:

var jaxbElement = new JAXBElement(qName, pspApp.getClass(), pspApp);

Upvotes: 1

Views: 1155

Answers (1)

Serg M Ten
Serg M Ten

Reputation: 5606

You must fully qualify QName including its package name in the constructor like:

var qName = new javax.xml.namespace.QName("xxx", "MyClass");

Upvotes: 1

Related Questions