Lombok – retain field’s annotation in constructor input params

In version v1.18.4 Lombok added support for copying specific annotations. Meaning, that if you put following setting to lombok.config:

lombok.copyableAnnotations += com.google.inject.name.Named

and apply following Lombok annotations to your class:

@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class Hello {
    @NonNull @Named("my-name") String name;
}

the @Named annotation should be copied to your generated constructor argument.

Limitations: this does not work when annotation can’t be put on a field or annotation on a field overrides constructor initialization

Leave a Comment