Kapt3: Support 'processors' option in Gradle plugin (KT-8558)
Also warn if kapt3 options are used without the "apply plugin: 'kotlin-kapt'" specified.
This commit is contained in:
+7
-1
@@ -6,6 +6,7 @@ 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
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class ExampleAnnotationProcessor : AbstractProcessor() {
|
||||
@@ -18,6 +19,7 @@ class ExampleAnnotationProcessor : AbstractProcessor() {
|
||||
|
||||
val SUFFIX_OPTION = "suffix"
|
||||
val GENERATE_KOTLIN_CODE_OPTION = "generate.kotlin.code"
|
||||
val GENERATE_ERROR = "generate.error"
|
||||
val KAPT_KOTLIN_GENERATED_OPTION = "kapt.kotlin.generated"
|
||||
}
|
||||
|
||||
@@ -57,11 +59,15 @@ class ExampleAnnotationProcessor : AbstractProcessor() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options[GENERATE_ERROR] == "true") {
|
||||
processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, "Error from annotation processor!")
|
||||
}
|
||||
}
|
||||
|
||||
override fun getSupportedSourceVersion() = SourceVersion.RELEASE_6
|
||||
|
||||
override fun getSupportedAnnotationTypes() = ANNOTATION_TO_PREFIX.keys.map { it.java.canonicalName }.toSet()
|
||||
|
||||
override fun getSupportedOptions() = setOf(SUFFIX_OPTION)
|
||||
override fun getSupportedOptions() = setOf(SUFFIX_OPTION, GENERATE_KOTLIN_CODE_OPTION, GENERATE_ERROR)
|
||||
}
|
||||
+15
@@ -43,6 +43,21 @@ class Kapt3IT : BaseGradleIT() {
|
||||
KAPT_SUCCESSFUL_REGEX.findAll(this.output).count() > 0
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAnnotationProcessorAsFqName() {
|
||||
val project = Project("annotationProcessorAsFqName", GRADLE_VERSION, directoryPrefix = "kapt2")
|
||||
|
||||
project.build("build") {
|
||||
assertSuccessful()
|
||||
assertKaptSuccessful()
|
||||
assertContains(":compileKotlin")
|
||||
assertContains(":compileJava")
|
||||
assertFileExists("build/generated/source/kapt/main/example/TestClassGenerated.java")
|
||||
assertFileExists("build/classes/main/example/TestClass.class")
|
||||
assertFileExists("build/classes/main/example/TestClassGenerated.class")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimple() {
|
||||
val project = Project("simple", GRADLE_VERSION, directoryPrefix = "kapt2")
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: "java"
|
||||
apply plugin: "kotlin"
|
||||
apply plugin: "kotlin-kapt"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
kapt {
|
||||
processors = "example.ExampleAnnotationProcessor"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
kapt "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
compile "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
kapt.verbose=true
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example
|
||||
|
||||
@example.ExampleAnnotation
|
||||
class TestClass
|
||||
+8
@@ -51,6 +51,14 @@ internal fun Project.initKapt(
|
||||
val kaptExtension = extensions.getByType(KaptExtension::class.java)
|
||||
val kotlinAfterJavaTask: KotlinCompile?
|
||||
|
||||
fun warnUnsupportedKapt1Option(optionName: String) {
|
||||
kotlinTask.logger.kotlinWarn("'$optionName' option is not supported by this kapt implementation. " +
|
||||
"Please add the \"apply plugin: 'kotlin-kapt\" line to your build script to enable it.")
|
||||
}
|
||||
|
||||
if (kaptExtension.processors.isNotEmpty()) warnUnsupportedKapt1Option("processors")
|
||||
if (kaptExtension.correctErrorTypes) warnUnsupportedKapt1Option("correctErrorTypes")
|
||||
|
||||
if (kaptExtension.generateStubs) {
|
||||
kotlinAfterJavaTask = createKotlinAfterJavaTask(javaTask, kotlinTask, kotlinOptions, tasksProvider)
|
||||
|
||||
|
||||
+5
@@ -173,6 +173,11 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
pluginOptions += SubpluginOption("sources", generatedFilesDir.canonicalPath)
|
||||
pluginOptions += SubpluginOption("classes", getKaptClasssesDir(project, sourceSetName).canonicalPath)
|
||||
|
||||
val annotationProcessors = kaptExtension.processors
|
||||
if (annotationProcessors.isNotEmpty()) {
|
||||
pluginOptions += SubpluginOption("processors", annotationProcessors)
|
||||
}
|
||||
|
||||
val androidPlugin = variantData?.let {
|
||||
project.extensions.findByName("android") as? BaseExtension
|
||||
}
|
||||
|
||||
+2
@@ -30,6 +30,8 @@ open class KaptExtension {
|
||||
|
||||
open var correctErrorTypes: Boolean = false
|
||||
|
||||
open var processors: String = ""
|
||||
|
||||
private var closure: Closure<*>? = null
|
||||
|
||||
open fun arguments(closure: Closure<*>) {
|
||||
|
||||
Reference in New Issue
Block a user