merge multiple annotations with parameters

I have a problem with using multiple annotations that all say more or less the same thing but to different frameworks and I would like to group them all into one custom annotation. Currently it looks like this:

@Column(name = "bank_account_holder_name")
@XmlElement(name = "bank_account_holder_name")
@ApiModelProperty(name = "bank_account_holder_name", value = "The name of the recipient bank account holder")
public String bankAccountHolderName;

As you can see they all repeat the same String and I would like to combine them, but I have yet to find a way of doing so.

Is it at all possible to do this, or do I have to either continue doing this or change/create a new framework?


The answer is: probably no, this is not possible (using “standard” java).

You see, there is no inheritance for annotations, yet alone “multiple” inheritance which would allow you to express:

public @interface MultiAnnotiation extends Column, XmlElement, ...

And most likely, those annotations work like this: at runtime the corresponding frameworks uses reflection to check if a certain object has that annotation. And if it doesn’t find “its” annotation, nothing happens.

So you would need a way to “magically” insert those annotations into your class file.

Boiling down to: when you would write your own compiler, and invent something on top of java, then you could do something like that.

Things might be different when dealing with annotations that come with compiler-plugins (meaning: an annotation that is processed at compile time). Maybe you could write your own custom annotation then that triggers the same compile-time operations.

Long story short: all of this sounds interesting, but rather advanced, and most likely: not resulting in robust, production-worthy code!

Leave a Reply

Your email address will not be published. Required fields are marked *