I've tried to use Lombok's @RequiredArgsConstructor annotation with the @Builder annotation. When I do the following:
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class Test
{
private final String email;
private final String name;
private String password;
}
Everything works fine and as expected, but when I add the @Builder annotation:
import lombok.Builder;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@Builder
public class Test
{
private final String email;
private final String name;
private String password;
}
I get the following error during the build:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project example: Compilation failure
[ERROR] /C:/[...]/Test.java:[9,1] constructor Test in class [...]Test cannot be applied to given types;
[ERROR] required: java.lang.String,java.lang.String
[ERROR] found: java.lang.String,java.lang.String,java.lang.String
[ERROR] reason: actual and formal argument lists differ in length
Is this a Lombok bug or am I doing something wrong?
I'm using Lombok in version 1.18.4.
EDIT: Ok, thank you for your answers. Now it gets a little bit more complex. I want to build an OkHttpClient inside the generated constructor.
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import okhttp3.OkHttpClient;
import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Builder
public class Test
{
private final String email;
private final String name;
private String password;
@Builder.Default
private final long timeout = 60000L;
private OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(timeout, TimeUnit.MILLISECONDS).build();
}
When I'm doing so i get this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project example: Compilation failure
[ERROR] /C:/[...]/Test.java:[25,79] variable timeout might not have been initialized
Is there any possibility to do so without writing the code of the constructor on my own? This is also the reason why I tried to avoid using the @AllArgsConstructor annotation.