akshattandon / projectlombok

Automatically exported from code.google.com/p/projectlombok
0 stars 0 forks source link

Feature request: @InjectConstructor #729

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Feature request: Generate constructor for use with JSR 330
Generates a @RequiredArgsConstructor or @AllArgsConstructor for use with 
injection frameworks. In addition to the normal generated constructors, it 
would:
* Put @javax.inject.Inject on the constructor
* For each constructor parameter, add any annotation which is annotated with 
@Qualifier (for bonus points, also consider 
com.google.inject.BindingAnnotation) from the corresponding field
* Emit an error if any of the fields are annotated with @Inject (easy mistake 
to make)

Could be a parameter on the constructor annotations instead of a new annotation.

I'm willing to implement this myself. Roel & Reinier, you know where to find me.

Original issue reported on code.google.com by jorn86 on 20 Aug 2014 at 10:28

GoogleCodeExporter commented 9 years ago
This feature request will not work unless the following holds in 99%+ of all 
cases:

_IF_ someone creates their own qualifier annotation (and thus, sticks 
@Qualifier or possibly @BindingAnnotation on their public @interface 
declaration), then either:

(A) there is no @Target annotation at all, or

(B) the @Target annotation always includes at least both ElementType.FIELD and 
ElementType.PARAMETER

Assuming that's true, we need to resolve the annotation in order to know what 
annotations are on the annotation definition. In fact, we have to resolve _ALL_ 
annotations on _ALL_ fields in order to know which ones are @Qualifiers / 
@BindingAnnotation-marked.

Resolving is very very tricky, so, I don't think this is worth it - assuming it 
can be done in the first place (which requires these things to always have no 
@Target or both param/field, as described earlier).

Original comment by r.spilker on 21 Aug 2014 at 8:35

GoogleCodeExporter commented 9 years ago
As far as I know, B is required by the spec, so that shouldn't be an issue (and 
we can simply emit an error if the annotation doesn't have @Target PARAMETER).

Original comment by jorn86 on 21 Aug 2014 at 9:10

GoogleCodeExporter commented 9 years ago
No idea about the spec, but I'd say that a binding annotation actually always 
allows targets FIELD and PARAMETER. Normally, you use constructor injection 
(i.e. PARAMETER), but sometimes it's not possible and you switch to FIELD (or 
method). Moreover, the annotation comes usually from the programmer who uses 
it, so they can change the @Target, if needed.

The resolution is a different story, so I'm afraid we've to stick with 
@OnConstructor for a while.

Original comment by Maaarti...@gmail.com on 27 Aug 2014 at 3:38