Java Reference
In-Depth Information
The Target annotation type,whichislocated inthe java.lang.annotation
package, identifies the kinds of application elements to which an annotation type ap-
plies. @Target indicatesthat @SuppressWarnings annotationscanbeusedtoan-
notate types, fields, methods, parameters, constructors, and local variables.
Each of TYPE , FIELD , METHOD , PARAMETER , CONSTRUCTOR , and
LOCAL_VARIABLE isamemberofthe ElementType enum,whichisalsolocatedin
the java.lang.annotation package.
The { and } characters surrounding the comma-separated list of values assigned to
Target 's value() elementsignifyanarray— value() 'sreturntypeis String[] .
Although these braces are necessary (unless the array consists of one item), value=
could be omitted when initializing @Target because Target declares only a
value() element.
The Retention annotation type, which is located in the
java.lang.annotation package,identifiestheretention(alsoknownaslifetime)
of an annotation type's annotations. @Retention indicates that @SuppressWarn-
ings annotationshavealifetimethatislimitedtosourcecode—theydonotexistafter
compilation.
SOURCE is one of the members of the RetentionPolicy enum (located in the
java.lang.annotation package).Theothermembersare CLASS and RUNTIME .
These three members specify the following retention policies:
CLASS :Thecompilerrecordsannotationsintheclassfile,buttheJVMdoesnot
retain them (to save memory space). This policy is the default.
RUNTIME :Thecompilerrecordsannotationsintheclassfile,andtheJVMre-
tainsthemsothattheycanbereadviatheReflectionAPI(discussedin Chapter
4 ) at runtime.
SOURCE : The compiler discards annotations after using them.
Therearetwoproblemswiththe Stub annotationtypesshownin Listings3-41 and
3-43 . First,thelackofan @Target meta-annotationmeansthatyoucanannotateany
application element @Stub . However, this annotation only makes sense when applied
to methods and constructors. Check out Listing 3-47 .
Listing 3-47. Annotating undesirable application elements
@Stub("1,12/21/2012,unassigned")
public class Deck
{
 
Search WWH ::




Custom Search