javalombok

Why Lombok does not compile when @Wither is used?


I'm using Lombok 1.18.8:

compileOnly 'org.projectlombok:lombok:1.18.8'

Here is my simple class with @Wither:

@Wither
public class User {
    private int a;
}

But withA() method doesn't appear when I'm trying to call it in another class:

class test {
    User user = new User().withA(1); // withA is red
}

What is wrong with my code?

UPD: Other Lombok annotations work. For example @Setter, @Getter, and @NoArgsConstructor.


Solution

  • Lombok 1.18.8: @Wither

    If you look at the actual implementation of withA() you will notice that it relies on an all-args constructors. To make your example work, try to add it, as well as a no-arg constructor:

    @Wither
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private int a;
    }
    

    The delombok'd version is:

    public class User {
        private int a;
    
        public User withA(int a) {
            return this.a == a ? this : new User(a);
        }
    
        public User(int a) {
            this.a = a;
        }
    
        public User() {
        }
    }
    

    Note: This has been tested with Lombok 1.18.8, IntelliJ IDEA and Lombok plugin.

    Lombok 1.18.10: @With

    @With has been promoted and @Wither deprecated: Simply replace lombok.experimental.Wither with lombok.With. Everything else is similar to 1.18.8:

    @With
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
        private int a;
    }