kapt: Support Kotlin code generation
This commit is contained in:
+10
-2
@@ -54,6 +54,12 @@ public class AnnotationProcessingManager(
|
||||
return File(aptWorkingDir, "$WRAPPERS_DIRECTORY/annotations.$taskQualifier.txt")
|
||||
}
|
||||
|
||||
fun getGeneratedKotlinSourceDir(): File {
|
||||
val kotlinGeneratedDir = File(aptWorkingDir, "kotlinGenerated")
|
||||
if (!kotlinGeneratedDir.exists()) kotlinGeneratedDir.mkdirs()
|
||||
return kotlinGeneratedDir
|
||||
}
|
||||
|
||||
fun setupKapt() {
|
||||
if (aptFiles.isEmpty()) return
|
||||
|
||||
@@ -72,7 +78,8 @@ public class AnnotationProcessingManager(
|
||||
|
||||
addGeneratedSourcesOutputToCompilerArgs(javaTask, aptOutputDir)
|
||||
|
||||
appendAnnotationsFileLocationArgument()
|
||||
|
||||
appendAnnotationsArguments()
|
||||
appendAdditionalComplerArgs()
|
||||
}
|
||||
|
||||
@@ -102,9 +109,10 @@ public class AnnotationProcessingManager(
|
||||
}
|
||||
}
|
||||
|
||||
private fun appendAnnotationsFileLocationArgument() {
|
||||
private fun appendAnnotationsArguments() {
|
||||
javaTask.modifyCompilerArguments { list ->
|
||||
list.add("-Akapt.annotations=" + getAnnotationFile())
|
||||
list.add("-Akapt.kotlin.generated=" + getGeneratedKotlinSourceDir())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
@@ -609,11 +609,13 @@ private fun Project.initKapt(
|
||||
|
||||
kotlinTask.doFirst {
|
||||
kaptManager.generateJavaHackFile()
|
||||
kotlinAfterJavaTask?.source(kaptManager.getGeneratedKotlinSourceDir())
|
||||
}
|
||||
|
||||
javaTask.doFirst {
|
||||
kaptManager.setupKapt()
|
||||
kaptManager.generateJavaHackFile()
|
||||
kotlinAfterJavaTask?.source(kaptManager.getGeneratedKotlinSourceDir())
|
||||
}
|
||||
|
||||
javaTask.doLast {
|
||||
|
||||
+15
@@ -158,4 +158,19 @@ class KotlinGradleIT: BaseGradleIT() {
|
||||
assertFileExists("build/classes/main/example/AncestorClassGenerated.class")
|
||||
}
|
||||
}
|
||||
|
||||
Test fun testKaptOutputKotlinCode() {
|
||||
Project("kaptOutputKotlinCode", "1.12").build("build") {
|
||||
assertSuccessful()
|
||||
assertContains("kapt: Using class file stubs")
|
||||
assertContains(":compileKotlin")
|
||||
assertContains(":compileJava")
|
||||
assertFileExists("build/tmp/kapt/main/wrappers/annotations.main.txt")
|
||||
assertFileExists("build/generated/source/kapt/main/TestClassCustomized.java")
|
||||
assertFileExists("build/tmp/kapt/main/kotlinGenerated/TestClass.kt")
|
||||
assertFileExists("build/classes/main/example/TestClass.class")
|
||||
assertFileExists("build/classes/main/example/TestClassCustomized.class")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven {
|
||||
url 'file://' + pathToKotlinPlugin
|
||||
}
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.1-SNAPSHOT"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: "java"
|
||||
apply plugin: "kotlin"
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url 'file://' + pathToKotlinPlugin
|
||||
}
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:0.1-SNAPSHOT"
|
||||
compile "org.jetbrains.kotlin:annotation-processor-example:0.1-SNAPSHOT"
|
||||
kapt "org.jetbrains.kotlin:annotation-processor-example:0.1-SNAPSHOT"
|
||||
}
|
||||
|
||||
task show << {
|
||||
buildscript.configurations.classpath.each { println it }
|
||||
}
|
||||
|
||||
task wrapper(type: Wrapper) {
|
||||
gradleVersion="1.12"
|
||||
}
|
||||
|
||||
kapt {
|
||||
generateStubs = true
|
||||
arguments {
|
||||
arg("suffix", "Customized")
|
||||
arg("generate.kotlin.code", "true")
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package example
|
||||
|
||||
@example.ExampleAnnotation
|
||||
public class TestClass {
|
||||
|
||||
fun test() {
|
||||
println(this.customToString())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user