Java Reference
In-Depth Information
•
PackageName...
: Assertions are enabled in the specified package and its
subpackages by specifying the package name followed by
...
.
•
...
: Assertions are enabled in the unnamed package, which happens to be
whatever directory is current.
•
ClassName
:Assertionsareenabledinthenamedclassbyspecifyingtheclass
name.
For example, you can enable all assertions except system assertions when running
the
MergeArrays
application via
java -ea MergeArrays
. Also, you could
enable any assertions in this chapter's
logging
package by specifying
java -
ea:logging TestLogger
.
Assertions can be disabled, and also at various granularities, by specifying either of
the
-disableassertions
or
-da
command-line options. These options take the
same arguments as
-enableassertions
and
-ea
.
Forexample,
java -ea -da:
loneclass mainclass
enables all assertions
exceptforthosein
loneclass
.(
loneclass
and
mainclass
areplaceholdersfor
the actual classes that you specify.)
The previous options apply to all classloaders (discussed in Appendix C). Except
whentakingnoarguments,theyalsoapplytosystemclasses.Thisexceptionsimplifies
theenablingofassertionstatementsinallclassesexceptforsystemclasses,whichisof-
ten desirable.
To enable system assertions, specify either
-enablesystemassertions
or
-
esa
; for example,
java -esa -ea:logging TestLogger
. Specify either
-
disablesystemassertions
or
-dsa
to disable system assertions.
Annotations
While developing a Java application, you might want to annotate various application
elements,orassociatemetadata(datathatdescribesotherdata)withthem.Forexample,
youmightwanttoidentifymethodsthatarenotfullyimplementedsothatyouwillnot
forgettoimplementthem.Java'sannotationslanguagefeatureletsyouaccomplishthis
task.
This section introduces you to annotations. After defining this term and presenting
threekindsofcompiler-supportedannotationsasexamples,thesectionshowsyouhow
todeclareyourownannotationtypesandusethesetypestoannotatesourcecode.Fin-
ally, you discover how to process your own annotations to accomplish useful tasks.