40. @groovy.util.logging.Log
class Foo {
def method() {
log.info('info message')
log.fine('fine message')
log.severe('severe message')
}
}
new Foo().method()
$ groovy astLog.groovy
Nov 9, 2011 1:09:10 PM java_util_logging_Logger$info call
INFO: info message
Nov 9, 2011 1:09:10 PM java_util_logging_Logger$severe call
SEVERE: severe message
41. @Immutable
class Foo {
int x
Date date = new Date(1000)
}
def a = new Foo(x:1)
assert a.x == 1
assert a.date.getTime() == 1000
a.date.setTime(2000)
assert a.date.getTime() == 1000
a.x = 3
42. @Singleton
class Foo {
def hello() { "hello" }
}
assert Foo.instance.hello() == "hello"
// => Caught: java.lang.RuntimeException:
// Can't instantiate singleton Foo. Use Foo.instance
new Foo()
43. class StringList {
@Delegate
List<String> list = new ArrayList<String>()
}
def stringList = new StringList()
stringList.add("A")
stringList.add("B")
assert stringList == ["A", "B"]
stringList.each { print it }
// => AB