[Spoon-discuss] code creation using factories

Jakub Mikulski kuba.mikulski at gmail.com
Sun Nov 9 19:21:38 CET 2008


Thanks for your reply, but there still is a problem. The line:

f.Field().createReference("java.lang.System.out");

creates a CtFieldReference, while the first argument to createInvocation 
needs to be a CtExpression - how do I fix that? I've been trying 
something like this:

CtFieldReference<?> streamRef = 
f.Field().createReference("java.lang.System.out");
CtExpression<?> itarget = f.Code().createVariableAccess(streamRef, true);

Unfortunately, it doesn't work... Any help would be appreciated.

Regards,
km

Renaud Pawlak pisze:
> Hi,
>
> You are missing the target and the arguments of the invocation. You
> should avoid the core factory methods because they are too simple...
>
> Factory f=...;
>
> f.Code().createInvocation(f.Field().createReference("java.lang.System.out"),
> f.Executable().createReference("void
> java.io.PrintStream.println(java.lang.String)"),
> f.Code().createLiteral("hello world"));
>
> Note1: I am not so sure about the format of the strings.. to be
> checked in the code if anything goes wrong.
>
> Note2: you can also use code snippets to create simple code... it is
> of course not very nice (no AST there) but simpler to write...
>
> f.Code().createCodeSnippetExpression(java.lang.System.out.println("hello
> world"));
>
> Note3: the cleanest and checked way it through templates, of course.
>
> Cheers,
> /Renaud
>
>   





More information about the Spoon-discuss mailing list