CHAPTER 10 PLUGGABLE ANNOTATION PROCESSING UPDATES 195 To get started, you need to create an implementation of the com.sun.mirror.apt. AnnotationProcessorFactory interface. There are three methods to the interface, as follows: AnnotationProcessor getProcessorFor(Set atds, AnnotationProcessorEnvironment env) Collection supportedAnnotationTypes() Collection supportedOptions() Note For Java SE 6.0, the latter two methods here, supportedAnnotationTypes() and supportedOptions(), have become annotations themselves. The first method is what is used to look up the annotation processor. All the method needs to do is return a new instance of your class, which implements AnnotationProcessor. The processor interface implementation is the worker bee. It has a single method to implement: process(). If you use the AnnotationProcessorEnvironment implementation passed into the constructor of your AnnotationProcessor, your process() method loops through all the declarations requested. The AnnotationProcessorEnvironment offers different ways to request declarations. The Collection getDeclarationsAnnotatedWith(AnnotationTypeDeclaration a) method allows you to ask for those declarations (methods, classes, and fields) defined with a particular annotation. The Collection getSpecifiedType. Declarations() method essentially allows you to get all of them, giving you access to everything passed from the command line. Lastly, Collection getTypeDeclarations() doesn t require you to specify everything. For the sample in Listing 10-6, use the getSpecifiedTypeDeclarations() variety. To process each declaration, you need a visitor. The com.sun.mirror.util package offers the DeclarationVisitor interface and SimpleDeclarationVisitor implementation to help. The DeclarationVisitor interface offers a series of visitXXXDeclaration() methods so that you can choose to work with only certain types of declarations, such as all the classes, all the interfaces, or all the methods. For instance, to print out the name of each class, you would override the visitClassDeclaration() method. public void visitClassDeclaration(ClassDeclaration d) { System.out.println(d.getQualifiedName()); }
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
No comments yet.
Sorry, the comment form is closed at this time.