196 CHAPTER 10 PLUGGABLE ANNOTATION PROCESSING UPDATES
CHAPTER 10 PLUGGABLE ANNOTATION PROCESSING UPDATES 197 DumpProcessor(AnnotationProcessorEnvironment env) { this.env = env; } public void process() { for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations()) { typeDecl.accept(getDeclarationScanner(new DumpVisitor(), NO_OP)); } } private static class DumpVisitor extends SimpleDeclarationVisitor { public void visitMethodDeclaration(MethodDeclaration d) { System.out.println(”t” + d.getSimpleName()); } public void visitClassDeclaration(ClassDeclaration d) { System.out.println(d.getQualifiedName()); } public void visitInterfaceDeclaration(InterfaceDeclaration d) { System.out.println(d.getQualifiedName()); } } } } Defining the class is the easy part. Compiling it is just step one, and you can t just use javac alone (yet). As previously mentioned, you need to include tools.jar in your class- path to compile an annotation. javac -cp c:jdk1.6.0libtools.jar DumpFactory.java Note At least for now, you have to manually include tools.jar in your classpath to compile annotation processors. It is possible that by the time Java SE 6 ships, that could change. Running of the annotation is not done with the java command. This is where apt comes into play. But before you can use apt, you have to package up the factory and processor into a JAR file and install it, like other items that use the service API. Typically, this is done by creating a file in META-INF/services named com.sun.mirror.apt. AnnotationProcessorFactory to point to the processor just defined. However, to avoid this step, you can include extra command-line options to the apt command. And, for a little test, just run the processor on itself.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services