개발/Java

[Lombok][Warning] @Builder will ignore the initializing expression entirely.

nova_dev 2023. 1. 15. 00:00
반응형

warning: @Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder. Default. If it is not supposed to be settable during building, make the field final.

빌드 시에 위 warning이 뜨는 케이스에 대해 알아보자.

@Builder
@Data
@NoArgsConstructor
public class Worker {
    private Long workerId;
    private WorkerType type = WorkerType.BASIC;
}

만약 위와 같은 코드가 있다고 가정해보자. 이 케이스에 클래스는 WorkerType을 초기화하고 싶어서 위와 같이 썼지만 빌드 시에는 아래와 같은 warning이 뜬다.

warning: @Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. If it is not supposed to be settable during building, make the field final.

위와 같이 @Builer, @SuperBuilder를 쓰고 값을 초기화하려 할 경우 초기화 표현을 완전히 무시한다.
만약 초기화하고 싶으면 초기화할 필드에 @Builder.Default를 사용한다.

@Builder
@Data
@NoArgsConstructor
public class Worker {
    private Long workerId;
    @Builder.Default
    private WorkerType type = WorkerType.BASIC;
}

만약 초기화 값을 넣고 이후 변경하지 않는다면 final을 사용해서 불변으로 만든다.

@Builder
@Data
@NoArgsConstructor
public class Worker {
    private final Long workerId;
    private final WorkerType type = WorkerType.BASIC;
}
반응형