How to combinie multiple annotations in one and rename their attributes

Viewed 40

The intention is to group together several spring validation annotations, and rebadge their default attributes. A simpflied example, given:

class @interface A {
    int value();
}
class @interface B {
    int value();
}

I would like to do something like:

class @interface Combo {
    int a(); // have this directed to A.value
    int b(); // have this directed to B.value
}

So in usage it would look like:

@Combo(a = 1, b = 2)

And have the effect of

@A(1)
@B(2)
1 Answers

As far as I understand , Java does not support merging annotations as the compiler does not provide the functionality. It's fairly easy to combine annotations in spring. For instance :

class @interface A {
    int value();
}
class @interface B {
    int value();
}

@A
@B
class @interface Combo {
    int a(); // have this directed to A.value
    int b(); // have this directed to B.value
}

The above code would work on spring and that is how many of the spring-boot annotations are created . For instance , @SpringBootApplication is a combination of three annotations mainly : @ComponentScan, @EnableAutoConfiguration and @SpringBootConfiguration.

Related