Add simple annotation processor for tests

This commit is contained in:
Yan Zhulanow
2015-06-11 16:44:27 +03:00
parent eda7f49b61
commit 37d52501c8
5 changed files with 137 additions and 0 deletions
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven-plugin-anno.version>1.4.1</maven-plugin-anno.version>
<maven.version>3.0.4</maven.version>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>annotation-processor-example</artifactId>
<packaging>jar</packaging>
<description>Simple Annotation Processor for testing kapt</description>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<configuration>
<annotationPaths>
<annotationPath>${basedir}/kotlinAnnotation</annotationPath>
</annotationPaths>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jetbrains-utils</id>
<url>http://repository.jetbrains.com/utils</url>
</repository>
</repositories>
</project>
@@ -0,0 +1,6 @@
package example
import java.lang.annotation.ElementType
import java.lang.annotation.Target
annotation public class ExampleAnnotation
@@ -0,0 +1,39 @@
package example
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.ElementKind
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic
public class ExampleAnnotationProcessor : AbstractProcessor() {
private companion object {
val ANNOTATION_FQ_NAME = javaClass<ExampleAnnotation>().getCanonicalName()
}
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
val elements = roundEnv.getElementsAnnotatedWith(javaClass<ExampleAnnotation>())
val elementUtils = processingEnv.getElementUtils()
val filer = processingEnv.getFiler()
for (element in elements) {
val packageName = elementUtils.getPackageOf(element).getQualifiedName().toString()
val className = element.getSimpleName().toString().capitalize() + "Generated"
filer.createSourceFile(className).openWriter().use { with(it) {
appendln("package $packageName;")
appendln()
appendln("public final class $className {}")
}}
}
return true;
}
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_6
override fun getSupportedAnnotationTypes() = setOf(ANNOTATION_FQ_NAME)
}