Skip to content

Ability to specify Field Annotations that gets taken over with @XArgsConstructor #1528

@marcus-held

Description

@marcus-held

Hi,

I use Lombok together with Spring and I inject all dependencies via constructor injection. But because I'm lazy I don't want to write the constructor on my own and rather use one of the @XArgsConstructor's. But with Spring you might want to specify some dependecies by name. In order to do so you need to annotate the parameter in the constructor with @qualified("someName"). And here lies the problem, of course I can not use the generated constructor and annotate just the field, because the annotation must be on the parameter of the constructor. So what I would like to have is the ability to define specific annotations in the lombok.config file which are taken over from the field into the parameter of the constructor. That way I would be able to build up the behavior that feels more natural.

Here are some examples:

@RequiredArgsConstructor
public class WorksFine {

  private final SomeDependency dependency; // Gets injected by Spring

}
@RequiredArgsConstructor
public class DoesNotWork {

  @Qualifier("specificDependency")
  private final SpecificDependency dependency; // Can not find the dependency by the qualifier

}
// :'-( Missing annotation
public class CurrentWorkaround {

  private final SpecificDependency dependency; 

  public CurrentWorkaround(@Qualifier("specificDependency") SpecificDependency dependency) {
    this.dependency = dependency;
  }

}

Proposed Solution:
lombok.config

lombok.xArgsConstructor.fieldAnnotationTakeOver += org.springframework.beans.factory.annotation.Qualifier

java class:

@RequiredArgsConstructor
public class Works {

  @Qualifier("specificDependency")
  private final SpecificDependency dependency;

}

generated java class:

public class Works {

  private final SpecificDependency dependency;

  public Works(@Qualifier("specificDependency") SpecificDependency dependency) {
    this.dependency = dependency;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions