Using Intellij and the lombok plugin version 203.5981.41, when I delombok to see what code is generated I do not get the AtomicReference the lombok GetterLazy docs suggest:
@Getter(lazy=true)
private final double[] cached = expensive();
private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}
Generates this in Intellij:
private final double[] cached = expensive();
private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}
Not this as the docs suggest:
private final java.util.concurrent.AtomicReference<java.lang.Object> cached = new java.util.concurrent.AtomicReference<java.lang.Object>();
public double[] getCached() {
java.lang.Object value = this.cached.get();
if (value == null) {
synchronized(this.cached) {
value = this.cached.get();
if (value == null) {
final double[] actualValue = expensive();
value = actualValue == null ? this.cached : actualValue;
this.cached.set(value);
}
}
}
return (double[])(value == this.cached ? null : value);
}
private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}
Clearly, this is not lazy and so is of limited use, is this a change in how lombok works for lazy getters, or is this a bug in how Delombok works in Intellij?
Or have I done something wrong and should have implemented something else/something I missed?
As pointed out by @chris the issue is with the Lombok plugin in intellij.
This is a limitation of the Delombok feature of the IntelliJ Lombok plugin. It doesn't seem to recognize
lazy=true
on@Getter
. Try to verify the behavior with a simple test like this instead, you'll see it works as expected: