Annotation: From English Dictionary, it's the explanation/comments/Remarks/Observations/Notes/Clarifications.
Annotations in Java: Annotations are like meta-tags that you can add to your code and apply them to package declarations, type declarations, constructors, methods, fields, parameters, and variables. As a result, you will have helpful ways 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.
Pros & cons of Annotations:
Pros :1) Annotations are relatively simple to use and understand.2) Compilation time requires less for annotations when compared to the xml-configurations.3) Annotations are used for flexibility.4) Annotations can be used by the compiler to detect errors or suppress warnings.5) Software tools can process annotation information to generate code, XML files, and so forth.6) Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.
Cons: Annotations unnecessarily couple the metadata to the code. Thus, changes to metadata require changing the source code. (i.e. when we want to change some configuration , we need to change the source code, where as in the xml -configuration, just need to change the xml file & redeploy it ).
Two things in Annotations:
1) Annotation : An annotation is the meta-tag that you will use in your code to give it some life.
2) Annotation Type: It is used for defining an annotation. used when we need to create our own custom annotation.
Define an Annotation type: In order to define our own custom tag, the following syntax need to be followed. "@" symbol followed by "interface" followed by "Name of the annotations".
Example:
public @interface MyAnnotation {String doSomething();}
Annotate Your Code (Annotation)
We are just supposed to use the name of our annotations & assign some values to the annotation variables.
Example:
MyAnnotation (doSomething="What to do")public void mymethod() {....}
Annotation Types:
There are three annotation types:
Marker: Marker type annotations have no elements, except the annotation name itself. Example: public @interface MyAnnotation {}Usage: @MyAnnotationpublic void mymethod() {....}
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: public @interface MyAnnotation {String doSomething();}Usage: @MyAnnotation ("What to do")public void mymethod() {....}
Full-value or multi-value: Full-value type annotations have multiple data members. Therefore, you must use a full data=value parameter syntax for each member.Example: public @interface MyAnnotation {String doSomething();int count; String date();}Usage: @MyAnnotation (doSomething="What to do", count=1,date="09-09-2005")public void mymethod() {....}
Rules to define 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:i) primitivesii) Stringiii) Classiv) enumv) array of the above types
Annotation :
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. There are only three types of simple annotations provided by JDK5.
They are:
Override : ( @Override ) throws an error when it some method is not overrided preoperly.
Deprecated: ( @Deprecated ) throws warnings when a method is used which is deprecated.
Suppress warnings: This annotation indicates that compiler warnings should be shielded in the annotated element and all of its sub-elements. @SuppressWarnings({"deprecation
Meta annotations : These are called the annotations-of-annotations
These are:i) Targetii) Retentioniii) Documentediv) Inherited.
The Target annotation : The target annotation indicates the targeted elements of a class in which the annotation type will be 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
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 compilerRetentionPolicy.CLASS—Annotations with this type will be by retained by the compiler at compile time, but will be ignored by the VMRetentionPolicy.RUNTIME—Annotations with this type will be retained by the VM so they can be read only at run-time
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 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
The Inherited annotation: Automatically all the methods are inherited in the child class.
Ex :@Inheritedpublic @interface myParentObject {boolean isInherited() default true;String doSomething() default "Do what?";}
Next, annotate a class with your annotation:
@myParentObjectpublic Class myChildObject {}
If there were no annotations , we should have written the class in the following way. i.e. we need to implement all the methods
public class myChildObject implements myParentObjectpublic boolean isInherited() {return false;}public String doSomething() {return "";}public boolean equals(Object obj) {return false;}public int hashCode() {return 0;}public String toString() {return "";}public Class annotationType() {return null;}}
No comments:
Post a Comment