Java Reference
In-Depth Information
public @interface Unfinished {
String message() default "Nothing has been
done";
}
Onceyoudeclareyourannotation, youmustnowdefineitscharacteristics.Thebasic
characteristicsofanannotationaredefinedthroughdedicatedannotationscontained
in the
java.lang.annotation
package. These annotations are as follows:
•
@Target
: This is used to define the element types that can be annotated
(such as class, method, and attribute), for example
@Tar-
get({ElementType.METHOD, ElementType.TYPE})
•
@Retention
: This is used to define the retention level (such as
RUNTIME
,
CLASS
, or
SOURCE
) of your annotation, for example
@Reten-
tion(RetentionPolicy.RUNTIME)
•
@Inherited
: This is used to say that the annotation will be automatically
applied to classes that inherit from the class that has the annotation
•
@Documented
: This is used to make your annotation appear in the
Javadoc
of the code that contains it
Itisimportanttonotethatthereareothercharacteristicssuchasthescope(setusing
the
@ScopeType
) in the case of custom CDI scope annotations.
After all changes, our annotation takes the form shown in the following code. Accord-
ing to the settings, this annotation can decorate methods, types of objects (such as
class
,
interface
, or
enum
) and attributes. It will be removed at the compile time
(because the retention level is
SOURCE
).
@Target({ElementType.METHOD, ElementType.TYPE,
ElementType.FIELD})
@Retention(RetentionPolicy.SOURCE)
public @interface Unfinished {
String message() default "Nothing has been
done";
}
The following code demonstrates the usage of the
Unfinished
annotation: