Lombok @Getter(lazy=true) does not appear to work

874 views Asked by At

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.

1

There are 1 answers

0
chris On

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:

public class LombokTests {

    @Test
    public void lazyGettersAreNotEagerlyCalled() {
        var foobarCache = new FoobarCache();
        assertThat(foobarCache.foobarFetchedTimes)
                .describedAs("Lazy means lazy!")
                .isEqualTo(0);
    }

    @Test
    public void lazyGettersAreCalledOnlyOnce() {
        var foobarCache = new FoobarCache();
        System.out.println(foobarCache.getFoobar());
        System.out.println(foobarCache.getFoobar());
        assertThat(foobarCache.foobarFetchedTimes)
                .describedAs("Repeated calls to lazy getter should be cached")
                .isEqualTo(1);
    }

    static class FoobarCache {
        @Getter(lazy = true)
        private final String foobar = fetchFoobarFromSuperExpensiveExternalApi();
        private int foobarFetchedTimes = 0;

        private String fetchFoobarFromSuperExpensiveExternalApi() {
            this.foobarFetchedTimes++;
            return "foobar";
        }
    }
}