blob: 7b993c3f07cc44a4d36bd83ff2ef8d40b971d9b9 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta name="copyright" content="Copyright (c) 2000, 2009 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at https://www.eclipse.org/legal/epl-2.0/. Contributors: IBM Corporation - initial API and implementation" />
<meta name="DC.rights.owner" content="(C) Copyright 2000, 2009" />
<meta content="public" name="security" />
<meta content="index,follow" name="Robots" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta content="task" name="DC.Type" />
<meta name="DC.Title" content="Defining and using annotations" />
<meta name="abstract" content="You can use the @Interface annotation to define your own annotation definition." />
<meta name="description" content="You can use the @Interface annotation to define your own annotation definition." />
<meta content="Java EE 5, annotations, defining" name="DC.subject" />
<meta content="Java EE 5, annotations, defining" name="keywords" />
<meta scheme="URI" name="DC.Relation" content="../topics/cannotations.html" />
<meta scheme="URI" name="DC.Relation" content="../topics/ctypesofanno.html" />
<meta content="XHTML" name="DC.Format" />
<meta content="tjimpear" name="DC.Identifier" />
<meta content="en-us" name="DC.Language" />
<link href="../../org.eclipse.wst.doc.user/common.css" type="text/css" rel="stylesheet" />
<title>Defining and using annotations</title>
</head>
<body id="tjimpear"><a name="tjimpear"><!-- --></a>
<h1 class="id_title">Defining and using annotations</h1>
<div class="id_taskbody"><p class="id_shortdesc">You can use the @Interface annotation
to define your own annotation definition.</p>
<div class="id_context"> <p class="anchor_topictop" />
<p><strong>Defining
your own annotations</strong></p>
</div>
<div class="p"><span>Use the @Interface annotation to define your own annotation definition:</span>
<div class="p"><ul>
<li>Annotation definitions resemble interface definitions</li>
<li>Annotation method declarations have neither parameters nor <samp class="codeph">throws</samp> 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>
</div>
<div class="p"><pre>public @interface CreatedBy{
String name();
String date();
boolean contractor() default false;
} </pre>
<pre>@CreatedBy(name = "Mary Smith",date="02/02/2008");
public class MyClass{....} </pre>
</div>
</div>
<div class="id_postreq"><div class="p"><strong>Meta-annotations</strong>: 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>
<strong>Other built-in annotations</strong>: <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<div class="p"><pre>@Deprecated
public void oldMethod() {...}
@ SupressWarnings
public void yesIknowIuseDeprecatedMethods() {...}</pre>
</div>
</li>
</ul>
</li>
</ul>
</div>
<p class="anchor_topicbottom" />
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="../topics/ctypesofanno.html">Types of annotations</a></strong><br />
Java EE 6 and Java EE 5 define a number of types or groups of annotations, defined in a number of Java Specification Requests (JSRs).</li>
</ul>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="../topics/cannotations.html" title="The goal of Java EE 5 and Java EE 6 platform development is to minimize the number of artifacts that you have to create and maintain, thereby simplifying the development process. Java EE 5 and Java EE 6 support the injection of annotations into your source code, so that you can embed resources, dependencies, services, and life-cycle notifications in your source code, without having to maintain these artifacts elsewhere.">Java EE 5 and Java EE 6 support for annotations</a></div>
</div>
</div>
</body>
</html>