groovyconsole2
TRANSCRIPT
今年の夏はGroovyConsole が熱い
@kiy0taka
自己紹介奥 清隆(おく きよたか)
id:kiy0taka、@kiy0taka
日本Grails/Groovyユーザグループ関西支部長
関西ジャバエンジニアの会
Jenkinsプラグイン開発者
jQuery、jQueryUI、Terminal
TEPCO、MongoDB
あわせて読みたい
GroovyConsoleで遊ぶ
http://www.slideshare.net/kiy0taka/groovyconsole
GroovyConsoleでお絵かき (Grな日々)
http://d.hatena.ne.jp/uehaj/20110701/1309532411
EqualsAndHashCode?Groovy 1.8で追加されたAST変換の1つ
AST変換とはコンパイル時にAST(構文木)をゴニョゴニョして変換する
PowerAsserとか
クラスに@EqualsAndHashCodeアノテーションを書いておけば、equals()とhashCode()が勝手に実装される
ないときclass Person { String name int age}
def p1 = new Person(name:'Kiyotaka Oku', age:31)def p2 = new Person(name:'Kiyotaka Oku', age:31)
assert p1 == p2
assert p1 == p2 | | | | | Person@20e5f01b | false Person@57f68d7c
あるとき
import groovy.transform.EqualsAndHashCode
@EqualsAndHashCodeclass Person { String name int age}
def p1 = new Person(name:'Kiyotaka Oku', age:31)def p2 = new Person(name:'Kiyotaka Oku', age:31)
assert p1 == p2assert p1.hashCode() == p2.hashCode()
1.8で追加されたAST変換
@Log、@Commons、@Log4j、@Slf4j、@Field、@PackageScope、
@AutoClone、@AutoExternalizable、@ThreadInterrupt、@TimedInterrupt、@ConditionalInterrupt、@ToString、
@EqualsAndHashCode、@TupleConstructor、
@Canonical、@InheritConstructors、@WithReadLock、@WithWriteLock、
@ListenerList
アノテーション書くだけで色々やってくれるのに、メンドイとか言うヤツがいる。
そんなゆとり世代のために1.8で追加された機能
Compilation customizersコンパイルのカスタマイズが可能
ImportCustomizerデフォルトインポートを追加
ASTTransformationCustomizerすべてのクラスに好きなAST変換を追加
SecureASTCustomizerAST使ってセキュアなコードにできる(?)
import org.codehaus.groovy.control.CompilerConfigurationimport org.codehaus.groovy.control.customizers.*import groovy.util.logging.Log
def configuration = new CompilerConfiguration()configuration.addCompilationCustomizers( new ASTTransformationCustomizer(Log))
def shell = new GroovyShell(configuration)shell.evaluate(""" class Car { Car() { log.info 'Car constructed' } }
log.info 'Constructing a car' def c = new Car()""")
ゆとり専用 GroovyConsole作ってみる
YutoriConsole.groovyimport javax.swing.UIManagerimport groovy.ui.Consoleimport org.codehaus.groovy.control.CompilerConfigurationimport org.codehaus.groovy.control.customizers.ASTTransformationCustomizerimport groovy.transform.*
def transformations = [ThreadInterrupt, Canonical]
Console.metaClass.newScript = { ClassLoader parent, Binding binding -> def config = new CompilerConfiguration() config.addCompilationCustomizers(*transformations.collect{ new ASTTransformationCustomizer(it)}) delegate.shell = new GroovyShell(parent, binding, config)}
UIManager.lookAndFeel = UIManager.systemLookAndFeelClassNamenew Console(Console.class.classLoader.getRootLoader()).run()
https://gist.github.com/1059105
まとめ
おっさん率が高いGroovy界にゆとり世代がきたことは多分いいこと。