AnnotationProcessor to generate jaxb.index for Java 6-. Replacing jelly-apt
authorDennis Schafroth <dennis@indexdata.com>
Fri, 14 Feb 2014 14:22:55 +0000 (15:22 +0100)
committerDennis Schafroth <dennis@indexdata.com>
Fri, 14 Feb 2014 14:22:55 +0000 (15:22 +0100)
.gitignore [new file with mode: 0644]
pom.xml [new file with mode: 0644]
src/main/java/com/indexdata/annotations/processor/JAXBIndexGenerator.java [new file with mode: 0644]
src/main/resources/META-INF/services/javax.annotation.processing.Processor [new file with mode: 0644]
src/test/java/com/indexdata/TestAnnotation.java [new file with mode: 0644]
src/test/java/com/indexdata/TestAnnotation2.java [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..d6ea9fc
--- /dev/null
@@ -0,0 +1,22 @@
+*.swp
+*~
+target/
+.classpath
+.project
+.settings/
+.metadata/
+bin/
+build/
+dist/
+nbproject/
+vpproject/
+*.jdbc
+*.versionsBackup
+mem.*
+.DS_Store
+deb-src
+solr.log
+install-stamp
+/data/
+logs/
+solr/collection1/data/
diff --git a/pom.xml b/pom.xml
new file mode 100644 (file)
index 0000000..dee6935
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,22 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.indexdata.annotations.processor</groupId>
+  <artifactId>jaxb-index-generator</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <name>JAXB Index Generator</name>
+  <build>
+    <sourceDirectory>src/main/java</sourceDirectory>
+    <plugins>
+      <plugin>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.1</version>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+          <compilerArgument>-proc:none</compilerArgument>
+          <encoding>UTF-8</encoding>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
\ No newline at end of file
diff --git a/src/main/java/com/indexdata/annotations/processor/JAXBIndexGenerator.java b/src/main/java/com/indexdata/annotations/processor/JAXBIndexGenerator.java
new file mode 100644 (file)
index 0000000..ef2dc7b
--- /dev/null
@@ -0,0 +1,72 @@
+package com.indexdata.annotations.processor;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Messager;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedOptions;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.TypeElement;
+import javax.tools.Diagnostic.Kind;
+import javax.tools.FileObject;
+import javax.tools.StandardLocation;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@SupportedAnnotationTypes("javax.xml.bind.annotation.XmlRootElement")
+@SupportedSourceVersion(SourceVersion.RELEASE_6)
+@SupportedOptions({ "test", "test2" })
+public class JAXBIndexGenerator extends AbstractProcessor {
+
+  @Override
+  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+    Set<String> options = this.getSupportedOptions();
+    Messager messager = processingEnv.getMessager();
+    for (String option : options) {
+       messager.printMessage(Kind.NOTE, "option: " + option);
+    }
+    Map<String, List<String>> packageMap = new HashMap<String, List<String>>();
+    for (Element elem : roundEnv.getElementsAnnotatedWith(XmlRootElement.class)) {
+      // XmlRootElement xmlRootElement =
+      // elem.getAnnotation(XmlRootElement.class);
+      String pkg = elem.getEnclosingElement().toString();
+      String className = elem.getSimpleName().toString();
+      String message = "XmlRootElement annotation found in " + pkg + ": " + className;
+      messager.printMessage(Kind.NOTE, message);
+      List<String> classList = packageMap.get(pkg);
+      if (classList == null) {
+       classList = new LinkedList<String>();
+       packageMap.put(pkg, classList);
+      }
+      classList.add(className);
+    }
+    FileObject fo = null;
+    for (String pkg : packageMap.keySet()) {
+      try {
+       Writer w = null;
+       fo = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, pkg,
+           "jaxb.index");
+       w = fo.openWriter();
+       messager.printMessage(Kind.NOTE, "Writing " + fo.getName() + " for package " + pkg);
+       for (String className : packageMap.get(pkg)) {
+         messager.printMessage(Kind.NOTE, "Class " + className);
+         w.write(className + "\n");
+       }
+       w.close();
+      } catch (IOException e) {
+       e.printStackTrace();
+      }
+    }
+    return true; // no further processing of this annotation type
+  }
+
+}
diff --git a/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/src/main/resources/META-INF/services/javax.annotation.processing.Processor
new file mode 100644 (file)
index 0000000..de14609
--- /dev/null
@@ -0,0 +1 @@
+com.indexdata.annotations.processor.JAXBIndexGenerator
diff --git a/src/test/java/com/indexdata/TestAnnotation.java b/src/test/java/com/indexdata/TestAnnotation.java
new file mode 100644 (file)
index 0000000..51fdd13
--- /dev/null
@@ -0,0 +1,18 @@
+package com.indexdata;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+
+@XmlRootElement
+public class TestAnnotation {
+  public TestAnnotation() {
+    
+  }
+  
+  @XmlElement
+  public String getName() {
+    return this.getClass().getSimpleName();
+  }
+
+}
diff --git a/src/test/java/com/indexdata/TestAnnotation2.java b/src/test/java/com/indexdata/TestAnnotation2.java
new file mode 100644 (file)
index 0000000..e9a0f75
--- /dev/null
@@ -0,0 +1,14 @@
+package com.indexdata;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+
+@XmlRootElement
+public class TestAnnotation2 {
+  @XmlElement
+  public String getName() {
+    return this.getClass().getSimpleName();
+  }
+
+}