groovy.lang
@Retention(value=SOURCE) @Target(value=FIELD) public @interface Lazy
@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.Modifier and Type | Optional Element and Description |
---|---|
boolean |
soft |