Java Reference
In-Depth Information
Instancesofannotationtypesthatsupplynodataapartfromaname-theirbodiesare
empty-areknownas marker annotations becausetheymarkapplicationelementsfor
some purpose. As Listing 3-42 reveals, @Stub is used to mark empty methods (stubs).
Listing 3-42. Annotating a stubbed-out method
public class Deck // Describes a deck of cards.
{
@Stub
public void shuffle()
{
// This method is empty and will presumably be filled
in with appropriate
// code at some later date.
}
}
Listing3-42 ' s Deck classdeclaresanempty shuffle() method.Thisfactisindic-
atedbyinstantiating Stub andprefixing shuffle() 'smethodheaderwiththeresult-
ing @Stub annotation.
Note Althoughmarkerinterfaces(introducedin Chapter2 )appeartohavebeenre-
placedbymarkerannotations,thisisnotthecase,becausemarkerinterfaceshavead-
vantagesovermarkerannotations.Oneadvantageisthatamarkerinterfacespecifiesa
typethatisimplementedbyamarkedclass,whichletsyoucatchproblemsatcompile
time. For example, if a class does not implement the Cloneable interface, its in-
stancescannotbeshallowlyclonedvia Object 's clone() method.If Cloneable
hadbeenimplementedasamarkerannotation,thisproblemwouldnotbedetecteduntil
runtime.
Althoughmarkerannotationsareuseful (@Override and @Deprecated aregood
examples),youwilltypicallywanttoenhanceanannotationtypesothatyoucanstore
metadata via its instances. You accomplish this task by adding elements to the type.
An element is a method header that appears in the annotation type's body. It cannot
have parameters or a throws clause, and its return type must be a primitive type (such
as int ), String , Class , an enum, an annotation type, or an array of the preceding
types. However, it can have a default value.
Listing 3-43 adds three elements to Stub .
 
Search WWH ::




Custom Search