Annotation Jdk1.6: Pawan Modi Senior R&D Engineer
Annotation Jdk1.6: Pawan Modi Senior R&D Engineer
Pawan Modi
Senior R&D Engineer
Pag
6/25/2009 Senior R&D Engineer e1
Agenda
• Introduction
• First Example
• Defining Annotations
• Annotations Types & Usage
• Example
• Summary
Pag
6/25/2009 Senior R&D Engineer e2
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.
Pag
6/25/2009 Senior R&D Engineer e3
First Example
Pag
6/25/2009 Senior R&D Engineer e4
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
Pag
6/25/2009 Senior R&D Engineer e5
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.
Pag
6/25/2009 Senior R&D Engineer e6
Simple Annotations
Override
◦ This annotation tells compiler that method
must be an overridden method.
Pag
6/25/2009 Senior R&D Engineer e7
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.
Pag
6/25/2009 Senior R&D Engineer e8
Simple Annotations Conti…
Pag
6/25/2009 Senior R&D Engineer e9
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.
Pag
e
6/25/2009 Senior R&D Engineer 10
Simple Annotations Conti…
Pag
e
6/25/2009 Senior R&D Engineer 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
Pag
e
6/25/2009 Senior R&D Engineer 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
Pag
e
6/25/2009 Senior R&D Engineer 13
Meta Annotations Conti . . .
Pag
e
6/25/2009 Senior R&D Engineer 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
Pag
e
6/25/2009 Senior R&D Engineer 15
Meta Annotations Conti…
Pag
e
6/25/2009 Senior R&D Engineer 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.
Pag
e
6/25/2009 Senior R&D Engineer 17
Meta Annotations Conti…
Pag
e
6/25/2009 Senior R&D Engineer 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
Pag
e
6/25/2009 Senior R&D Engineer 19
Meta Annotations Conti…
Pag
e
6/25/2009 Senior R&D Engineer 20
Example
Pag
e
6/25/2009 Senior R&D Engineer 21
Example
Pag
e
6/25/2009 Senior R&D Engineer 22
Example Conti…
Pag
e
6/25/2009 Senior R&D Engineer 23
Summary
Annotation is a special form of syntactic metadata
that can be added to Java source code.
These are not data about data but data about classes,
methods, instance and static variables etc
Pag
e
6/25/2009 Senior R&D Engineer 24
References
Online 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
Pag
e
6/25/2009 Senior R&D Engineer 25
Question ??
Pag
e
6/25/2009 Senior R&D Engineer 26
Thank you!
Pag
e
6/25/2009 Senior R&D Engineer 27