KT-31127: add only generated Java sources to JavaCompile inputs

This commit changes how generated sources are added to the Gradle JavaCompile
task. Previously, directory was added to sources which caused issues with
annotation processors that only generate Kotlin sources. I.e. JavaCompile task
was executed but no java sources existed.

Now, only generated Java sources are added to the JavaCompile task. This
means that if only Kotlin sources are generated JavaCompile task will be
skipped.

Fixes: KT-31127
Test: Kapt3IT.testKotlinProcessorUsingFiler
This commit is contained in:
Ivan Gavrilovic
2019-05-28 17:19:26 +01:00
committed by Yan Zhulanow
parent 55ba985cd2
commit 695f202e46
6 changed files with 86 additions and 5 deletions
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package example
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.tools.StandardLocation
/** Annotation processor that users [javax.annotation.processing.Filer] APIs to generate Kotlin sources. */
class KotlinFilerGeneratingProcessor : AbstractProcessor() {
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
val annotatedWith = roundEnv.getElementsAnnotatedWith(KotlinFilerGenerated::class.java)
for (element in annotatedWith) {
val packageName = processingEnv.elementUtils.getPackageOf(element).qualifiedName.toString()
val simpleName = element.simpleName.toString()
processingEnv.filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, "${simpleName}Generated.kt", element)
.openWriter().use {
it.write(
"""
package $packageName
class ${simpleName}Generated
""".trimIndent()
)
}
}
return true
}
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_6
override fun getSupportedAnnotationTypes(): Set<String> {
return setOf(KotlinFilerGenerated::class.java.canonicalName)
}
}
@@ -17,4 +17,6 @@ annotation class ExampleBinaryAnnotation
@Retention(AnnotationRetention.RUNTIME)
annotation class ExampleRuntimeAnnotation
annotation class GenError
annotation class GenError
annotation class KotlinFilerGenerated