Created
March 7, 2012 05:40
-
-
Save ponkichi7/1991302 to your computer and use it in GitHub Desktop.
DSLのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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