[Spoon-discuss] code creation using factories

Jakub Mikulski kuba.mikulski at gmail.com
Sun Nov 9 15:39:23 CET 2008


Hello,

I'm using Spoon to create some new code based on annotations in 
processed classes (no templates, I'm trying to do this using factories). 
I have successfully created a new class and its methods. Now I'm trying 
to add some code to these methods, but I'm having some problems... Let's 
say I want to add a classic System.out.println("Hello world!") to a 
method which code block is referenced by cb. My code looks like this:

// crf, ef, tf are factories (Core, Executable, Type)
CtInvocation<?> inv = crf.createInvocation();
List<CtTypeReference<?>> args = new LinkedList<CtTypeReference<?>>();
args.add(tf.createReference("java.lang.String"));
CtExecutableReference ref = 
ef.createReference(tf.createReference("java.io.PrintStream"), 
tf.createReference("void"), "println", args);
inv.setExecutable(ref);
cb.insertBegin(inv);

I have no idea how to specify that the PrintStream I'm calling this 
method on is System.out, and that the String argument is "Hello world!" 
- accordingly, the resulting code block in the new method is simply { 
println(); }. Any leads...?

Thanks in advance,
km




More information about the Spoon-discuss mailing list