Java Reference
In-Depth Information
Processing Annotations
Itisnotenoughtodeclareanannotationtypeandusethattypetoannotatesourcecode.
Unless you do something specific with those annotations, they remain dormant. One
way to accomplish something specific is to write an application that processes the an-
notations. Listing 3-49 ' s StubFinder application does just that.
Listing 3-49. The StubFinder application
import java.lang.reflect.Method;
class StubFinder
{
public static void main(String[] args) throws Exception
{
if (args.length != 1)
{
System.err.println("usage:
java
StubFinder
classfile");
return;
}
Method[]
methods
=
Class.forName(args[0]).getMethods();
for (int i = 0; i < methods.length; i++)
if (methods[i].isAnnotationPresent(Stub.class))
{
Stub
stub
=
meth-
ods[i].getAnnotation(Stub.class);
String[] components = stub.value().split(",");
System.out.println("Stub
ID
=
"+compon-
ents[0]);
System.out.println("Stub
Date
=
"+compon-
ents[1]);
System.out.println("Stub Developer = "+compon-
ents[2]);
System.out.println();
}
Search WWH ::




Custom Search