Wednesday, 25 June 2014

Declaring Annotations

Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword.

Syntax
modifier @interface AnnotationName AnnotationBody
1. public, protected, private, abstract, static, strictfp can be used as modifiers.

2. To distinguish an annotation type declaration from a normal interface declaration, the keyword interface is preceded by an at-sign (@).

3. AnnotationName specifies the name of the annotation.

4. AnnotationBody contains method declarations. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. Methods can have default values

Example : Lets generate java doc for a java file using Annotations and using javadoc.

Using Documentation Comments
/**
 *  Author : Krishna
 *  Date : June 17th 2014
 *  Current Version : 1
 *  Last Modified : June 17th 2014
 *  Reviewers : Raghav
 */
public class SampleFile{

}

Run the below command
  javadoc SampleFile.java.

You can get the documentation like below.










   
                                                                          
                             

Using @Documented Annotation
import java.lang.annotation.*;

@Documented
public @interface AuthorDetails{
 String author();
 String date();
 int currentVersion() default 1;
 String lastModified() default "N/A";
 String[] Reviewers();
}

@AuthorDetails(
 author = "Krishna",
 date = "June 17th 2014",
 currentVersion = 1,
 lastModified = "June 17th 2014",
 Reviewers = {"Raghav", "Krishna"}
)
public class SampleFile{

}

Run the below command
  javadoc SampleFile.java.

You can get the documentation like below.


Annotation @Documented is used to make the information in @AuthorDetails appear in Javadoc-generated documentation.


java.lang.annotation.Annotation is the direct super interface for every Annotation.

Annotations don't support inheritance, I.e, one annotation type can't extends another.

1. It is a compile-time error if any method declared in an annotation type has a signature that is override-equivalent to that of any public or protected method declared in class Object or in the interface java.lang.annotation.Annotation.

public @interface Sample{
 int hashCode();
}

When you tries to compile the above program, compiler throws below error.

Sample.java:2: error: annotation type Annotation declares an element with the
me name as method hashCode()
        int hashCode();
            ^
1 error

2. It is a compile-time error if an annotation type declaration T contains an element of type T, either directly or indirectly.

public @interface Sample{
 Sample name();
}

When you tries to compile the program, compiler throws below error.

Sample.java:2: error: type of element {0} is cyclic
        Sample name();
               ^
1 error





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment