the word annotate means to add noes to a book or giving explanations or comments.In relation to java
Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.
@Author(
name = "Sougata Das",
date = "2/25/2012"
)
class MyClass() { }
let a class looks likepublic class Generation3List
extends Generation2List {
// Author:Sougata Das
// Date: 2/18/2012
// Current revision: 6
// Last modified: 1/1/2012
// By: sougata
// Reviewers: Alice, Bill, Cindy
// class code goes here
}To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
// Note use of array
String[] reviewers();
}Once the annotation type has been defined, you can use annotations of that type, with the values filled in, like this:
@ClassPreamble (
author = "Sougata Das",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Sougata",
// Note array notation
reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {
// class code goes here
}
To make the information in @ClassPreamble appear in Javadoc-generated documentation, you must annotate the @ClassPreamble definition itself with the @Documented annotation:
// import this to use @Documented
import java.lang.annotation.*;
@Documented
@interface ClassPreamble {
// Annotation element definitions
}
Annotations Used by the Compiler
@Deprecated, @Override, and @SuppressWarnings.
@Deprecated—the
@Deprecated annotation indicates that the marked element is
deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the
@Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc
@deprecated tag, as shown in the following example. The use of the "@" symbol in both Javadoc comments and in annotations is not coincidental — they are related conceptually. Also, note that the Javadoc tag starts with a lowercase "d" and the annotation starts with an uppercase "D".
// Javadoc comment follows
/**
* @deprecated
* explanation of why it
* was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}@Override—the
@Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").
// mark method as a superclass method
// that has been overridden
@Override
int overriddenMethod() { }@SuppressWarnings—the
@SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the example below, a deprecated method is used and the compiler would normally generate a warning. In this case, however, the annotation causes the warning to be suppressed.
// use a deprecated method and tell
// compiler not to generate a warning
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
// deprecation warning
// - suppressed
objectOne.deprecatedMethod();
}
No comments:
Post a Comment