annotations

27
Annotation Jdk1.6 Pawan Modi Senior R&D Engineer 6/25/2009 Senior R&D Engineer Pag e 1

Upload: pawan

Post on 15-Nov-2014

126 views

Category:

Documents


4 download

DESCRIPTION

Brief introduction of Annotations a new features introduced with java1.5 & enhanced with java1.6. Very handy ppt to understand the concept of annotation with simple & pictorial examples. Annotations are the wild use features by framework builders like spring, hibernate. Read it once & build a better understanding of java annotations.

TRANSCRIPT

Annotation Jdk1.6

Pawan Modi Senior R&D Engineer

6/25/2009 Senior R&D EngineerPage 1

Agenda

• Introduction• First Example• Defining Annotations• Annotations Types & Usage• Example• Summary

6/25/2009 Senior R&D EngineerPage 2

Introduction

Annotations are generally a way to add meta-data information to an element.

An annotation type definition takes an "at" (@) sign, followed by the interface keyword plus the annotation name.

An element can be a class, method, field, constructor, variables etc.

This meta-data are processed by the tools i.e. compilers, javadoc, etc.

6/25/2009 Senior R&D EngineerPage 3

First Example

6/25/2009 Senior R&D EngineerPage 4

Defining Annotation Some rules to define an annotation type Annotation declaration should start with an 'at' sign like

@, following with an interface keyword, following with the annotation name.

Method declarations should not have any parameters. Method declarations should not have any throws

clauses. Return types of the method should be one of the

following: primitives String Class enum array of the above types

6/25/2009 Senior R&D EngineerPage 5

Annotation Types There are two types of annotations available in JDK5◦ Simple annotations: Just to annotate your code. Developer cannot use them to create a custom annotation type. There are only three types of simple annotations provided by JDK5 Override Deprecated Suppresswarnings

◦ Meta annotations: These are the annotation types designed for annotating annotation-

type declarations. Simply speaking, these are called the annotations-of-annotations.

6/25/2009 Senior R&D EngineerPage 6

Simple AnnotationsOverride ◦ This annotation tells compiler that method

must be an overridden method.

6/25/2009 Senior R&D EngineerPage 7

Simple Annotations Conti…

Deprecated ◦ This annotation informs client not to use this

method / class anymore because in the future versions this old method / class may not be supported. ◦ Clients may prepare themselves not to

depend on the old method anymore.

6/25/2009 Senior R&D EngineerPage 8

Simple Annotations Conti…

6/25/2009 Senior R&D EngineerPage 9

Simple Annotations Conti…

SuppressWarnings ◦ From Java 5.0, references to any of the

Collection types should be parameterized. Else the compiler will issue a warning message. The solution for this problem is to make use of @SuppressWarnings annotation.

6/25/2009 Senior R&D Engineer

Page

10

Simple Annotations Conti…

6/25/2009 Senior R&D Engineer

Page 11

Annotation Types Conti…

Meta annotations: ◦ These are the annotation types designed for

annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations. ◦ There are four types of meta-annotations. Target Retention Documented Inherited

6/25/2009 Senior R&D Engineer

Page

12

Meta Annotations Target

tells for which element type this annotation is applicable It contains the following enumerated types as its value: @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 annotation type

6/25/2009 Senior R&D Engineer

Page

13

Meta Annotations Conti . . .

6/25/2009 Senior R&D Engineer

Page

14

Meta Annotations Conti…Retention Tells what extent the Annotations should be

retained. The three possible ways of telling this are, 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

6/25/2009 Senior R&D Engineer

Page

15

Meta Annotations Conti…

6/25/2009 Senior R&D Engineer

Page

16

Meta Annotations Conti…Document◦ indicates that an annotation with this type

should be documented by the javadoc tool ◦ By default, annotations are not included in

javadoc ◦ But if @Documented is used, it then will be

processed by javadoc-like tools and the annotation type information will also be included in the generated document.

6/25/2009 Senior R&D Engineer

Page

17

Meta Annotations Conti…

6/25/2009 Senior R&D Engineer

Page

18

Meta Annotations Conti…Inherited◦ It indicates that the annotated class with this

type is automatically inherited. ◦ if you define an annotation with the

@Inherited 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

6/25/2009 Senior R&D Engineer

Page

19

Meta Annotations Conti…

6/25/2009 Senior R&D Engineer

Page

20

Example

6/25/2009 Senior R&D Engineer

Page

21

Example

6/25/2009 Senior R&D Engineer

Page

22

Example Conti…

6/25/2009 Senior R&D Engineer

Page

23

Summary Annotation is a special form of syntactic metadata

that can be added to Java source code.

They provide information about various elements of a Java class.

Classes, methods, variables, parameters and packages may be annotated.

There are seven annotations provided in the J2SE 5.0 release. @Deprecated, @Override, @SuppressWarnings,

@Documented, @Inherited, @Retention and @Target.

These are not data about data but data about classes, methods, instance and static variables etc

6/25/2009 Senior R&D Engineer

Page

24

ReferencesOnline Tutorial

http://en.wikipedia.org/wiki/Java_annotation http://www.javabeat.net/articles/30-annotations-

in-java-50-1.html http://www.developer.com/java/other/article.php/

10936_3556176_1 http://java.sun.com/j2se/1.5.0/docs/guide/language/

annotations.html

6/25/2009 Senior R&D Engineer

Page

25

Question ??

Kindly send all you queries to the following id.

[email protected]

6/25/2009 Senior R&D Engineer

Page

26

Thank you!

6/25/2009 Senior R&D Engineer

Page

27