+2

Repeat Annotation In Java

Kiến thức yêu cầu( Prerequirement )

Trong một vài trường hợp, bạn cần sử dụng nhiều lần annotation tại cùng 1 vị trí như trên một method, constructor. Ví dụ khi viết cơ chế về authorization và cho phép chỉ có admin và superAdmin có thể tạo 1 user

    // java 7 not allow this format
    @Role("Admin")
    @Role("SuperAdmin")
    public void addUser()
    {
        System.out.println("This is admin app");
    }

1. Với Java 7 và phiên bản thấp hơn

  • Để giải quyết bài toán như trên sẽ có 2 cách:
    • Một là tạo annotation với value() method trả về mảng String của roleName

Khai báo

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Role {

    String[] value() default "Normal";
}

Sử dụng:

    @Role({"Admin", "SuperAdmin"})
    public void addUser()
    {
        System.out.println("Add user");
    }

  • Cách thứ 2 là tạo ra 1 annotation container chứa annotation Role

Khai báo contained annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RoleAnnotation {
    String roleName() default "Normal";
    String systemName() default "Internal App";
}

Khai báo container annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RoleAnnotationContainer {

    RoleAnnotation[] value();
}

Sử dụng:

    // java 7: use same Annotation
    @RoleAnnotationContainer(value = {@RoleAnnotation(roleName = "Admin", systemName = "Administration app"),
                                           @RoleAnnotation(roleName = "SuperAdmin", systemName = "Administration app")})
    public void editUser()
    {
        System.out.println("This is Administration app");
    }

Hoặc:

    @RoleAnnotationContainer({@RoleAnnotation(roleName = "Admin", systemName = "Administration app"),
            @RoleAnnotation(roleName = "Super Admin", systemName = "Administration app")})
    public void deleteUser()
    {
        System.out.println("This is Administration app");
    }

**2. Với Java 8 ** Java 8 hỗ trợ repeat annotation bằng cách sử dụng @Repeatable

  • Khai báo contained annotation: giá trị của attribute value trỏ đến container annotation
@Repeatable(value = NewRoleAnnotationContainer.class)
public @interface RoleRepeatAnnotation {
    String roleName() default "Normal";
    String systemName() default "Internal App";
}
  • Khai báo container annotation: method value trả về 1 mảng contained annotation
public @interface NewRoleAnnotationContainer {
    RoleRepeatAnnotation[] value() default{};
}
  • Sử dụng:
    @RoleRepeatAnnotation(roleName = "Admin", systemName = "Client app")
    @RoleRepeatAnnotation(roleName = "Normal", systemName = "Client app")
    public void getDetail()
    {
        System.out.println("This is client app");
    }

3. Note

  • Lưu ý: khi khai báo Retention cho contained annotation và container annotation. Retention của container annotation phải có giá trị bằng hoặc lớn hơn retention của container annotation.

Ví dụ chúng ta không thể khai báo như sau:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Repeatable(Schedules.class)
public @interface Schedule {
    String dayOfMonth() default "first";
    String dayOfWeek() default "Mon";
    int hour() default 12;
}

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface Schedules {
    Schedule[] value();
}

  • Lưu ý tương tự với khai báo @Target khi sử dụng repeat annotation
@Target(ElementType.PACKAGE)
@Repeatable(Schedules.class)
public @interface Schedule {
    String dayOfMonth() default "first";
    String dayOfWeek() default "Mon";
    int hour() default 12;
}

Và:

@Target(ElementType.METHOD)
public @interface Schedules {
    Schedule[] value();
}

**4. Link tham khảo: **


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí