Skip to content

Instantly share code, notes, and snippets.

@ponkichi7
Created March 7, 2012 05:40
Show Gist options
  • Save ponkichi7/1991302 to your computer and use it in GitHub Desktop.
Save ponkichi7/1991302 to your computer and use it in GitHub Desktop.
DSLのサンプル
class Main {
static main(args){
run( new File("src/main/resources/dsl.groovy"))
}
static run(File file){
Script script = new GroovyShell().parse(file.text)
Message mes = new Message()
script.metaClass = createEMC(script.class,{
ExpandoMetaClass emc ->
emc.hello = {
Closure cl ->
cl.delegate = new HelloDelegate(mes)
cl.resolveStrategy = Closure.DELEGATE_FIRST
cl()
}
})
script.run()
printMessage(mes)
}
static ExpandoMetaClass createEMC(Class clazz, Closure cl){
ExpandoMetaClass emc = new ExpandoMetaClass(clazz,false)
cl(emc)
emc.initialize()
emc
}
static printMessage(Message message){
println "${message.hello}${message.mes}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment