Lombok - retain field's annotation in constructor input params
Asked Answered
H

2

47

Lombok misses field's annotation while auto generating constructor. Is there a way to retain field's annotation in constructor input params?

Class to generate constructor,

@RequiredArgsConstructor(onConstructor = @__(@Inject))
public class Test {

    @Named("MyField")
    private final String field;
    @Named("MyHandler")
    private final SomeHandler handler;
}

Generated class :

public class Test {

    @Named("MyField")
    private final String field;
    @Named("MyField")
    private final SomeHandler handler;

    @Inject
    public Test(final String field, final SomeHandler handler) {
        this.field = field;
        this.handler = handler;
    }
}

Desired class :

public class Test {

    @Named("MyField")
    private final String field;
    @Named("MyHandler")
    private final SomeHandler handler;

    @Inject
    public Test(@Named("MyField")final String field, 
                @Named("MyHandler")final SomeHandler handler) {
        this.field = field;
        this.handler = handler;
    }

}
Harville answered 1/3, 2017 at 8:5 Comment(2)
I don't think this ever got implemented as per: groups.google.com/forum/#!topic/project-lombok/2vaujDkV8NwRinglet
@Ringlet I'm afraid, you're right. I started implementing it years ago, but it was more complicated than I thought. Nobody cared and I found out that I myself needed it only rarely and there was a workaround.Accrescent
A
64

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

Attenuant answered 8/2, 2019 at 16:53 Comment(0)
A
13

There's no such feature and it looks like nobody cares. I proposed it once and started to implement it, but gave up (no demand and too much work).

It could look like

@RequiredArgsConstructor(onConstructor=@__(@Inject))
public class Something {
    @OnConstructor(@Named("userName"))
    private final String userName;

    @OnConstructor(@Named("userPassword"))
    private final String userPassword;

    private final int anotherField;

    private final int yetAnotherField;
}

or maybe just

@RequiredArgsConstructor(
     onConstructor=@__(@Inject),
     moveToConstructorArg=@__(@Named))
public class Something {
    @Named("userName")
    private final String userName;

    @Named("userPassword")
    private final String userPassword;

    private final int anotherField;

    private final int yetAnotherField;
}

or it could be controlled using lombok.config as you probably want all @Named annotations to be moved to the constructor.

I'm afraid, if you want it, then you have to do it yourself (my incomplete implementation might help you a bit).

FTR: There's a feature request now.

Accrescent answered 7/3, 2017 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.