Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you accept varargs as a value in an annotation?

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?

like image 671
sparkyspider Avatar asked Apr 22 '15 13:04

sparkyspider


1 Answers

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.

like image 181
Konstantin Yovkov Avatar answered Sep 21 '22 16:09

Konstantin Yovkov