14

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

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Annotation trước tiên được hiểu là một dạng meta data
Page 2: 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ì đó.

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

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

Để 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();}

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

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.

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

@Override

Click to edit Master text stylesSecond level

Third level Fourth level

Fifth level

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

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

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

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

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

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()); } }}

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

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.

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

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.

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

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.

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

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.

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

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.