From 37d52501c8a4fbe22460f0e4e873e13f740b60aa Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Thu, 11 Jun 2015 16:44:27 +0300 Subject: [PATCH] Add simple annotation processor for tests --- .../annotation-processor-example/pom.xml | 90 +++++++++++++++++++ .../main/kotlin/example/ExampleAnnotation.kt | 6 ++ .../example/ExampleAnnotationProcessor.kt | 39 ++++++++ .../javax.annotation.processing.Processor | 1 + libraries/pom.xml | 1 + 5 files changed, 137 insertions(+) create mode 100644 libraries/examples/annotation-processor-example/pom.xml create mode 100644 libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotation.kt create mode 100644 libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotationProcessor.kt create mode 100644 libraries/examples/annotation-processor-example/src/main/resources/META-INF/services/javax.annotation.processing.Processor diff --git a/libraries/examples/annotation-processor-example/pom.xml b/libraries/examples/annotation-processor-example/pom.xml new file mode 100644 index 00000000000..1e66c98bb83 --- /dev/null +++ b/libraries/examples/annotation-processor-example/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + 1.4.1 + 3.0.4 + + + + org.jetbrains.kotlin + kotlin-project + 0.1-SNAPSHOT + ../../pom.xml + + + annotation-processor-example + jar + + Simple Annotation Processor for testing kapt + + + + org.jetbrains.kotlin + kotlin-stdlib + ${project.version} + provided + + + + + src/main/kotlin + src/test/kotlin + + + + src/main/resources + true + + + + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${project.version} + + + ${basedir}/kotlinAnnotation + + + + + + compile + compile + compile + + + ${project.basedir}/src/main/kotlin + + + + + + test-compile + test-compile + test-compile + + + + + + maven-deploy-plugin + + true + + + + + + + + jetbrains-utils + http://repository.jetbrains.com/utils + + + diff --git a/libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotation.kt b/libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotation.kt new file mode 100644 index 00000000000..a88da9823f6 --- /dev/null +++ b/libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotation.kt @@ -0,0 +1,6 @@ +package example + +import java.lang.annotation.ElementType +import java.lang.annotation.Target + +annotation public class ExampleAnnotation \ No newline at end of file diff --git a/libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotationProcessor.kt b/libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotationProcessor.kt new file mode 100644 index 00000000000..96d8a0917b6 --- /dev/null +++ b/libraries/examples/annotation-processor-example/src/main/kotlin/example/ExampleAnnotationProcessor.kt @@ -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().getCanonicalName() + } + + override fun process(annotations: MutableSet?, roundEnv: RoundEnvironment): Boolean { + val elements = roundEnv.getElementsAnnotatedWith(javaClass()) + + 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) +} \ No newline at end of file diff --git a/libraries/examples/annotation-processor-example/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/libraries/examples/annotation-processor-example/src/main/resources/META-INF/services/javax.annotation.processing.Processor new file mode 100644 index 00000000000..af5b149f6fb --- /dev/null +++ b/libraries/examples/annotation-processor-example/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -0,0 +1 @@ +example.ExampleAnnotationProcessor \ No newline at end of file diff --git a/libraries/pom.xml b/libraries/pom.xml index be5efa5beb9..263bbfdbab4 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -86,6 +86,7 @@ tools/kotlin-js-library tools/kotlin-annotation-processing + examples/annotation-processor-example tools/kotlin-gradle-plugin tools/kotlin-gradle-plugin-core tools/kotlin-gradle-plugin-api