@Entity
@
) indicates to the compiler that what follows is an annotation. @Override
void mySuperMethod() { ... }
@Author(
name = "Benjamin Franklin",
date = "3/27/2003"
)
class MyClass() { ... }
Or
@SuppressWarnings(value = "unchecked")
void myMethod() { ... }
@SuppressWarnings("unchecked")
void myMethod() { ... }
@Author(name = "Jane Doe")
@EBook
class MyClass { ... }
@Author(name = "Jane Doe")
@Author(name = "John Smith")
class MyClass { ... }
new @Interned MyObject();
myString = (@NonNull String) str;
class UnmodifiableList<T> implements
@Readonly List<@Readonly T> { ... }
void monitorTemperature() throws
@Critical TemperatureException { ... }
public class Generation3List extends Generation2List {
// Author: John Doe
// Date: 3/17/2002
// Current revision: 6
// Last modified: 4/12/2004
// By: Jane Doe
// Reviewers: Alice, Bill, Cindy
// class code goes here
}
@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();
}
@ClassPreamble(
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
// Note array notation
reviewers = {
"Alice",
"Bob",
"Cindy”
}
)
public class Generation3List extends Generation2List {
// class code goes here
}
Note: To make the information in @ClassPreamble
appear in Javadoc-generated documentation, you must annotate the @ClassPreamble
definition with the @Documented
annotation:
// import this to use @Documented
import java.lang.annotation.*;
@Documented
@interface ClassPreamble {
// Annotation element definitions
}
A set of annotation types are predefined in the Java SE API. Some annotation types are used by the Java compiler, and some apply to other annotations.
java.lang
are @Deprecated
, @Override
, and @SuppressWarnings
// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated
static void deprecatedMethod() {}
}
// mark method as a superclass method
// that has been overridden
@Override
int overriddenMethod() { }
// use a deprecated method and tell
// compiler not to generate a warning
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
// deprecation warning
// - suppressed
objectOne.deprecatedMethod();
}
@SuppressWarnings({"unchecked", "deprecation"})
@SafeVarargs
annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on its varargs parameter. @FunctionalInterface
annotation, introduced in Java SE 8, indicates that the type declaration is intended to be a functional interface, as defined by the Java Language Specification.Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in java.lang.annotation.
@Schedule(dayOfMonth="last")
@Schedule(dayOfWeek="Fri", hour="23")
public void doPeriodicCleanup() { ... }
@Alert(role="Manager")
@Alert(role="Administrator")
public class UnauthorizedAccessException extends SecurityException {...}
import java.lang.annotation.Repeatable;
@Repeatable(Schedules.class)
public @interface Schedule {
String dayOfMonth() default "first";
String dayOfWeek() default "Mon";
int hour() default 12;}
public @interface Schedules {
Schedule[] value();
}
It is important to design your annotation type carefully to ensure that the programmer using the annotation finds it to be as flexible and powerful as possible.
@NonNull String str;