Tutorials : Program Annotation Facility :

Annotations:

An annotation is a modifier that can be used on any declaration like a class, interface, method, variable, parameter etc. An annotation contains zero or more member value pairs depending on which annotation type is used. Annotation should define member- value pairs for all members defined in the annotation type. The only exception to this rule is members who have default values already assigned in the annotation type definition. Annotations can be used on Enum constants.

Annotations can also be used on packages. When they are declared in packages they need to be in a file called package-info.java. Most people who have worked with generating javadocs from classes would be familiar with package.html file. The package.html file contains all the package level information that is required for generating documentation. The package- info.java file replaces this package.html file. Document generators should ignore package.html file if they find the package-info.java file. The package-info.java file will contain package level annotations and documentations, which can be used by the generator to generate documentation.

There are three kinds of annotations: normal annotation, marker annotation, and single member annotation. A normal annotation is used to annotate a program element using a regular annotation, an example of an annotation using the annotation type (Normal Annotation) defined above would be:

	@NormalAnnotation(	value1 = “”, value2=0)
      public void someMethod(){
      …
      }

A marker annotation uses a marker annotation type. A single member annotation is a short hand for a regular annotation and uses a single member annotation type.

Meta-Annotation Types

The program annotation specification provides meta-annotation types, which are used to annotate annotation types. The java.lang.Annotation class provides the facility for doing this.

The first meta-annotation type is the target, which is used to specify the program element to which an annotation type can be applied. The possible values are defined in the ElementType enumeration: TYPE , FIELD, METHOD, PARAMETER, LOCAL_VARIABLE, PACKAGE etc. The TYPE ElementType defines a class, interface or an enum declaration; the FIELD can include any field declarations and enum constants. When an annotation type is marked with the target Meta tag say TYPE, then the annotation can only be used in the scope of a class, interface or a enum declaration.

Usage: @Target(FIELD) 

The second type is the Documented meta-annotation tag. This specifies whether the method needs to be documented by javadoc. Specifying the @Documented tag within a method tells the javadoc compiler to generate Javadoc documentation for this method.

Usage : @Documented

The third type of meta-annotation tag is Retention. Retention decides how long an annotation has to be retained. It is used in conjunction with the RetentionPolicy enumerated type. The possible values for the enumeration are

  • "SOURCE" - Instructs the compiler to discard any annotations
  • "CLASS" - Tells the compile to add the annotations to the class file but allows the VM to discard the annotations.
  • "RUNTIME" – Tells the compiler to add the annotations to the class file and also make them available to the runtime VM through reflection.

    The default value is Source.

    Usage : @Retention(SOURCE)
    

    The fourth type of meta-annotation is the inherited tag. This indicates if annotations can be automatically inherited by the subclasses and subclass methods.

    A example with all the meta tags is given below.

    @Retention(SOURCE) 
    @Target(METHOD) 
    @Documented 
    public @metaExampleMethod{
     …
    }
    

    This annotation type does the following

  • Has a retention value "source" which instructs the compiler and runtime to ignore any annotations. (It will be used by General tools which will be discussed shortly)
  • Instructs the javadoc tool to generate documentation for this method it is used on.
  • Has a target of type method, which ensures that the annotation type will be used only on methods.

    How to Add Java Applets to Your Site

    New on the Java Boutique:

    New Review:

    Time Management Made Easy with the Quartz Enterprise Job Scheduler
    Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

    New Applet:

    Reverse Complement
    Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

    Elsewhere on internet.com:

    WebDeveloper Java
    Lots of Java information on webdeveloper.com

    WDVL Java
    Thorough Java resource at the Web Developer's Virtual Library.

    ScriptSearch Java
    Hundreds of free Java code files to download.

    jGuru: Your View of the Java Universe
    Customizable portal with online training, FAQs, regular news updates, and tutorials.