groovy.lang
Annotation Type Lazy
java.lang.Object
groovy.lang.Lazy
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.FIELD})
@GroovyASTTransformationClass("org.codehaus.groovy.transform.LazyASTTransformation")
@interface Lazy
Field annotation to make a field lazy initializable.
If the field is declared volatile then initialization will be synchronized.
@Lazy volatile T x
becomes
private volatile T $x
T getX () {
if ($x != null)
return $x
else {
synchronized(this) {
$x = new T ()
return $x
}
}
}
By default a field will be initialized by calling its default constructor.
If the field has an initial value expression then this expression will be used instead of default constructor.
In particular, it is possible to use closure { ... } ()
syntax as follows:
@Lazy T x = { [1,2,3] } ()
becomes
private T $x
T getX () {
if ($x != null)
return $x
else {
$x = { [1, 2, 3] } ()
return $x
}
}
@Lazy(soft=true)
will use a soft reference instead of the field and use the above rules each time re-initialization is required.
- author:
- Alex Tkachman
Optional Element Summary |
boolean |
soft
|
Methods inherited from class Object
|
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll |
soft
boolean soft
-
- return:
- if field should be soft referenced instead of hard referenced
- default:
- false
Copyright © 2003-2009 The Codehaus. All rights reserved.