diff options
-rw-r--r-- | doc/changelog.markdown | 1 | ||||
-rw-r--r-- | usage_examples/GetterLazyExample_post.jpage | 19 | ||||
-rw-r--r-- | website/features/GetterLazy.html | 10 |
3 files changed, 13 insertions, 17 deletions
diff --git a/doc/changelog.markdown b/doc/changelog.markdown index e922f8c9..c9dafc61 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,6 +2,7 @@ Lombok Changelog ---------------- ### v0.11.9 (Edgy Guinea Pig) +* CHANGE: The desugaring of @Getter(lazy=true) is now less object creation intensive. Documentation has been updated to reflect what the new desugaring looks like. [@Getter(lazy=true) documentation](http://projectlombok.org/features/GetterLazy.html). * PROMOTION: `@Value` has been promoted from experimental to the main package with no changes. The 'old' experimental one is still around but is deprecated, and is an alias for the new main package one. [@Value documentation](http://projectlombok.org/features/Value.html). * FEATURE: {Experimental} `@Builder` support. One of our earliest feature request issues, [Issue #16](https://code.google.com/p/projectlombok/issues/detail?id=16), has finally been addressed. [@Builder documentation](http://projectlombok.org/features/experimental/Builder.html). * FEATURE: `@NonNull` on a method or constructor parameter now generates a null-check statement at the start of your method. This nullcheck will throw a `NullPointerException` with the name of the parameter as the message. [Issue #514](https://code.google.com/p/projectlombok/issues/detail?id=514) diff --git a/usage_examples/GetterLazyExample_post.jpage b/usage_examples/GetterLazyExample_post.jpage index 9f4b1ba3..afed1748 100644 --- a/usage_examples/GetterLazyExample_post.jpage +++ b/usage_examples/GetterLazyExample_post.jpage @@ -1,18 +1,19 @@ public class GetterLazyExample { - private double[] $lombok$lazy1v; - private volatile boolean $lombok$lazy1i; - private final Object $lombok$lazyLock = new Object[0]; + private final java.util.concurrent.AtomicReference<java.lang.Object> cached = new java.util.concurrent.AtomicReference<java.lang.Object>(); public double[] getCached() { - if (!this.$lombok$lazy1i) { - synchronized (this.$lombok$lazyLock) { - if (!this.$lombok$lazy1i) { - this.$lombok$lazy1v = expensive(); - this.$lombok$lazy1i = true; + java.lang.Object value = this.cached.get(); + if (value == null) { + synchronized(value) { + value = this.cached.get(); + if (value == null) { + final double[] actualValue = expensive(); + value = actualValue == null ? this.cached : actualValue; + this.cached.set(value); } } } - return this.$lombok$lazy1v; + return (double[])(value == this.cached ? null : value); } private double[] expensive() { diff --git a/website/features/GetterLazy.html b/website/features/GetterLazy.html index f70c8e78..c6d21f01 100644 --- a/website/features/GetterLazy.html +++ b/website/features/GetterLazy.html @@ -15,12 +15,7 @@ <div class="overview"> <h3>Overview</h3> <p> - <em>NEW IN Lombok 0.10: </em>You can let lombok generate a getter which will calculate a value once, the first time this getter is called, and cache it from then on. This can be useful - if calculating the value takes a lot of CPU, or the value takes a lot of memory. To use this feature, create a <code>private final</code> variable, - initialize it with the expression that's expensive to run, and annotate your field with <code>@Getter(lazy=true)</code>. The field will be hidden from the - rest of your code, and the expression will be evaluated no more than once, when the getter is first called. There are no magic marker values (i.e. even - if the result of your expensive calculation is <code>null</code>, the result is cached) and your expensive calculation need not be thread-safe, as lombok - takes care of locking. + <em>NEW IN Lombok 0.10: </em>You can let lombok generate a getter which will calculate a value once, the first time this getter is called, and cache it from then on. This can be useful if calculating the value takes a lot of CPU, or the value takes a lot of memory. To use this feature, create a <code>private final</code> variable, initialize it with the expression that's expensive to run, and annotate your field with <code>@Getter(lazy=true)</code>. The field will be hidden from the rest of your code, and the expression will be evaluated no more than once, when the getter is first called. There are no magic marker values (i.e. even if the result of your expensive calculation is <code>null</code>, the result is cached) and your expensive calculation need not be thread-safe, as lombok takes care of locking. </p> </div> <div class="snippets"> @@ -38,8 +33,7 @@ <div class="overview"> <h3>Small print</h3><div class="smallprint"> <p> - Lombok actually creates a few fields all prefixed with <code>$lombok$</code> to cache the value. You should not rely on the exact type, name, and structure - of these fields as future implementations may change them. To access the lazily initialized value, <em>always</em> use the generated getter. + You should never refer to the field directly, always use the getter generated by lombok, because the type of the field will be mangled into an <code>AtomicReference</code>. Do not try to directly access this <code>AtomicReference</code>; if it points to itself, the value has been calculated, and it is <code>null</code>. If the reference points to <code>null</code>, then the value has not been calculated. This behaviour may change in future versions. Therefore, <em>always</em> use the generated getter to access your field! </p><p> Other Lombok annotations such as <code>@ToString</code> always call the getter even if you use <code>doNotUseGetters=true</code>. </p> |