[Spoon-discuss] Hello Spoon!

Renaud Pawlak renaud.pawlak at gmail.com
Fri Aug 28 10:03:25 CEST 2009


Hey Matias,

Thanks for the code :) It looks like the best way to do it with this
technique. I agree, it's ugly. There must be a slightly better way
with spoon templates but I am too lazy to think of it right now (it
would be complicated anyways because of the "ifs elseifs" generation).

Cheers,
/Renaud

2009/8/28 Matías Costa <m.costacano at gmail.com>:
> 2009/8/27 Matías Costa <m.costacano at gmail.com>:
>> On Fri, Aug 21, 2009 at 11:16 AM, Renaud Pawlak<renaud.pawlak at gmail.com> wrote:
>>> Hi, the code you need to generate is highly dynamic and untyped
>>> (string equality), so I wouldn't use a Spoon template in that case.
>>>
>>> You can insert some text as a statement. In your (class) processor do
>>> something like:
>>>
>>> Factory f=getFactory();
>>> CtMethod m=f.Method().create(targetClass,
>>> f.Code().modifiers(Modifier.PUBLIC),
>>> f.Type().createReference(Object.class),"get",f.Type().createReferences(String.class),null);
>>> m.getBody().getStatements().add(f.Code().createCodeSnippetStatement("HERE
>>> GOES ANY COMPILABLE JAVA STATEMENT AS A STRING"));
>>> // normally, do one code snippet statement per statement (here one for
>>> the if statement and one for the return statement
>>>
>>> Hope this will help.
>>
>> Thank you so much for the advice!. I just got back from a short vacancy week.
>>
>> I am going to test it right now and I'll tell the results.
>>
>
> I got to make it working, it's ugly but... I don't know better.
>
> First the problems I have found. Just for the feedback. Let f be
> getFactory(), then f.Method().create(...) needs a CtParameter but to
> create one with  f.Executable().createParameter(...) you need the
> method. So I create the method first without parameters and I make the
> param later. f.Method().create() without block parameter returns a
> CtMethod with its block null, so your last line fails
> m.getBody().getStatements().add(...)
>
> The code
>
>                Factory f = getFactory();
>                List<CtElement> properties = w.getAnnotatedChildren(Property.class);
>                StringBuilder code = new StringBuilder();
>                for (CtElement prop : properties) {
>                        String name = ((CtNamedElement) prop).getSimpleName();
>                        code.append("if (propertyName.equals(\"");
>                        code.append(name);
>                        code.append("\")) return ");
>                        code.append(name);
>                        if (prop instanceof CtMethod<?>)
>                                code.append("()");
>                        code.append(";\n        ");
>                }
>                code.append("return super.get(propertyName)");
>
>                // Statement block from literal string
>                CtBlock<Object> block = f.Core().createBlock();
>                block.getStatements().add(f.Code().createCodeSnippetStatement(code.toString()));
>                // The method!
>                CtMethod<Object> m = f.Method().create(w,
> f.Code().modifiers(ModifierKind.PUBLIC),
>                        f.Type().createReference(Object.class), "get", null, null, block);
>                // The String paramenter 'propertyName'
>                f.Executable().createParameter(m,
> f.Type().createReference(String.class), "propertyName");
>
> Thank you for reading.
>
> _______________________________________________
> Spoon-discuss mailing list
> Spoon-discuss at lists.gforge.inria.fr
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/spoon-discuss
>



-- 
Renaud Pawlak, Professor
ISEP - 21 rue d’Assas 75006 Paris, France
Head of the Computer Engineering Department
Head of ISEP Research and Consulting for Computer Engineering
Phone: +33 149 54 52 78
Cell #1: +33 6 37 29 12 15
Cell #2: +33 6 36 47 06 01
Fax: +33 1 49 54 52 51
http://renaudpawlak.wikidot.com



More information about the Spoon-discuss mailing list