Annotation trước tiên được hiểu là một dạng meta data. Meta data là đặc tả dữ liệu cho một đối tượng, giá trị gì đó. VD: các tập tin mp3, ảnh, hoặc một bài viết có thể có meta data dạng XML Format là RSS. Đặc tả dữ liệu là một tập giá trị chứa những thông tin gắn gọn, cơ bản mô tả về đối tượng nào đó. Để khai báo 1 annotation, bắt đầu bằng 1 dấu “@”, theo sau là từ khóa interface và tên của annotation. Có 3 kiểu annotation: - Maker: không có phần tử, chỉ có duy nhất tên của annotation @interface MyAnnotation {} - Single-element: chỉ chứa 1 dữ liệu đơn lẻ trong annotation @interface MyAnnotation { Java defines seven built-in annotations. Four are imported from java.lang.annotation: @Retention, @Documented, @Target, and @Inherited. Three, @Override, @Deprecated, and @SuppressWarnings, are included in java.lang. @Override Click to edit Master text styles Second level Third level Fourth level Fifth level Deprecated Deprecated is a marker annotation type that can be applied to a method or a type (class/interface) to indicate that the method or type is deprecated SuppressWarnings import java.util.Date; public class Main { @SuppressWarnings(value={"deprecation"}) public static void main(String[] args) { Date date = new Date(2009, 9, 30); System.out.println("date = " + date); } } import java.util.ArrayList; import java.util.Iterator; public class Main { @SuppressWarnings("unchecked") public static void main(String[] args) { ArrayList data = new ArrayList(); data.add("hello"); data.add("world"); SuppressWarnings SuppressWarnings is used to suppress compiler warnings. You can apply @SuppressWarnings to types, constructors, methods, fields, parameters, and local variables. The following are valid parameters to @SuppressWarnings: unchecked. Give more detail for unchecked conversion. Documented Documented is a marker annotation type used to annotate the declaration of an annotation type so that instances of the annotation type will be included in the documentation. Override annotation type is not annotated using Documented. Deprecated annotation type is annotated @Documented. Inherited Use Inherited to annotate an annotation type, any instance of the annotation type will be inherited. Use Inherited to annotate a class, the annotation will be inherited by any subclass of the annotated class. If the user queries the annotation type on a class declaration, and the class declaration has no annotation of this type, then the class's parent class will automatically be queried for the annotation type. This process Retention @Retention indicates how long annotations whose annotated types are annotated @Retention are to be retained. The value of @Retention can be one of the members of the java.lang.annotation.RetentionPolicy enum: SOURCE. Annotations are to be discarded by the Java compiler. Target Target indicates which program element(s) can be annotated using instances of the annotated annotation type. The value of Target is one of the members of the java.lang.annotation.ElementType enum: ANNOTATION_TYPE. The annotated annotation type can be used to annotate annotation type declaration. CONSTRUCTOR. The annotated annotation type can be used to annotate constructor declaration.