Close

Java Annotations - @Repeatable Examples

Java Annotations Java 

@Retention(RetentionPolicy.RUNTIME)
public @interface MultiAccess {
Access[] value();
}
Original Post




import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MultiAccess.class)
public @interface Access {
Role value() default Role.GUEST;
String env();
}
Original Post




public class UiApplication {
@MultiAccess({
@Access(value = Role.ADMIN, env = "prod"),
@Access(value = Role.SUPER_USER, env = "test")})
public View getAccountView() {
return createAccountView();
}

private View createAccountView() {
//just returning null, it's not important in this example
return null;
}

public static void main(String[] args) throws NoSuchMethodException {
for (Access a : UiApplication.class.getMethod("getAccountView")
.getDeclaredAnnotationsByType(Access.class)) {
System.out.println(a.value());
}

//another way
MultiAccess m = UiApplication.class.getMethod("getAccountView")
.getDeclaredAnnotation(MultiAccess.class);
for (Access a : m.value()) {
System.out.println(a.value());
}

}
}
Original Post




See Also