Groovyが拡張してくれたObject.dump()とinspect()が便利。
dump()はJavaのクラス名とか内部プロパティまで詳細情報を文字列で取得出来ます。
inspect()ですとフレンドリーな文字列表現を取得出来ます。
つまるところ、printlnとかロギングでのデバッグ出力で便利です。
例:t_collections.groovy ( http://groovy.codehaus.org/Collections から適当に練習用に抜き出した。)
def c1 = [1, 2, 3] println '------------------------------------' println 'dump() : [' + c1.dump() + ']' println 'inspect(): [' + c1.inspect() + ']' c1 << 4 println '------------------------------------' println 'dump() : [' + c1.dump() + ']' println 'inspect(): [' + c1.inspect() + ']' c1 << [5, 6] << 7 println '------------------------------------' println 'dump() : [' + c1.dump() + ']' println 'inspect(): [' + c1.inspect() + ']' def c2 = [:] println '------------------------------------' println 'dump() : [' + c2.dump() + ']' println 'inspect(): [' + c2.inspect() + ']' c2['name'] = 'abc' c2.age = 20 def m1 = { you -> return "hello, ${you}." } c2.greet = m1 println '------------------------------------' println 'dump() : [' + c2.dump() + ']' println 'inspect(): [' + c2.inspect() + ']' println c2.greet('bob') def c3 = new Expando() c3.name = 'Jon' c3.greet = { "Good morning, ${name}" } println '------------------------------------' println 'dump() : [' + c3.dump() + ']' println 'inspect(): [' + c3.inspect() + ']' println c3.greet();
実行:
$ groovy t_collections.groovy ------------------------------------ dump() : [<java.util.ArrayList@7861 elementData=[1, 2, 3] size=3 modCount=1>] inspect(): [[1, 2, 3]] ------------------------------------ dump() : [<java.util.ArrayList@e93c3 elementData=[1, 2, 3, 4] size=4 modCount=2>] inspect(): [[1, 2, 3, 4]] ------------------------------------ dump() : [<java.util.ArrayList@36b936e8 elementData=[1, 2, 3, 4, [5, 6], 7] size=6 modCount=4>] inspect(): [[1, 2, 3, 4, [5, 6], 7]] ------------------------------------ dump() : [<java.util.LinkedHashMap@0 header=null=null accessOrder=false table=[null] size=0 threshold=0 loadFactor=0.75 modCount=0 useAltHashing=false hashSeed=-1854095574 entrySet=[] keySet=null values=null>] inspect(): [[:]] ------------------------------------ dump() : [<java.util.LinkedHashMap@2029ac47 header=null=null accessOrder=false table=[greet=t_collections$_run_closure1@1a16ff7a, age=20] size=3 threshold=1 loadFactor=0.75 modCount=3 useAltHashing=false hashSeed=-1854095574 entrySet=[name=abc, age=20, greet=t_collections$_run_closure1@1a16ff7a] keySet=null values=null>] inspect(): [['name':'abc', 'age':20, 'greet':t_collections$_run_closure1@1a16ff7a]] hello, bob. ------------------------------------ dump() : [<groovy.util.Expando@4c48b2f8 expandoProperties=[name:Jon, greet:t_collections$_run_closure2@38dddee8]>] inspect(): [{name=Jon, greet=t_collections$_run_closure2@38dddee8}] Good morning, Jon
コメント