Java Reference
In-Depth Information
StubFinder loads a classfile whose name is specified as a command-line argu-
ment, and outputs the metadata associated with each @Stub annotation that precedes
each public method header. These annotations are instances of Listing 3-48 ' s Stub
annotation type.
StubFinder nextusesaspecialclassnamed Class (inthe java.lang package)
and its forName() class method to load a classfile. Class also provides a
getMethods() methodthatreturnsanarrayof Method objectsdescribingtheloaded
class's public methods.
For each loop iteration, a Method object's isAnnotationPresent() method
is called to determine if the method is annotated with the annotation described by the
Stub class (referred to as Stub.class ).
If isAnnotationPresent() returns true, Method 's getAnnotation()
method is called to return the annotation Stub instance. This instance's value()
method is called to retrieve the string stored in the annotation.
Next, String 's split() methodiscalledtosplitthestring'scomma-separatedlist
ofID,date,anddevelopervaluesintoanarrayof String objects.Eachobjectisthen
output along with descriptive text.
Class 's forName() method is capable of throwing various exceptions that must
append a throws Exception clause to the main() method's header.
Caution Therearetwoproblemswith throws Exception .First,itisbetterto
handle the exception and present a suitable error message than to “pass the buck” by
throwing it out of main() . Second, Exception is generic—it hides the names of
thekindsofexceptionsthatarethrown.However,itisconvenienttospecify throws
Exception in a throwaway utility.
Do not be concerned if you do not understand Class , forName() , getMeth-
ods() , Method , isAnnotationPresent() , .class , getAnnotation() ,
and split() . You will learn about these items in Chapter 4 .
After compiling StubFinder ( javac ), Stub ( javac ), and Listing 3-45 's Deck class ( javac ), run
StubFinder with Deck asitssinglecommand-lineargument( java StubFinder
Deck ). You will observe the following output:
Search WWH ::

Custom Search