Commits

Micha Kops committed 2d32451

Initial import.

  • Participants

Comments (0)

Files changed (12)

+.project
+.settings
+.classpath
+target
+# Custom Annotation Processing Examples
+
+Playing around with Java 8 and the annotation processing API..
+
+----
+
+**2014 Micha Kops / hasCode.com**
+<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>
+	<artifactId>api</artifactId>
+
+	<parent>
+		<groupId>com.hascode.tutorial</groupId>
+		<artifactId>custom-annotation-processor</artifactId>
+		<version>1.0.0</version>
+	</parent>
+</project>

api/src/main/java/com/hascode/tutorial/annotation/Dao.java

+package com.hascode.tutorial.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Target(ElementType.TYPE)
+public @interface Dao {
+	Class<?> entity();
+
+	Class<?> idClass() default Long.class;
+}
+<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>
+	<artifactId>impl</artifactId>
+
+	<parent>
+		<groupId>com.hascode.tutorial</groupId>
+		<artifactId>custom-annotation-processor</artifactId>
+		<version>1.0.0</version>
+	</parent>
+
+	<dependencies>
+		<dependency>
+			<groupId>${project.groupId}</groupId>
+			<artifactId>api</artifactId>
+			<version>1.0.0</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+					<compilerArgument>-proc:none</compilerArgument>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

impl/src/main/java/com/hascode/tutorial/processor/DaoProcessor.java

+package com.hascode.tutorial.processor;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.Set;
+
+import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.Filer;
+import javax.annotation.processing.Messager;
+import javax.annotation.processing.RoundEnvironment;
+import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedSourceVersion;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.MirroredTypeException;
+import javax.lang.model.type.TypeMirror;
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import com.hascode.tutorial.annotation.Dao;
+
+@SupportedAnnotationTypes({ "com.hascode.tutorial.annotation.Dao" })
+@SupportedSourceVersion(SourceVersion.RELEASE_8)
+public class DaoProcessor extends AbstractProcessor {
+
+	@Override
+	public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
+		Messager messager = processingEnv.getMessager();
+		Filer filer = processingEnv.getFiler();
+
+		for (TypeElement te : annotations) {
+			for (Element e : roundEnv.getElementsAnnotatedWith(te)) {
+				String fqEntityClassName = null;
+				Element entityElement = null;
+				try {
+					fqEntityClassName = e.getAnnotation(Dao.class).entity().getName();
+				} catch (MirroredTypeException mte) {
+					TypeMirror typeMirror = mte.getTypeMirror();
+					entityElement = processingEnv.getTypeUtils().asElement(typeMirror);
+					fqEntityClassName = entityElement.toString();
+				}
+				String fqIdClassName = null;
+				try {
+					fqIdClassName = e.getAnnotation(Dao.class).idClass().getName();
+				} catch (MirroredTypeException mte) {
+					TypeMirror typeMirror = mte.getTypeMirror();
+					fqIdClassName = processingEnv.getTypeUtils().asElement(typeMirror).toString();
+				}
+				messager.printMessage(Diagnostic.Kind.NOTE, "dao annotation on type " + e.toString() + " found. entity is " + fqEntityClassName + " and has an id of type: " + fqIdClassName);
+				try {
+					JavaFileObject f = processingEnv.getFiler().createSourceFile(fqEntityClassName + "Dao");
+					processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Creating " + f.toUri());
+					Writer w = f.openWriter();
+					try {
+						PrintWriter pw = new PrintWriter(w);
+						pw.println("package com.hascode.tutorial.app;");
+						pw.println("public class BookDao {");
+						pw.println("    public void print() {");
+						pw.println("        System.out.println(\"Helo world!\");");
+						pw.println("    }");
+						pw.println("}");
+						pw.flush();
+					} finally {
+						w.close();
+					}
+				} catch (IOException x) {
+					processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, x.toString());
+				}
+			}
+		}
+		return true;
+	}
+}

impl/src/main/resources/META-INF/services/javax.annotation.processing.Processor

+com.hascode.tutorial.processor.DaoProcessor
+<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.hascode.tutorial</groupId>
+	<artifactId>custom-annotation-processor</artifactId>
+	<version>1.0.0</version>
+	<packaging>pom</packaging>
+
+	<modules>
+		<module>api</module>
+		<module>impl</module>
+		<module>project</module>
+	</modules>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>3.1</version>
+				<configuration>
+					<source>1.8</source>
+					<target>1.8</target>
+					<showWarnings>true</showWarnings>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>
+<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>
+	<artifactId>project</artifactId>
+
+	<parent>
+		<groupId>com.hascode.tutorial</groupId>
+		<artifactId>custom-annotation-processor</artifactId>
+		<version>1.0.0</version>
+	</parent>
+
+	<dependencies>
+		<dependency>
+			<groupId>${project.groupId}</groupId>
+			<artifactId>api</artifactId>
+			<version>1.0.0</version>
+		</dependency>
+		<dependency>
+			<groupId>${project.groupId}</groupId>
+			<artifactId>impl</artifactId>
+			<version>1.0.0</version>
+		</dependency>
+	</dependencies>
+</project>

project/src/main/java/com/hascode/tutorial/app/Book.java

+package com.hascode.tutorial.app;
+
+public class Book {
+	private Long id;
+	private String title;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(final String title) {
+		this.title = title;
+	}
+}

project/src/main/java/com/hascode/tutorial/app/Main.java

+package com.hascode.tutorial.app;
+
+public class Main {
+	public static void main(final String[] args) {
+		System.out.println("foooo");
+		SomeClass some = new SomeClass();
+		System.out.println(some.foo());
+		BookDao dao = new BookDao();
+	}
+}

project/src/main/java/com/hascode/tutorial/app/SomeClass.java

+package com.hascode.tutorial.app;
+
+import com.hascode.tutorial.annotation.Dao;
+
+@Dao(entity = Book.class)
+public class SomeClass {
+	public String foo() {
+		return "foo";
+	}
+}