Java Annotation

Thường được sử dụng để lấy data từ class, từ data đó sẽ có các xử lý khác nhau như gen sql từ class entity, đọc file csv và bind to object dựa theo annotation column csv nào ứng với field nào của object.

  • SOURCE: sử dụng bằng cách extends AbstractProcessor và dùng javac để lấy
  • CLASS: sử dụng javap để lấy thông tin
  • RUNTIME: sử dụng như code thông thường để lấy thông tin
  • Annotation ở super class có thể được detect ở subclass khi sử dụng @Inherited ở Annotation, và chỉ có tác dụng khi khai báo cho super class, không phải cho interface. Khi đó cần sử dụng getAnnotations() ở subclass thay vì getDeclaredAnnotations()
  • @Repeatable: Khi sử dụng annotation này khi khai báo một annotation(annotation x), annnotation này có thể chỉ định 1 annotation khác (annotation y) có thể sử dụng value = array annotation x
  • Target = TYPE_USE được sử dụng ở nhiều nơi, bao gồm cả catch, exception, thường là để check design.
@Repeatable(MyRepeatableAnnotations.class)
public @interface MyRepeatableAnnotation {
    int value() default 0;
}

public @interface MyRepeatableAnnotations {
    MyRepeatableAnnotation[] value();
}


// --Marker Annotation,  Repeatable
@MyRepeatableAnnotation

// --Single Element Annotation,  Repeatable
// Do not have to specify element name if it is 'value'
@MyRepeatableAnnotation(10)
public class TestingAnnotatedClasses {}

Common Annotation

@SafeVarargs ở nơi khai báo= @SuppressWarnings({“unchecked”, “varargs”})

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *