Saturday, 15 June 2013

Annotation

  • Annotation, a form of metadata, provide data about program that is not part of the code. Or an annotation is simply to associate information with the annotated program element.
  • They have no direct effect on the operation of the code they annotate.
  •  

Uses of Annotations

  • Information for the compiler : Annotation can be used by the compiler to detect errors and suppress warnings.
  • Compile-time and deployment time processing : Software tools can process annotation information to generate code, XML files and so forth.
  • Runtime Processing : some annotation are available to be examined at runtime.


  • Annotation will look like the following.

@Annoteexample
  • '@' indicates to the compiler what follows is an annotation.
  • Annotation can contains element-value pair.

@Author{
Name="B";
Date="5/20/2013"
}
Class my class{...}

@SuppresWarnings(value="unchecked" )
Void myMethod(){}

@SuppresWarnings("unchecked" )
Void myMethod(){}

  •  If the annotation has no element, then parentheses cab be omitted.
  • It is possible multiple annotation on the same declarations.

@Author(name ="B")
@Ebook
Class myclass {...}

  • The annotation can be one of the type defined in the java.lang or java.lang.annotation packages.
  • There are three kind of annotations, normal annotation, marker annotation and single-element annotation.


Where Annotation can be used

  • Annotation can be applied to declaration : declaration of classes, method, fields, enums, interface, annotation types, formal parameters, constructors, local variables and other program elements.
  • Annotation can be used in enum constants, such annotation placed immediately before the enum constant.
  • It is compile-time error if a declaration is annotated with more than one annotation for a given annotation type.

Declaring a Annotation Type

  • The Annotation Type definition looks similar to an interface definition where the keyword interface is preceded by the sign @.
  • Annotation Type is form of interface.
  • They can define default value.

Syntax

@interface ClassPreamble{
String Author();
String date();
Int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy default "N/A";
String[] reviwers();
}

Usage

@ClassPremble(
Author = "A";
Date = "5/21/2013",
currentRevision = 6;
lastModified ="5/15/2013",
lastModifiedBy ="G",
Reviwers = {"F","A","E"}
)
  • To make this information in @ClassPreamble appears in Javadoc-generated documentation, you must annotate @ClassPreamble with the @Documented annotation.

// import this to use @Documented
import java.lang.annotation.*;
@Documented
@interface ClassPreamble {
// Annotation element definitions
  
}


Predefined Annotation Types

  • The predefined annotation type defined in java.lang are @Deprecated, @Override, and @SuppressWarnings

@Deprecated

  • The above annotation indicates that the marked element is deprecated and no longer be used.
  • The generates a warning whenever the program uses a method, class, or field with deprecated annotation.
  • When an element is deprecated it should be documented using java-docs.

@Override

  • This annotation informs compiler that the element is meant to override an element declared in superclass.
  • While it is not require to use when overriding a method, it helps prevent errors.

@SuppressWarnings
 
  • It tells the compiler suppress specific warnings that it would otherwise generate.

e.g.
@SuppressWarning("deprecation")
Void decreatedMethod(){..}

  • In above example, we use deprecated method, but compile omits the warning.
  • To suppress multiple warning we can use like below.

@SupressWarnings( {"unchecked", "deprecated"})

@SafeVarargs

  • This annotation when applied to method or constructor, assert that the code doesn't perform any un-safe operation on varargs params.
  • When this annotation is used the unchecked warning relate to varargs are suppressed

@Functionalnterface (Java 8.0)

  • Indicates that the type of declaration is intended to functional interface.


Annotation that apply to other Annotations

  • Annotation that apply  to other annotations are meta-annotations.
  • There are several meta-annotation defined in java.lang.annoation.

@Retention
 
  • This annotation specifies how the marked annotation is stored.

RetentionPolicy.SOURCE : retained only at source level and ignored by compiler.
RetentionPolicy.CLASS : retained by compiler at compile time and ignore by JVM
RetentionPolicy.RUNTIME : retained by JVM.

@Documented

  • This annotation indicates that type are to be documented by javadoc or other tools.

@Target


  • It marks another annotation to restrict what kind Java elements the annotation that can be applied to.

No comments:

Post a Comment