groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anto Aravinth <anto.aravinth....@gmail.com>
Subject Re: Add closures to class via AST
Date Fri, 21 Aug 2015 23:05:45 GMT
Any ideas will greatly helps!!
On 21 Aug 2015 16:57, "Anto Aravinth" <anto.aravinth.cse@gmail.com> wrote:

> Hi All,
>
> Any solutions?
> On 21 Aug 2015 09:21, "Anto Aravinth" <anto.aravinth.cse@gmail.com> wrote:
>
>> Hi all,
>>
>>
>>
>> I'm trying to add an closure to my classNode via AST transformation. My
>> code looks something like this:
>>
>>
>>
>> List<ASTNode> closure = new AstBuilder().buildFromCode {
>>
>>                 { param ->
>>
>>                     delegate.with {
>>
>>                         param()
>>
>>                     }
>>
>>                 }
>>
>>             }
>>
>>
>>
>>             cNode.addMethod(new MethodNode('helloTest', ACC_PUBLIC,
>> ClassHelper.DYNAMIC_TYPE,[new Parameter(ClassHelper.OBJECT_TYPE, "param")]
>> as Parameter[], ClassNode.EMPTY_ARRAY, closure[0]));
>>
>>
>>
>> And running AST transformation, on the test class:
>>
>>
>>
>> @AddMethod
>>
>> class Test{
>>
>> }
>>
>>
>>
>> def a = new Test()
>>
>> a.helloTest({
>>
>>         println "Works great!";
>>
>> })
>>
>>
>>
>> Now no errors nothing. The code works fine, but since I have passed an
>> println to the helloTest closure, I expect it to print on my console, which
>> didn't happen. Looks like my closure itself didn't ran. Then I called the
>> closure myself, something like this:
>>
>>
>>
>>             List<ASTNode> closure = new AstBuilder().buildFromCode {
>>
>>                 { param ->
>>
>>                     delegate.with {
>>
>>                         println delegate
>>
>>                         param()
>>
>>                     }
>>
>>                 }.call()
>>
>>             }
>>
>>
>>
>> Now this gives NPE, saying param is null. Looks now the parameter is not
>> getting binded. Delegate is getting printed as expected.
>>
>>
>>
>> Also I'm not sure, the way I have added the closure to my class is right
>> or not. Any help would be great!
>>
>

Mime
View raw message