41
loading...
This website collects cookies to deliver better user experience
@Entity
Entity
.@Author(
name = "Benjamin Franklin",
date = "3/27/2003"
)
value
, then the name can be omitted, as in:@SuppressWarnings("unchecked")
void myMethod() { ... }
@Override
void mySuperMethod() { ... }
@Author(name = "Jane Doe")
@EBook
class MyClass { ... }
repeating annotation
.@Author(name = "Jane Doe")
@Author(name = "John Smith")
class MyClass { ... }
java.lang
or java.lang.annotation
packages, like @Override
and @SuppressWarnings
, which are predefined Java annotations. It is also possible to define your own annotation type, i.e. custom annotations.use
of types. This form of annotation is called a type annotation.new @Interned MyObject();
myString = (@NonNull String) str;
implements
clause
class UnmodifiableList<T> implements
@Readonly List<@Readonly T> { ... }
void monitorTemperature() throws
@Critical TemperatureException { ... }
@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();
}
interface
is preceded by the @ sign. Annotations are a form of interface. The body of the annotation definition contains annotation type element
declaration, which look a lot like methods. They can also be defined with default values filled in.java.lang
are : @Deprecated
, @Override
, and @SuppressWarnings
.@Deprecated
. This indicates that the marked element is deprecated and should no longer be used. The compuler generates a warning whenever a program uses a method, class, or field with this annotation. When an element is deprecated it should also be documented using the Javadoc @deprecated
tag.
// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
@Override
. This informs the compiler that the element is meant to override an element declared in superclass. While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override
fails to correclty override a method in one of its superclasses, the compiler generates an error.
@SupressWarnings
. This tells the compiler to suppress specific warnings that it would otherwise generate. For instance,
// use a deprecated method and tell
// compiler not to generate a warning
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
// deprecation warning
// - suppressed
objectOne.deprecatedMethod();
}
deprecation
and unchecked
. Unchecked warnings occur when interfacing with legacy code written before the advent of generics.@SafeVarargs
. When applied to a method or constructor, this asserts that the code does not perform potentially unsafe operations on its varargs
parameter. When this annotation is used, unchecked warnings related to varargs
are suppressed.@FunctionalInterface
. Introduced in Java 8, this indicates that the type declaration is intended to be a functional interface.java.lang.annotation
.@Retention
. This specifies how the marked annotation is stored.
RetentionPolicy.SOURCE
. The marked annotation is retained only in the source level and is ignored by the compiler.RetentionPolicy.CLASS
. The marked annotation is retained by the compiler at compile time, but is ignored by JVM.RetentionPolicy.RUNTIME
. The marked annotation is retained by the JVM so it can be used by the runtime environment.@Documented
. This indicates that whenever the specified annotation is used, those elements should be documented using the Javadoc tool (By default, annotations are not inclueded in the Javadoc). @Target
. This annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to. A target annotation specifies one of the ff. element.
ElementType.ANNOTATION_TYPE
. This can be applied to an annotation type.ElementType.CONSTRUCTOR
. This can be applied to a constructor.ElementType.FIELD
. This can be applied to a field or property.ElementType.LOCAL_VARIABLE
. This can be applied to a local variable.ElementType.METHOD
. This can be applied to a method level annotation.ElementType.PACKAGE
. This can be applied to a package declaration.ElementType.PARAMETER
. THis can be applied to the parameters of a method.ElementType.TYPE
. This can be applied to any element of a class.@Inherited
. This indicates that the annotation type can be inherited from the superclass (not true by default). When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type. This can only be applied to class declarations.
@Repeatable
. Introduced in Java 8, this indicates that the marked annotation can be applied more than once to the same declaration or type use.
(new)
, casts, implements
clauses and throws
clauses. This form of annotation is called type annotation
. null
.@NonNull String str;
NonNull
module, the compiler prints a warning if it detects a potential problem, allowing developers to modify the code to avoid the error. After the code is corrected, this particular error will not occur when the program runs.@Repeatable
metannotation. For instance
@Repeatable(Schedules.class)
public @interface Schedule {
String dayOfMonth() default "first";
// ...
}
public @interface Schedules {
Schedule[] value();
}
AnnotatedElement.getAnnotation(Class<T>)
, are unchanged in that they only return a single annotation if one annotation of the requested type is present. If more than one annotation of the requested type is present, you can obtain them by first getting their container annotation. In this way, legacy code continues to work. Other methods were introduced in Java SE 8 that scan through the container annotation to return multiple annotations at once, such as AnnotatedElement.getAnnotationsByType(Class<T>)
. @Repeatable
, more than once. It is also possible to restrict where an annotation type can be used by using the @Target
meta-annotation. It is important to design annotation types carefully to ensure that the person using the annotation finds it to be as flexible and powerful as possible.