From 0250bca18df891dfe9db7e1ece513946180fc076 Mon Sep 17 00:00:00 2001 From: Dennis Schafroth Date: Fri, 14 Feb 2014 15:22:55 +0100 Subject: [PATCH 1/1] AnnotationProcessor to generate jaxb.index for Java 6-. Replacing jelly-apt --- .gitignore | 22 ++++++ pom.xml | 22 ++++++ .../annotations/processor/JAXBIndexGenerator.java | 72 ++++++++++++++++++++ .../services/javax.annotation.processing.Processor | 1 + src/test/java/com/indexdata/TestAnnotation.java | 18 +++++ src/test/java/com/indexdata/TestAnnotation2.java | 14 ++++ 6 files changed, 149 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/indexdata/annotations/processor/JAXBIndexGenerator.java create mode 100644 src/main/resources/META-INF/services/javax.annotation.processing.Processor create mode 100644 src/test/java/com/indexdata/TestAnnotation.java create mode 100644 src/test/java/com/indexdata/TestAnnotation2.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6ea9fc --- /dev/null +++ b/.gitignore @@ -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 index 0000000..dee6935 --- /dev/null +++ b/pom.xml @@ -0,0 +1,22 @@ + + 4.0.0 + com.indexdata.annotations.processor + jaxb-index-generator + 0.0.1-SNAPSHOT + JAXB Index Generator + + src/main/java + + + maven-compiler-plugin + 3.1 + + 1.6 + 1.6 + -proc:none + UTF-8 + + + + + \ 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 index 0000000..ef2dc7b --- /dev/null +++ b/src/main/java/com/indexdata/annotations/processor/JAXBIndexGenerator.java @@ -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 annotations, RoundEnvironment roundEnv) { + Set options = this.getSupportedOptions(); + Messager messager = processingEnv.getMessager(); + for (String option : options) { + messager.printMessage(Kind.NOTE, "option: " + option); + } + Map> packageMap = new HashMap>(); + 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 classList = packageMap.get(pkg); + if (classList == null) { + classList = new LinkedList(); + 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 index 0000000..de14609 --- /dev/null +++ b/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -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 index 0000000..51fdd13 --- /dev/null +++ b/src/test/java/com/indexdata/TestAnnotation.java @@ -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 index 0000000..e9a0f75 --- /dev/null +++ b/src/test/java/com/indexdata/TestAnnotation2.java @@ -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(); + } + +} -- 1.7.10.4