-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onX support for constructor argument annotations #1335
Comments
FTR: Same request on SO. |
The problem with this, is twofold:
A solution, then, would take one of the following two forms:
Both are really hard to 'discover' and not very natural. No nice way to do this, I'm afraid. |
@rzwitserloot I agree the very general case might be hard to support, though I think there is value in supporting more specific, common use cases. I think the use case I'm about to describe is quite typical, but if it's code smell or there's a better way to do it, I'm all ears. My use case is to use Lombok/code generation to lower the barrier to using constructor-based dependency injection (rather than field based). In this use case, for any relevant annotation the annotation is allowed on a field as well as a constructor parameter. Typically the only annotation added is ConfigProperty or a custom binding annotation. I'd see value in being able to use onX to support this, though even if it was supported the next hurdle is to support automatic generation of interfaces from classes as otherwise typically DI doesn't work as the classes are not proxyable. I've seen a few other libraries out there that support interface generation from classes (though they are very old) and as far as I know Lombok does not support this either. So in short you don't think the second bullet "We could just move or copy all annotations..." is worth implementing for this common use case? |
Sure, but for mapping a property file with dozens of lines, it's the only sane way. Anyway, I agree that hardcoding any annotation feels wrong.
To me, it looks like the simplest workable solution. Having to add all binding annotations to the config file sounds like boilerplate, but it allows to save much more boilerplate. It's error-prone as it's easy to forget one annotation, but it's easy to write a test for it (finding all binding annotations is the hard part, but Guava
Right, but DI can't use multiple constructors (there must be either a single constructor or a single constructor annotated with
Sure, this could happen, but binding annotations make sense on constructor arguments, setters and fields, so I don't think anyone ever writes a binding annotation disallowed on fields. For me, either |
Literally ran into this problem at work on Friday. I thought the problem was something else, but as it turns out, Lombok doesn't copy @nAmed for RequiredArgsConstructor. This is very common, especially at my company (a very well known and big company).. so I would like to +1 the hell out of this issue. Even if it's added to experimental, I totally plan on using something like this. |
👍 |
+1 for such a feature. It's not only handy but also promotes constructor DI which promotes clean unit testing. |
I posted about this in Lombok google group.This would be nice, I was thinking on an annotation for the parameters, something like this: |
Using Spring Boot, sometimes I need to inject a |
+1, |
@rspilker The use cases you linked to are for including and excluding, and a few other specific actions. They don't cover arbitrary annotations, which is the original request (and what people have been +1) Specifically, the linked use case does not cover annotations required by dependency injection frameworks, or serialization / deserialization frameworks. Like some others, I'm specifically wanting to be able to add com.fasterxml.jackson.annotation.JsonProperty to each arg of the AllArgsConstructor. Copying the annotation from the member fields would work fine for this case. An AllArgsConstructor property that allows specifying a annotation to add to each arg would be even better. |
Doesn't |
I also missed the |
@janrieke It seems like |
|
I've found this trying to use Spring |
Hello, I noticed the feature proposal mentioned here on the wiki was removed. Can this be reopened? Thank you for your time. |
Feature request to be able to include annotations on generated constructors for
@RequiredArgsConstructor
and/or@AllAgsConstructor
Right now it is possible to add an annotation to the constructor itself via:
but to add annotations to the arguments does not seem to be possible. This is supported via
@Setter
annotation, at least I think, though this related issue hinting maybe the documentation is not quite accurate#1178
I think this could work either by adding an annotation to the fields themselves or some new syntax added to the AllArgsConstructor / RequiredArgsConstructor annotation.
The use case is being able to generate constructors for DI-managed beans that need the arguments to contain additional qualifier annotations to inject the proper bean.
The text was updated successfully, but these errors were encountered: