05-05-2012, 11:30 AM
ava Annotations
Annatation.docx (Size: 32.21 KB / Downloads: 28)
Annotation-based Java development is certainly one of the most notable recent development trends. Annotation-based development relieves Java developers from the pain of cumbersome configuration. First introduced in Java 5.0, annotations are one of the features in that JDK version that shifted the responsibility for writing boilerplate Java code from the programmer to the compiler. When the source code is free of boilerplate code, it becomes easier to maintain. The resulting code is also less likely to contain bugs.
Java annotations are one of the main ease-of-development features introduced in JDK 5. Annotations are like meta-tags that you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters and variables. They provide a helpful way to indicate whether your methods are dependent on other methods, whether they are incomplete, whether your classes have references to other classes, and so on.
Quoting from Oracle's official site, "It [annotation-based development] lets us avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a declarative programming style where the programmer says what should be done and tools emit the code to do it."
Simply speaking, an annotation is a mechanism for associating a meta-tag with program elements and allowing the compiler or the VM to extract program behaviors from these annotated elements and generate interdependent code when necessary.
In the first part of this three-article series, I'll describe some basics of Java annotations, their benefits, as well as provide some example usages.
Java Annotations Basics
There are two things you need to consider with annotations. One is the "annotation" itself; another is the "annotation type." An annotation is the meta-tag that you will use in your code to give it some life. Annotation type is used for defining an annotation. You will use it when you want to create your own custom annotation. The type is the actual construct used, and the annotation is the specific usage of that type.
Rules of Thumb for Defining Java Annotation Types
Here are some rules-of-thumb when defining an annotation type:
1. Annotation declaration should start with an 'at' sign like @, following with an interface keyword, following with the annotation name.
2. Method declarations should not have any parameters.
3. Method declarations should not have any throws clauses.
4. Return types of the method should be one of the following:
primitives
String
Class
enum
array of the above types
Java Annotation Types
There are two types of annotations available with JDK5:
• Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.
• Meta annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.