P. Deters
P. Deters

Reputation: 825

How to metaClass a Groovy method that takes a closure?

How would one metaClass a method that takes an object(s) and a closure?

Specifically say,

public void eachRow(GString gstring, Closure closure)

I'd like to metaClass eachRow() such that it prints the gstring parameter, and ignores the Closure. The goal is to create an app, that when passed a certain parameter does a sort of 'dry run' execution using groovy.sql.Sql, where instead of executing the queries, it just prints the statements it would have normally executed.

Upvotes: 0

Views: 226

Answers (1)

P. Deters
P. Deters

Reputation: 825

So I think my issue wasn't how I was defining the method, but what I was (or wasn't returning). This appears to work ...

sql.metaClass.eachRow = { String qry, Closure c ->
    println qry
    new GroovyRowResult([:]) // I was returning null, adding this fixed me.
}

Upvotes: 1

Related Questions