groovy.lang
Annotation Type Lazy
@Retention(value=SOURCE)
@Target(value=FIELD)
public @interface Lazy
Field annotation to make field lazy initializable.
If field 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 field will be initialized by call to default constructor
If field has initial value expression then this expression will be used instead of default constructor.
In particularly it is possible to use closures { ... } ()
syntax
@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 soft reference instead of the field and use above rules each time re-initialization 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