akshattandon / projectlombok

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

Allow usage of annotations as meta-annotations #522

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Very often I keep reusing the same set of annotations or even a particular 
configuration of an annotation in different places of my application. E.g. 
assume the following usage to automatically create an all-args constructor 
annotated with @Autowired.

@AllArgsConstructor(onConstructor = @_(@Autowired))

This expression feels rather technical and if I wanted to switch from 
@Autowired to @Inject, I'd have to trace down all usages and change them.

If @AllArgsConstructor was allowed on annotations as well, I could introduce 
the following annotation:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@AllArgsConstructor(onConstructor = @_(@Autowired))
@interface AutowiredConstructor {

}

and then go ahead and use the annotation as follows:

@AutowiredConstructor
class MyComponent {

  private final @NonNull Dependency dependency;

  // business methods
}

To enable this behavior, two changes are required:

1. Add ElementType.ANNOTATION to the annotations currently allowed at types 
(@AllArgsConstructor is definitely not the only annotation that might make 
sense on).
2. Alter the annotation detection to traverse annotation hierarchies.

Original issue reported on code.google.com by oliver.gierke@me.com on 26 May 2013 at 2:19

GoogleCodeExporter commented 9 years ago
Yep this would be very handy indeed :)

Original comment by thomas.d...@googlemail.com on 29 Aug 2013 at 9:11

GoogleCodeExporter commented 9 years ago
This is the same request as issue 567. I don't think that changing @Target is a 
good idea, but there's already a syntax allowing nesting arbitrary annotations 
(and the OP is using it above). So it could look like

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)

@lombok.Metaannotation(@_({
  @AllArgsConstructor(onConstructor = @_(@Autowired)),
  @SomeOtherFancyStuff(1, 2, 3)
)})

@interface AutowiredConstructor {
}

So I believe the syntax is no problem, but I can't tell if this feature can be 
implemented.

Original comment by Maaarti...@gmail.com on 15 Dec 2013 at 3:24