<?xml version="1.0" encoding="utf-8"?> | |
<!--Arbortext, Inc., 1988-2006, v.4002--> | |
<!DOCTYPE task PUBLIC "-//OASIS//DTD DITA Task//EN" | |
"task.dtd"> | |
<task id="tjimpear" xml:lang="en-us"> | |
<title outputclass="id_title">Defining and using annotations</title> | |
<shortdesc outputclass="id_shortdesc">You can use the @Interface annotation | |
to define your own annotation definition.</shortdesc> | |
<prolog><metadata> | |
<keywords><indexterm>Java EE 5<indexterm>annotations</indexterm><indexterm>defining</indexterm></indexterm> | |
</keywords> | |
</metadata></prolog> | |
<taskbody outputclass="id_taskbody"> | |
<context outputclass="id_context"> <p outputclass="anchor_topictop"></p><p><b>Defining | |
your own annotations</b></p></context> | |
<steps> | |
<step><cmd>Use the @Interface annotation to define your own annotation definition:</cmd> | |
<info><p><ul> | |
<li>Annotation definitions resemble interface definitions</li> | |
<li>Annotation method declarations have neither parameters nor <codeph>throws</codeph> clauses, | |
and return one of the following elements:<ul> | |
<li>primitives</li> | |
<li>String</li> | |
<li>Class</li> | |
<li>enum</li> | |
<li>array of the above types</li> | |
</ul></li> | |
<li>Methods may have default values</li> | |
</ul></p><p><codeblock>public @interface CreatedBy{ | |
String name(); | |
String date(); | |
boolean contractor() default false; | |
} </codeblock><codeblock>@CreatedBy(name = "Mary Smith",date="02/02/2008"); | |
public class MyClass{....} </codeblock></p></info></step> | |
</steps> | |
<postreq outputclass="id_postreq"><p><b>Meta-annotations</b>: Meta-annotations | |
(annotations of annotations) provide additional information on how an annotation | |
should be used:<ul> | |
<li>@Target<ul> | |
<li>Restricts the use of an annotation</li> | |
<li>Single argument must be from Enum ElementType <ul> | |
<li>{TYPE, FIELD,METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE}</li> | |
</ul></li> | |
</ul></li> | |
<li>@Retention<ul> | |
<li>Indicates where the annotation information will be retained</li> | |
<li>Single argument must be from Enum RetentionPolicy<ul> | |
<li>{SOURCE, CLASS, RUNTIME}</li> | |
</ul></li> | |
</ul></li> | |
<li>@Documented<ul> | |
<li>Marker for annotations that should be included in Javadoc</li> | |
</ul></li> | |
<li>@Inherited<ul> | |
<li>marker for Type annotations that are to be inherited by subtypes</li> | |
</ul></li> | |
</ul><b>Other built-in annotations</b>: <ul> | |
<li>@Overrides<ul> | |
<li>Applied to a method</li> | |
<li>Indicates that the compiler should generate an error if the method does | |
not actually override a superclass method.</li> | |
</ul></li> | |
<li>@Deprecated<ul> | |
<li>Applied to a method</li> | |
<li>Indicates that the compiler should generate a warning when the method | |
is used externally</li> | |
</ul></li> | |
<li>@SuppressWarnings<ul> | |
<li>Applies to a type or a method</li> | |
<li>Indicates that the compiler should supress warnings for that element and | |
all subelements<p><codeblock>@Deprecated | |
public void oldMethod() {...} | |
@ SupressWarnings | |
public void yesIknowIuseDeprecatedMethods() {...}</codeblock></p></li> | |
</ul></li> | |
</ul></p><p outputclass="anchor_topicbottom"></p></postreq> | |
</taskbody> | |
</task> |