1 / 14

Annotation trước tiên được hiểu là một dạng meta data.

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ì đó.

Download Presentation

Annotation trước tiên được hiểu là một dạng meta data.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 đó. VD: với một bài hát thì meta data có thể bao gồm: tên ca sĩ trình bày, tên nhạc sĩ, bài hát này hát trong bao lâu,...

  2. Để 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 { String stringValue();} - Full value @interface MyAnnotation { String stringValue();int intValue();}

  3. 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.

  4. @Override • Click to edit Master text styles • Second level • Third level • Fourth level • Fifth level

  5. 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

  6. 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);    }}

  7. 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");    Iterator it = data.iterator();while (it.hasNext()) {      System.out.println(it.next());    }  }} 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");    Iterator it = data.iterator();while (it.hasNext()) {      System.out.println(it.next());    }  }}

  8. 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. path. Warn about nonexistent path (classpath, sourcepath, etc) directories. serial. Warn about missing serialVersionUID definitions on serializable classes. finally. Warn about finally clauses that cannot complete normally. fallthrough. Check switch blocks for fall-through cases.

  9. 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.

  10. 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 will be repeated until an annotation of this type is found or the root class is reached.

  11. 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. CLASS. Annotations are to be recorded in the class file but not be retained by the JVM. This is the default value. RUNTIME. Annotations are to be retained by the JVM so you can query them using reflection.

  12. 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. FIELD. The annotated annotation type can be used to annotate field declaration. LOCAL_VARIABLE. The annotated annotation type can be used to annotate local variable declaration. METHOD. The annotated annotation type can be used to annotate method declaration. PACKAGE. The annotated annotation type can be used to annotate package declarations. PARAMETER. The annotated annotation type can be used to annotate parameter declarations. TYPE. The annotated annotation type can be used to annotate type declarations.

More Related