10

I have java 1.7. Is there any way to group multiple annotations to single one. So that i annotate with single annotation and gets all the properties of all grouped multiple annotations. I want to avoid multiple annotation lines repeated every time. Can we use the 3 annotation into one Does java core libraries or others support this. Thnx

Rahul
  • 295
  • 3
  • 14
  • Have you tried `@interface Combined extends A, B, C`? (not sure if that would work) – assylias Nov 13 '14 at 13:30
  • Its answered here already. http://stackoverflow.com/questions/13401581/how-to-create-an-annotation-that-is-a-group-of-annotations – Sanjeev Nov 13 '14 at 13:41

2 Answers2

5

No, that's not possible. An established idiom for this is to meta-annotate the @Combined annotation with its composing annotations:

@A
@B
@C
public @interface Combined {}

This pattern expresses that annotating an element with @Combined is equivalent to specifying @A, @B and @C at that element.

This of course requires the library consuming these annotations to be aware and make use of this pattern. That's e.g. the case for constraint annotations in Bean Validation or stereotypes in CDI.

Gunnar
  • 18,095
  • 1
  • 53
  • 73
3

I'm not sure if this is what you're looking for, but CDI provide stereotype annotations.

Puce
  • 37,247
  • 13
  • 80
  • 152