Using the second example you linked to, I can get this to (I think) work:
import groovy.inspect.swingui.AstNodeToScriptVisitor
String method( Closure a ) {
new StringWriter().with { writer ->
a.metaClass.classNode.getDeclaredMethods("doCall")[0].code.visit new AstNodeToScriptVisitor( writer )
"{${writer.toString()}}"
}
}
c1 = {p1 + p2}
c2 = '{return p1 * p2}'
data = [fields: ['p1': 'int', 'p2':'int'], methods: ['m1': method( c1 ), 'm2':c2], name: 'Agent']
templateText = '''
class $name
{
<%fields.each {%> $it.value $it.key \n<% } %>
<%methods.each {%> def $it.key() $it.value \n<% } %>
}
'''
engine = new groovy.text.SimpleTemplateEngine()
template = engine.createTemplate(templateText)
result = template.make(data)
println result
GroovyClassLoader loader = new GroovyClassLoader()
Class cls = loader.parseClass(result.toString())
i = cls.newInstance()
i.p1 = 1
i.p2 = 2
i.setP2(10)
println i.m1()
println i.m2()
It does not work in the GroovyConsole, but it works if you save it as a script, and invoke it from the command line