Skip to content

Commit dc8fb89

Browse files
committed
Add Kotlin code generation example
1 parent b88d5c2 commit dc8fb89

File tree

16 files changed

+414
-0
lines changed

16 files changed

+414
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea
5+
.DS_Store
6+
/build
7+
/example/build
8+
/annotation-processor/build
9+
/captures
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apply plugin: 'kotlin'
2+
3+
dependencies {
4+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
5+
compile 'com.github.yanex:takenoko:0.1'
6+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.kotlin.annotationProcessor
2+
3+
import org.yanex.takenoko.*
4+
import java.io.File
5+
import javax.annotation.processing.*
6+
import javax.lang.model.SourceVersion
7+
import javax.lang.model.element.Element
8+
import javax.lang.model.element.TypeElement
9+
import javax.tools.Diagnostic.Kind.*
10+
11+
@Target(AnnotationTarget.CLASS)
12+
annotation class TestAnnotation
13+
14+
@SupportedSourceVersion(SourceVersion.RELEASE_8)
15+
@SupportedAnnotationTypes("org.kotlin.annotationProcessor.TestAnnotation")
16+
@SupportedOptions(TestAnnotationProcessor.KAPT_KOTLIN_GENERATED_OPTION_NAME)
17+
class TestAnnotationProcessor : AbstractProcessor() {
18+
companion object {
19+
const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
20+
}
21+
22+
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
23+
val annotatedElements = roundEnv.getElementsAnnotatedWith(TestAnnotation::class.java)
24+
if (annotatedElements.isEmpty()) return false
25+
26+
val kaptKotlinGeneratedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME] ?: run {
27+
processingEnv.messager.printMessage(ERROR, "Can't find the target directory for generated Kotlin files.")
28+
return false
29+
}
30+
31+
val generatedKtFile = kotlinFile("test.generated") {
32+
for (element in annotatedElements) {
33+
val typeElement = element.toTypeElementOrNull() ?: continue
34+
35+
property("simpleClassName") {
36+
receiverType(typeElement.qualifiedName.toString())
37+
getterExpression("this::class.java.simpleName")
38+
}
39+
}
40+
}
41+
42+
File(kaptKotlinGeneratedDir, "testGenerated.kt").apply {
43+
parentFile.mkdirs()
44+
writeText(generatedKtFile.accept(PrettyPrinter(PrettyPrinterConfiguration())))
45+
}
46+
47+
return true
48+
}
49+
50+
fun Element.toTypeElementOrNull(): TypeElement? {
51+
if (this !is TypeElement) {
52+
processingEnv.messager.printMessage(ERROR, "Invalid element type, class expected", this)
53+
return null
54+
}
55+
56+
return this
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.kotlin.annotationProcessor.TestAnnotationProcessor
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
buildscript {
2+
ext.kotlin_version = '1.1-SNAPSHOT'
3+
4+
repositories {
5+
jcenter()
6+
mavenLocal()
7+
}
8+
dependencies {
9+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
10+
}
11+
}
12+
13+
allprojects {
14+
repositories {
15+
jcenter()
16+
mavenLocal()
17+
maven { url 'https://jitpack.io' }
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: 'kotlin'
2+
apply plugin: 'kotlin-kapt'
3+
4+
dependencies {
5+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
6+
7+
compile project(":annotation-processor")
8+
kapt project(":annotation-processor")
9+
10+
testCompile 'junit:junit:4.12'
11+
}
12+
13+
apply plugin: 'idea'
14+
15+
idea {
16+
module {
17+
sourceDirs += files('build/generated/source/kapt/main', 'build/generated/source/kaptKotlin/main')
18+
generatedSourceDirs += files('build/generated/source/kapt/main', 'build/generated/source/kaptKotlin/main')
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.kotlin.test
2+
3+
import org.kotlin.annotationProcessor.TestAnnotation
4+
5+
@TestAnnotation
6+
class SimpleClass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.kotlin.test
2+
3+
import org.junit.Test
4+
import org.junit.Assert.*
5+
import test.generated.simpleClassName
6+
7+
class AnnotationTest {
8+
@Test fun testSimple() {
9+
assertEquals("SimpleClass", SimpleClass().simpleClassName)
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kapt.verbose=true
52.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)