1 package com.indexdata.annotations.processor;
3 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.LinkedList;
11 import javax.annotation.processing.AbstractProcessor;
12 import javax.annotation.processing.Messager;
13 import javax.annotation.processing.RoundEnvironment;
14 import javax.annotation.processing.SupportedAnnotationTypes;
15 import javax.annotation.processing.SupportedOptions;
16 import javax.annotation.processing.SupportedSourceVersion;
17 import javax.lang.model.SourceVersion;
18 import javax.lang.model.element.Element;
19 import javax.lang.model.element.TypeElement;
20 import javax.tools.Diagnostic.Kind;
21 import javax.tools.FileObject;
22 import javax.tools.StandardLocation;
23 import javax.xml.bind.annotation.XmlRootElement;
25 @SupportedAnnotationTypes("javax.xml.bind.annotation.XmlRootElement")
26 @SupportedSourceVersion(SourceVersion.RELEASE_6)
27 @SupportedOptions({ "test", "test2" })
28 public class JAXBIndexGenerator extends AbstractProcessor {
31 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
32 Set<String> options = this.getSupportedOptions();
33 Messager messager = processingEnv.getMessager();
34 for (String option : options) {
35 messager.printMessage(Kind.NOTE, "option: " + option);
37 Map<String, List<String>> packageMap = new HashMap<String, List<String>>();
38 for (Element elem : roundEnv.getElementsAnnotatedWith(XmlRootElement.class)) {
39 // XmlRootElement xmlRootElement =
40 // elem.getAnnotation(XmlRootElement.class);
41 String pkg = elem.getEnclosingElement().toString();
42 String className = elem.getSimpleName().toString();
43 String message = "XmlRootElement annotation found in " + pkg + ": " + className;
44 messager.printMessage(Kind.NOTE, message);
45 List<String> classList = packageMap.get(pkg);
46 if (classList == null) {
47 classList = new LinkedList<String>();
48 packageMap.put(pkg, classList);
50 classList.add(className);
53 for (String pkg : packageMap.keySet()) {
56 fo = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, pkg,
59 messager.printMessage(Kind.NOTE, "Writing " + fo.getName() + " for package " + pkg);
60 for (String className : packageMap.get(pkg)) {
61 messager.printMessage(Kind.NOTE, "Class " + className);
62 w.write(className + "\n");
65 } catch (IOException e) {
69 return true; // no further processing of this annotation type