I'd like to create a method annotation that accepts varargs as a default. Here is the use case:
@Authorize(Role.PRESIDENT, Role.ARMY_GENERAL, Role.INSANE_MADMAN)
public void secureMethod() {
// Code to launch nuclear missile (ok, not really)
}
This is my annotation interface:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Authorize {
Role[] value() default {}; // Tempted to say Rights... value() default {}
}
Is this possible, and if so, how?
No, it's not possible, because varargs are valid only for constructor/method parameters, not for members.
And also, even if you could define value()
with varags, internally the values would be converted to an array anyway, so you wouldn't gain much. Using an array here is just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With