Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming Course

Similar presentations


Presentation on theme: "Java Programming Course"— Presentation transcript:

1 Java Programming Course
Annotation

2 Session objectives Introduction Anotation - basic Annotation Types The Built-In Annotations from @SuppressWarnings. Custom Annotations

3 Introduction Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate. Annotations have a number of uses, among them: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth. Runtime processing — Some annotations are available to be examined at runtime. Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.

4 The Basics Example to Define an Annotation (Annotation type)
MyAnnotation { String doSomething(); } Example to Annotate Your Code (Annotation) @MyAnnotation (doSomething="What to do") public void mymethod() {

5 Structure of Java Compiler
Parser Type Checker Class File Writer Source File Class File class C { @NonNull Object field; Object p) { field = p; } Object get() { return field; Comments Error

6 Structure of Java5 (and later) Compiler
Parser Type Checker Annotation Checker Class File Writer Source File Class File class C { @NonNull Object field; Object p) { field = p; } Object get() { return field; Program with annotations Error Error Annotation Checker Plugins

7 What can be annotated? Annotatable program elements: method field
package class, including interface enum method field only at compile time local variable formal parameter

8 Annotation Types Marker Single-Element Full-value or multi-value

9 Annotation Types: Marker
Marker annotations take no parameters. They are used to mark a Java element to be processed in a particular way. Example: MyAnnotation { } Usage: @MyAnnotation public void mymethod() { ....

10 Annotation Types: Single-Element
Single-element, or single-value type, annotations provide a single piece of data only. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis. Example: MyAnnotation { String value(); } Usage: @MyAnnotation ("What to do") public void mymethod() { ....

11 Annotation Types: Full-value (multi-value)
Full-value type annotations have multiple data members. Example: Usage:

12 The Built-In Annotations
Java defines seven built-in annotations. Four are imported from java.lang.annotation @Retention, @Documented, @Target, @Inherited. Three are included in java.lang. @Override, @Deprecated,

13 Standard Annotations: The @Override annotation
Override is a Marker Annotation type that can be applied to a method to indicate to the Compiler that the method overrides a method in a Superclass. This Annotation type guards the programmer against making a mistake when overriding a method. Example Program: Whenever you want to override a method, declare the Override annotation type before the method:

14 Standard Annotations: The @Deprecated annotation
This annotation indicates that when a deprecated program element is used, the compiler should warn you about it. If you use or override a deprecated method, you will get a warning at compile time.

15 The @Suppresswarnings annotation
@SuppressWarnings is used to suppress compiler warnings. You can to types, constructors, methods, fields, parameters, and local variables. Before After

16 Meta-Annotations Meta-annotations - types designed for annotating annotation-type declarations (annotations-of-annotations) Meta-annotations: @Target - indicates the targeted elements of a class in which the annotation type will be applicable TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, etc @Retention - how long the element holds onto its annotation SOURCE, CLASS, RUNTIME @Documented - indicates that an annotation with this type should be documented by the javadoc tool @Inherited - indicates that the annotated class with this type is automatically inherited Retention : sự duy trì

17 The Target annotation @Target(ElementType.TYPE)—can be applied to any element of a class @Target(ElementType.FIELD)—can be applied to a field or property @Target(ElementType.METHOD)—can be applied to a method level annotation @Target(ElementType.PARAMETER)—can be applied to the parameters of a method @Target(ElementType.CONSTRUCTOR)—can be applied to constructors @Target(ElementType.LOCAL_VARIABLE)—can be applied to local variables @Target(ElementType.ANNOTATION_TYPE)—indicates that the declared type itself is an

18 The @Retention Annotation
The retention annotation indicates where and how long annotations with this type are to be retained. There are three values: RetentionPolicy.SOURCE—Annotations with this type will be by retained only at the source level and will be ignored by the compiler RetentionPolicy.CLASS—Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VM RetentionPolicy.RUNTIME—Annotations with this type will be retained by the VM so they can be read only at run-time

19 Annotation Processing
It's possible to read a Java program and take actions based on its annotations. To make annotation information available at runtime, the annotation type itself must be annotated Annotation data can be examined using reflection mechanism java.lang.reflect.AccessibleObject: <T extends Annotation> T getAnnotation(Class<T>) Annotation[] getAnnotations() boolean isAnnotationsPresent(<Class<? extends Annotation>)

20 Annotation Processing example (1)

21 Annotation Processing example (2)

22 The @Documented Annotation
The documented annotation indicates that an annotation with this type should be documented by the javadoc tool. By default, annotations are not included in javadoc. But is used, it then will be processed by javadoc-like tools and the annotation type information will also be included in the generated document.

23 The @Documented Annotation

24 The @Documented Annotation
As you can see from the screenshot, there is no annotation-type information for the doSomeTestRetention() method. But, this description is provided for the doSomeTestDocumented() method. This is because of tag attached with your Test_Documented annotation. Your earlier annotation Test_Retention did not include this tag.

25 The @Inherited Annotation
This is a bit of a complex annotation type. It indicates that the annotated class with this type is automatically inherited. More specifically, if you define an annotation with tag, then annotate a class with your annotation, and finally extend the class in a subclass, all properties of the parent class will be inherited into its subclass

26 Custom Annotations To create an annotation type for classes
Syntax: symbol + interface keyword + annotation name Saved with a . java extension. Annotations will be compiled to .class files. To use a class level annotation, the annotation name will precede the class declaration. Parameters can be added to Annotation declaration. Will not have a null value. Can have a default value. Are written as simple methods. Can be arrays of comma-separated values of primitive data type.

27 Example of Annotation for Classes
Output

28 Annotation for Methods
Output

29 Reallife example – JPA Annotations
When using JPA, you can configure the JPA behavior of your entities using annotations: @Entity - designate a plain old Java object (POJO) class as an entity so that you can use it with JPA services – database schema attributes – relationship mappings – inheritance controlling

30 Reallife example – JUnit Annotations
Annotations and support for Java 5 are key new features of JUnit 4: @Test – annotates test method annotates setUp() and tearDown() methods for each test – class-scoped setUp() and tearDown() @Ignore – do not run test

31 Summary

32

33 Thank you all for your attention and patient !
That’s all for this session! Thank you all for your attention and patient ! 33/27


Download ppt "Java Programming Course"

Similar presentations


Ads by Google