50

A year or so I read an article that explained how I could create an annotation that basically is a container for other annotations. This way if I always use the same 5 annotations in a specific use-case I create an annotation that contains them and use that instead.

Unfortunately, I can't find the article anymore and would really like to do that right now for my jackson configuration.

Since I can't find any information on that on my own I'm beginning to question my memory. Is this possible or I am just wrong?

EDIT

What i want is something like:

@Target(ElementType.METHOD)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(using=MySerializerThatIsUsedEverywhere.class
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(MyCustomXmlAdapter.class)
@SomeOtherEvaluatedByTheSerializer
public @interface SerializerUseCase01 {
    public String a();
    public int b();
)

my scenario is that i have a bunch of serialization use cases that can be handled by the same serializer with different configs. To make everything easier to use and more transparent i want to wrap the jackson config and the serializer config into one annotation.

pirho
  • 11,565
  • 12
  • 43
  • 70
Laures
  • 5,389
  • 11
  • 50
  • 76

2 Answers2

48

For Jackson, this can be done with @JacksonAnnotationsInside meta-annotation. See this article for more, but code snippet from there is:

@Retention(RetentionPolicy.RUNTIME) // IMPORTANT
@JacksonAnnotationsInside
@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({ "id", "name" }) 
public @interface MyStdAnnotations

and from thereon you can use this type for your own classes like so:

@MyStdAnnotations
public class MyBean {
   public String name, id;
}
StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Does this work only thanks to `@JacksonAnnotationsInside`? Is there something like groovy's meta-annotations (`groovy.transform.AnnotationCollector`)? In Groovy documentation it says that `INFO: Meta-annotations are a Groovy feature only. There is no chance for you to annotate a Java class with a meta-annotation and hope it will do the same as in Groovy. Likewise, you cannot write a meta-annotation in Java: both the meta-annotation definition and usage have to be Groovy code.`, but since this works then there is some possiblity. – Shadov Apr 12 '18 at 16:32
  • Unless Groovy annotations are handled by some other processor to expand them, no, Jackson does not recognize them. However, if it would behave similar to `JacksonAnnotationsInside`, it is actually possible to define "aliases" by overriding a method in `JacksonAnnotationIntrospector`, which is responsible for detecting annotation bundles. – StaxMan Apr 17 '18 at 05:55
  • 1
    Thanks! The key is `@JacksonAnnotationsInside` otherwise jackson ignores any jackson-specific annotations added. I tried `@Inherited` and it did not work. – GameSalutes Aug 22 '18 at 19:37
4

There are some examples here on how to make various combinations of annotations containing other annotations. Is this what you're looking for?

Example from the source:

@Target(ElementType.METHOD)
public @interface SimpleAnnotation {
    public String a();
    public int b();
)

@Target(ElementType.METHOD)
public @interface ReallyComplexAnnotation {
    public SimpleAnnotation[] value();
)

Used like this:

@ReallyComplexAnnotation(
    { @SimpleAnnotation(a="...", b=3), @SimpleAnnotation(a="...", b=4) }
)
Vala
  • 5,628
  • 1
  • 29
  • 55
  • Hmm. So you want `SerializerUseCase01` to be something akin to an alias for a bunch of other (hidden as an implementation detail) interfaces, is that correct? – Vala Nov 15 '12 at 16:33
  • Correct. But I'm not sure anymore that is possible as none of the annotations i want to "hide" is marked with `@Target({ElementType.ANNOTATION_TYPE,...}`. – Laures Nov 15 '12 at 16:35
  • I've never tried to do something like that, but looking for it I've come across what looks like it [here](http://cyrille.martraire.com/2010/07/patterns-for-using-annotations/) (scroll down to "Meta-inheritance" - the "PayPal" example looks particularly like this). By the looks of that any annotations on your annotation will be "added" to your annotation. Maybe I'm reading it wrong, but worth a go. – Vala Nov 15 '12 at 16:38