Skip to content

Instantly share code, notes, and snippets.

@spmallette
Created September 30, 2014 19:41
Show Gist options
  • Save spmallette/b0fdb3ed8166a4d3c3a2 to your computer and use it in GitHub Desktop.
Save spmallette/b0fdb3ed8166a4d3c3a2 to your computer and use it in GitHub Desktop.
Groovy Script From Code
public static String getClosureBody(final Closure closure) {
def node = closure.getMetaClass().getClassNode().getDeclaredMethods("doCall")[0].getCode();
def writer = new StringWriter()
node.visit(new AstNodeToGremlinScriptVisitor(writer))
return writer.toString()
}
class AstNodeToGremlinScriptVisitor extends AstNodeToScriptVisitor {
def AstNodeToGremlinScriptVisitor(Writer writer, boolean showScriptFreeForm = true, boolean showScriptClass = true) {
super(writer, showScriptFreeForm, showScriptClass)
}
@Override
void visitReturnStatement(ReturnStatement statement) {
// printLineBreak()
// print 'return '
statement.getExpression().visit(this)
printLineBreak()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment