Your SlideShare is downloading. ×
0
Java Annotations - An Overview
Meta Data <ul><li>Decorates programs with additional information </li></ul><ul><li>Annotations don't directly affect progr...
Why Metadata <ul><li>Many APIs require a fair amount of boilerplate </li></ul><ul><ul><li>Example: JAX-RPC web service req...
Benefits of Metadata <ul><li>Encourages a  declarative  programming style </li></ul><ul><ul><li>Tell computer  what  to do...
Components of the Metadata Facility <ul><li>Syntax for declaring annotation types </li></ul><ul><li>Syntax for annotating ...
Annotation Type Declarations <ul><li>Similar to interface declarations </li></ul><ul><li>Methods define annotation type  e...
Example 1 - Annotation Type Declaration <ul><li>package java.lang.annotation; </li></ul><ul><li>@Documented </li></ul><ul>...
Example 2 - Annotation Type Declaration <ul><li>import java.lang.annotation.*; </li></ul><ul><li>import java.util.EnumSet;...
Annotations <ul><li>Special kind of  modifier </li></ul><ul><ul><li>Can be applied to any declaration </li></ul></ul><ul><...
Example Annotation <ul><li>@AuthorInfo(name = &quot;Unknown&quot;) //Class level </li></ul><ul><li>public class SomeClass ...
Marker Annotations <ul><li>Annotation with out properties </li></ul><ul><li>Example </li></ul><ul><ul><li>public @interfac...
Single Element Annotations <ul><li>public @interface Copyright { </li></ul><ul><li>String value() ; // The name  value  is...
Meta Annotation <ul><li>If you want to say something about the annotation itself </li></ul><ul><ul><li>Where it can be use...
Fetching Annotation Info  <ul><li>Reflection API can be used to explore the  Annotation details at runtime  programmatical...
apt <ul><li>Annotation processing tool </li></ul><ul><li>Command line utility for annotation processing  </li></ul><ul><li...
+ / - <ul><li>Annotation is very powerful </li></ul><ul><li>Better than tagging interfaces </li></ul><ul><li>If intent is ...
References <ul><li>Annotations in Tiger, Part 1: Add metadata to Java code at  http://www-128.ibm.com/developerworks/java/...
Thank U
Upcoming SlideShare
Loading in...5
×

Java Annotation

6,998

Published on

Published in: Technology, Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,998
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
213
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "Java Annotation"

  1. 1. Java Annotations - An Overview
  2. 2. Meta Data <ul><li>Decorates programs with additional information </li></ul><ul><li>Annotations don't directly affect program semantics </li></ul><ul><ul><li>They can affect treatment by tools and libraries </li></ul></ul><ul><li>Can be read from: source, class file, or reflectively </li></ul><ul><li>Ad hoc examples: @override , @deprecated </li></ul><ul><li>Java 1.5 provides a general purpose metadata facility (JSR - 175)‏ </li></ul>
  3. 3. Why Metadata <ul><li>Many APIs require a fair amount of boilerplate </li></ul><ul><ul><li>Example: JAX-RPC web service requires </li></ul></ul><ul><li>paired interface and implementation </li></ul><ul><ul><li>Boilerplate could be generated automatically </li></ul></ul><ul><li>Many APIs require “side files” to be maintained </li></ul><ul><ul><li>Example: deployment descriptor </li></ul></ul><ul><ul><li>Information could be maintained in program </li></ul></ul><ul><li>Many APIs use naming patterns </li></ul><ul><ul><li>Example: JUnit </li></ul></ul><ul><ul><li>Naming pattern could be replace by annotation </li></ul></ul>
  4. 4. Benefits of Metadata <ul><li>Encourages a declarative programming style </li></ul><ul><ul><li>Tell computer what to do, not how to do it </li></ul></ul><ul><li>Should vastly simplify many programming tasks </li></ul><ul><ul><li>Web services, enterprise app development (EJB 3)‏ </li></ul></ul><ul><li>Many other uses, limited only by imagination </li></ul><ul><ul><li>Test frameworks, Parser generators, etc. </li></ul></ul>
  5. 5. Components of the Metadata Facility <ul><li>Syntax for declaring annotation types </li></ul><ul><li>Syntax for annotating declarations </li></ul><ul><li>APIs for reading annotations </li></ul><ul><li>Annotation processing tool (apt)‏ </li></ul>
  6. 6. Annotation Type Declarations <ul><li>Similar to interface declarations </li></ul><ul><li>Methods define annotation type elements </li></ul><ul><li>Method return type: primitives, String , Class , enums, annotations, arrays of same </li></ul><ul><li>Methods have no parameters, throws clause </li></ul><ul><li>Methods can have default value </li></ul>
  7. 7. Example 1 - Annotation Type Declaration <ul><li>package java.lang.annotation; </li></ul><ul><li>@Documented </li></ul><ul><li>@Retention(RetentionPolicy.RUNTIME)‏ </li></ul><ul><li>@Target(ElementType.ANNOTATION_TYPE)‏ </li></ul><ul><li>public @interface Inherited { </li></ul><ul><li>} </li></ul>
  8. 8. Example 2 - Annotation Type Declaration <ul><li>import java.lang.annotation.*; </li></ul><ul><li>import java.util.EnumSet; </li></ul><ul><li>@Retention(RetentionPolicy.RUNTIME) </li></ul><ul><li>@Target({ElementType.TYPE, ElementType.METHOD}) </li></ul><ul><li>@Inherited </li></ul><ul><li>public @interface AuthorInfo </li></ul><ul><li>{ </li></ul><ul><li>public String name(); </li></ul><ul><li>public String email() default &quot;&quot;; </li></ul><ul><li>public String comment() default &quot;&quot;; </li></ul><ul><li>} </li></ul>
  9. 9. Annotations <ul><li>Special kind of modifier </li></ul><ul><ul><li>Can be applied to any declaration </li></ul></ul><ul><ul><li>By convention, precede other modifiers </li></ul></ul><ul><li>Annotation type, element-value pairs </li></ul><ul><ul><li>Values must be compile-time constants </li></ul></ul>
  10. 10. Example Annotation <ul><li>@AuthorInfo(name = &quot;Unknown&quot;) //Class level </li></ul><ul><li>public class SomeClass </li></ul><ul><li>{ </li></ul><ul><li>@AuthorInfo(name = &quot;Unknown&quot;, comment = &quot;bug free&quot;) //Annotation @ method level </li></ul><ul><li>public void foo()‏ </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  11. 11. Marker Annotations <ul><li>Annotation with out properties </li></ul><ul><li>Example </li></ul><ul><ul><li>public @interface Info </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>} </li></ul></ul>
  12. 12. Single Element Annotations <ul><li>public @interface Copyright { </li></ul><ul><li>String value() ; // The name value is special </li></ul><ul><li>} </li></ul><ul><li>@Copyright(&quot;SSL&quot;)‏ </li></ul><ul><li>public class SomeClass { </li></ul><ul><li>... </li></ul><ul><li>} </li></ul>
  13. 13. Meta Annotation <ul><li>If you want to say something about the annotation itself </li></ul><ul><ul><li>Where it can be used, how,etc. </li></ul></ul><ul><li>Retention says whether the VM retains it for reflective access at runtime </li></ul><ul><li>Target's ElementType specifies where the enum can be used: Class, Method, etc. </li></ul>
  14. 14. Fetching Annotation Info <ul><li>Reflection API can be used to explore the Annotation details at runtime programmatically </li></ul><ul><li>isAnnotationPresent() </li></ul>
  15. 15. apt <ul><li>Annotation processing tool </li></ul><ul><li>Command line utility for annotation processing </li></ul><ul><li>Support API with reflection </li></ul><ul><li>Helps with development of code by offering code generation and compilation </li></ul>
  16. 16. + / - <ul><li>Annotation is very powerful </li></ul><ul><li>Better than tagging interfaces </li></ul><ul><li>If intent is to affect documentation, use javadoc </li></ul><ul><li>Otherwise consider use of annotation </li></ul>
  17. 17. References <ul><li>Annotations in Tiger, Part 1: Add metadata to Java code at http://www-128.ibm.com/developerworks/java/library/j-annotate1/ </li></ul><ul><li>How to process a user defined annotation is explained in the following url: http://www.eclipse.org/jdt/apt/introToAPT.html </li></ul><ul><li>Sun Tutorial - http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html </li></ul><ul><li>Annotation Processing Tool - http://java.sun.com/j2se/1.5.0/docs/guide/apt/ </li></ul>
  18. 18. Thank U
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×