0

I'm using sun's code model (2.4.1) classes to generate code. How do I pass a Class to JInvocation.arg? The code I am trying to generate is:

JAXBContext jc;
jc = JAXBContext.newInstance(NaturalLanguageUsage.class);

The code I am running is:

JClass importJAXBContext = codeModel.directClass(javax.xml.bind.JAXBContext.class.toString());
JType typeJAXBContext = codeModel._ref(javax.xml.bind.JAXBContext.class);
JVar varJc = block.decl(typeJAXBContext, "jc");

JInvocation invokeJAXBContext = block.staticInvoke(importJAXBContext, "newInstance");
invokeJAXBContext.arg(??); // how do I pass NaturalLanguageUsage.class

What I would like to produce:

JAXBContext jc;
jc = JAXBContext.newInstance(NaturalLanguageUsage.class);

or even better:

JAXBContext jc = JAXBContext.newInstance(NaturalLanguageUsage.class);
EGHM
  • 2,144
  • 23
  • 37

1 Answers1

2
    JClass importJAXBContext = codeModel.ref(javax.xml.bind.JAXBContext.class);
    JVar varJc = block.decl(importJAXBContext, "jc");

    JClass naturalLangClassRef = codeModel.ref(NaturalLanguageUsage.class);

    JInvocation invokeJAXBContext = importJAXBContext.staticInvoke("newInstance");
    invokeJAXBContext.arg(naturalLangClassRef.dotclass());

    block.assign(varJc, invokeJAXBContext);

To produce declaration and assignment in one line:

    JVar varJc = block.decl(importJAXBContext, "jc", invokeJAXBContext);
ddms
  • 50
  • 6
  • I'm getting 1 error when I use this exactly as presented. The first line codeModel.ref wants a String not a Class so I've got a toString() on that like in my Question. I'm accepting your answer, though would you know how I can do the declaration and assignment on one line vs two? – EGHM Oct 09 '15 at 20:54
  • 2
    That's weird because you should be able to use 'ref' method which takes Class arg - see [this](https://codemodel.java.net/nonav/apidocs/com/sun/codemodel/JCodeModel.html#ref%28java.lang.Class%29). Which version of codemodel do you use? There is an overloaded `decl` method in JBlock class which takes JExpression as third argument - that allows you to do declaration and initialization in one line. – ddms Oct 09 '15 at 21:26
  • Using version 2.4.1 of sun.codemodel. In the previous comment I said, toString() but getName() is better as it keeps the class or interface out of the import. I think there is an issue with that URL. – EGHM Oct 09 '15 at 21:30
  • 1
    URL that I provided in my previous comment should be ok now. Looks like version 2.4.1 of codemodel supports referencing by class - [link](http://grepcode.com/file/repo1.maven.org/maven2/com.sun.codemodel/codemodel/2.4.1/com/sun/codemodel/JCodeModel.java?av=f#338) – ddms Oct 09 '15 at 21:41
  • How can I just create the declaration with assignment statement in one line without assigning it to a block, rather returning it somehow? Here's my question along similar lines :- https://stackoverflow.com/questions/56323876/how-can-i-create-a-simple-assignment-statement-in-codemodel – Simar Singh May 27 '19 at 11:56