groovy.lang
Annotation Type Lazy
@Retention(value=SOURCE)
@Target(value=FIELD)
public @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
|
soft
public abstract boolean soft
- Returns:
- if field should be soft referenced instead of hard referenced
- Default:
- false