Annotations are tags inserted into source code that can be processed by tools. Annotations can be applied to classes, fields, methods, and other program elements. Common Java annotations include @Override, @Deprecated, and @SuppressWarnings. Annotations are defined by annotation interfaces and can include named elements. The apt tool supports annotation processing by running annotation processors that can generate new source code files.
2. Annotations - Introduction
• Annotations are tags that you insert into your
source code so that they can be processed by
tools.
• Annotations can be processed at compile time
or at run time or at deployment time.
• Annotations can be applied to
classes, fields, methods or other program
elements.
3. Annotations
• An annotation is used like a modifier, and it is
placed before the annotated item, without a
semicolon. (A modifier is a keyword such as
public or static.)
• The name of each annotation is preceded by an
@ symbol, similar to Javadoc comments. Javadoc
comments occur inside /** . . . */
delimiters, whereas annotations are part of the
code.
• By itself, an annotation does not do anything. It
needs a tool to be useful.
4. Java Standard Annotations
• Part of java.lang package
• @Override – method will override method of the same signature from the
base class.
• @Deprecated – attribute or method that should no longer be used. A
replacement feature has been provided. Deprecated may be removed in
future java versions.
• @SuppressWarnings
–
–
–
–
–
all — all warnings
deprecation — warnings relative to deprecation
fallthrough — warnings relative to missing breaks in switch statements
hiding — warnings relative to locals that hide variable
serial — warnings relative to missing serialVersionUID field for a serializable
class
– unchecked — warnings relative to unchecked operations
– unused — warnings relative to unused code
5. How to use Annotations?
•
•
•
Annotations are written before declarations of classes, methods, fields or other
program elements.
Can include elements with named or unnamed values. Elements must be compile
time constant values.
The annotation appears first, usually on its own line, and may include elements
with named or unnamed values:
@AuthorDetails (
owner = “JohnSmith",
date = “9/11/2011"
)
class Mammal { }
6. Using Annotations
• Annotation elements must be compile-time constant values.
• Annotation elements may be primitive
types, Strings, Classes, enums, annotations, or an array of a
permitted type.
• An array of values is supplied within braces, for example:
@SuppressWarnings({"unchecked","deprecation"})
• If there is just element called value, you don’t need to specify it.
@Override
public int calculateSalary(String empId) { … }
7. Define Annotations
•
•
Annotations can be defined to have elements. These elements can be processed
by the tools that read the annotations.
Each annotation must be defined by an annotation interface. The methods of the
interface correspond to the elements of the annotation.
For example, a TestCase annotation could be defined by the following interface:
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestCase
{
String id() default "[none]";
}
8. Define Annotations
• The @interface declaration creates an actual Java
interface.
• Tools that process annotations receive objects that
implement the annotation interface. A tool would call
the id method to retrieve the id element of a particular
TestCase annotation.
• The Target and Retention annotations are metaannotations. They annotate the TestCase
annotation, marking it as an annotation that can be
applied to methods only and that is retained when the
class file is loaded into the virtual machine.
9. Apt
• apt is a command-line utility for annotation
processing.
• It includes a set of reflective APIs and
supporting infrastructure to process program
annotations.
• First runs annotation processors that can
produce new source code and other files.
• Next, apt compiles both original and
generated source files.