Intro to annotation p2

Annotations: JDK 5 (Tiger) contains two types of annotations:
  • Simple annotations: These types of annotations are used to annotate the code only. We can not use these types of annotations for creating the custom annotation type.
  • Meta annotations: Also known as annotations of annotations are used to annotate the annotation-type declaration.
Simple annotations: JDK 5 includes three types of simple annotations.
  • Override
  • Depricated
  • Suppresswarnings


Meta-Annotations (Annotation Types): There are four types ofm Meta annotations (or annotations of annotations) defined by the JDK 5. These are as follows:
  • Target
  • Retention
  • Documented
  • Inherited
Target annotation: Target annotation specifies the elements of a class to which annotation is to be applied. Here is the listing of the elements of the enumerated types as its value:
  • @Target(ElementType.TYPE)—applicable to any element of a class.
  • @Target(ElementType.FIELD)—applicable to field or property.
  • @Target(ElementType.PARAMETER)—applicable to the parameters of a method.
  • @Target(ElementType.LOCAL_VARIABLE)—applicable to local variables.
  • @Target(ElementType.METHOD)—applicable to method level annotation.
  • @Target(ElementType.CONSTRUCTOR)—applicable to constructors.
  • @Target(ElementType.ANNOTATION_TYPE)—specifies that the declared type itself is an annotation type.

Retention annotation: These types of annotation specify where and how long annotation with this types are to be retained. There are three type of Retention annotations are of three types.
  • RetentionPolicy.SOURCE: This type of annotation will be retained only at source level and the compiler will ignore them.
  • RetentionPolicy.CLASS: This type of annotation will be retained at the compile time the virtual machine (VM) will ignore them.
  • RetentionPolicy.RUNTIME: Virtual machine will retained the annotation of this type and they can be read only at run-time.


Documented annotation: This type of annotation should be documented by the javadoc tool. javadoc does not include the annotation by default. Include the annotation type information by using @Documented in the generated document. In this type of annotation all the processing is done by javadoc-like tool.


Inherited Annotation: This annotation is little bit complex. It inherits the annotated class automatically. If you specify @Inherited tag before defining a class then apply the annotation at your class and finally extend the class then the child class inherits the properties of the parent class automatically. Lets demonstrate the benefits of using the @Inherited tag by an example: 
Example:
Lets first, define the annotation:

@Inherited
public @interface ParentObjectDemo {
boolean isInherited() default true;
String showSomething() default "Show anything?";
}



Now, annotate the class with our annotation:

@ParentObjectDemo
public Class ChildObjectDemo {
}

The above example shows that you do not need to define the interface methods inside the implemented class. The @Inherited tag automatically inherits the methods for you. Suppose you define the implementing class in the old-fashioned-java-style then let us see the effect of doing this:
public class ChildObjectDemo implements ParentObjectDemo {
public boolean isInherited() {
return false;
}
public String showSomething() {
return "";
}
public boolean equals(Object obj) {
return false;
}
public int hashCode() {
return 0;
}
public String toString() {
return "";
}
public Class annotationType() {
return null;
}

}
Have you seen the difference? You have to implement all the methods of the parent interface. You will have to implement the equals(), toString(), and the hashCode() methods of the Object class and also the annotation type method of the java.lang.annotation.Annotation class. You will also have to include all these methods in your class regardless of whether you are implementing all these methods or not.

评论

此博客中的热门博文

Fader av pingyins

Nu förbjuder Kina handel med elfenben

Kineserna vill köpa Volvos kompetens