Kapt3: Support 'processors' option in the compiler plugin (KT-8558)

Support specifying qualified names of annotation processors in the kapt3 compiler plugin (feature-parity with javac).
This commit is contained in:
Yan Zhulanow
2017-02-21 19:01:36 +03:00
parent d30dbf25b5
commit 47ab47d6d1
6 changed files with 22 additions and 6 deletions
@@ -51,13 +51,14 @@ class ClasspathBasedKapt3Extension(
stubsOutputDir: File,
incrementalDataOutputDir: File?,
options: Map<String, String>,
annotationProcessors: String,
aptOnly: Boolean,
val useLightAnalysis: Boolean,
correctErrorTypes: Boolean,
pluginInitializedTime: Long,
logger: KaptLogger
) : AbstractKapt3Extension(compileClasspath, annotationProcessingClasspath, javaSourceRoots, sourcesOutputDir,
classFilesOutputDir, stubsOutputDir, incrementalDataOutputDir, options,
classFilesOutputDir, stubsOutputDir, incrementalDataOutputDir, options, annotationProcessors,
aptOnly, pluginInitializedTime, logger, correctErrorTypes) {
override val analyzePartially: Boolean
get() = useLightAnalysis
@@ -102,6 +103,7 @@ abstract class AbstractKapt3Extension(
val stubsOutputDir: File,
val incrementalDataOutputDir: File?,
val options: Map<String, String>,
val annotationProcessors: String,
val aptOnly: Boolean,
val pluginInitializedTime: Long,
val logger: KaptLogger,
@@ -144,7 +146,7 @@ abstract class AbstractKapt3Extension(
val (annotationProcessingTime) = measureTimeMillis {
kaptContext.doAnnotationProcessing(
javaSourceFiles, processors, compileClasspath, annotationProcessingClasspath,
sourcesOutputDir, classFilesOutputDir)
annotationProcessors, sourcesOutputDir, classFilesOutputDir)
}
logger.info { "Annotation processing took $annotationProcessingTime ms" }
@@ -67,6 +67,9 @@ object Kapt3ConfigurationKeys {
val APT_OPTIONS: CompilerConfigurationKey<String> =
CompilerConfigurationKey.create<String>("annotation processing options")
val ANNOTATION_PROCESSORS: CompilerConfigurationKey<String> =
CompilerConfigurationKey.create<String>("annotation processor qualified names")
val VERBOSE_MODE: CompilerConfigurationKey<String> =
CompilerConfigurationKey.create<String>("verbose mode")
@@ -104,6 +107,10 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
CliOption("apoptions", "options map", "Encoded annotation processor options",
required = false, allowMultipleOccurrences = false)
val ANNOTATION_PROCESSORS_OPTION: CliOption =
CliOption("processors", "<fqname,[fqname2,...]>", "Annotation processor qualified names",
required = false, allowMultipleOccurrences = true)
val VERBOSE_MODE_OPTION: CliOption =
CliOption("verbose", "true | false", "Enable verbose output", required = false)
@@ -122,11 +129,12 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
override val pluginOptions: Collection<CliOption> =
listOf(SOURCE_OUTPUT_DIR_OPTION, ANNOTATION_PROCESSOR_CLASSPATH_OPTION, APT_OPTIONS_OPTION,
CLASS_OUTPUT_DIR_OPTION, VERBOSE_MODE_OPTION, STUBS_OUTPUT_DIR_OPTION, APT_ONLY_OPTION,
USE_LIGHT_ANALYSIS_OPTION, CORRECT_ERROR_TYPES_OPTION)
USE_LIGHT_ANALYSIS_OPTION, CORRECT_ERROR_TYPES_OPTION, ANNOTATION_PROCESSORS_OPTION)
override fun processOption(option: CliOption, value: String, configuration: CompilerConfiguration) {
when (option) {
ANNOTATION_PROCESSOR_CLASSPATH_OPTION -> configuration.appendList(ANNOTATION_PROCESSOR_CLASSPATH, value)
ANNOTATION_PROCESSORS_OPTION -> configuration.put(Kapt3ConfigurationKeys.ANNOTATION_PROCESSORS, value)
APT_OPTIONS_OPTION -> configuration.put(Kapt3ConfigurationKeys.APT_OPTIONS, value)
SOURCE_OUTPUT_DIR_OPTION -> configuration.put(Kapt3ConfigurationKeys.SOURCE_OUTPUT_DIR, value)
CLASS_OUTPUT_DIR_OPTION -> configuration.put(Kapt3ConfigurationKeys.CLASS_OUTPUT_DIR, value)
@@ -172,6 +180,8 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
val stubsOutputDir = configuration.get(Kapt3ConfigurationKeys.STUBS_OUTPUT_DIR)?.let(::File)
val incrementalDataOutputDir = configuration.get(Kapt3ConfigurationKeys.INCREMENTAL_DATA_OUTPUT_DIR)?.let(::File)
val annotationProcessors = configuration.get(Kapt3ConfigurationKeys.ANNOTATION_PROCESSORS) ?: ""
val apClasspath = configuration.get(ANNOTATION_PROCESSOR_CLASSPATH)?.map(::File)
if (sourcesOutputDir == null || classFilesOutputDir == null || apClasspath == null || stubsOutputDir == null) {
@@ -215,13 +225,14 @@ class Kapt3ComponentRegistrar : ComponentRegistrar {
logger.info("Incremental data output directory: $incrementalDataOutputDir")
logger.info("Compile classpath: " + compileClasspath.joinToString())
logger.info("Annotation processing classpath: " + apClasspath.joinToString())
logger.info("Annotation processors: " + annotationProcessors)
logger.info("Java source roots: " + javaSourceRoots.joinToString())
logger.info("Options: $apOptions")
}
val kapt3AnalysisCompletedHandlerExtension = ClasspathBasedKapt3Extension(
compileClasspath, apClasspath, javaSourceRoots, sourcesOutputDir, classFilesOutputDir,
stubsOutputDir, incrementalDataOutputDir, apOptions,
stubsOutputDir, incrementalDataOutputDir, apOptions, annotationProcessors,
isAptOnly, useLightAnalysis, correctErrorTypes, System.currentTimeMillis(), logger)
AnalysisHandlerExtension.registerExtension(project, kapt3AnalysisCompletedHandlerExtension)
}
@@ -35,6 +35,7 @@ fun KaptContext.doAnnotationProcessing(
processors: List<Processor>,
compileClasspath: List<File>,
annotationProcessingClasspath: List<File>,
annotationProcessors: String,
sourcesOutputDir: File,
classesOutputDir: File,
additionalSources: JavacList<JCTree.JCCompilationUnit> = JavacList.nil(),
@@ -49,6 +50,7 @@ fun KaptContext.doAnnotationProcessing(
put(Option.CLASSPATH, compileClasspath.joinToString(File.pathSeparator) { it.canonicalPath })
put(Option.PROCESSORPATH, annotationProcessingClasspath.joinToString(File.pathSeparator) { it.canonicalPath })
put(Option.PROCESSOR, annotationProcessors)
put(Option.S, sourcesOutputDir.canonicalPath)
put(Option.D, classesOutputDir.canonicalPath)
put(Option.ENCODING, "UTF-8")
@@ -162,7 +162,7 @@ abstract class AbstractKotlinKapt3IntegrationTest : CodegenTestCase() {
incrementalDataOutputDir: File
) : AbstractKapt3Extension(PathUtil.getJdkClassesRoots() + PathUtil.getKotlinPathsForIdeaPlugin().runtimePath,
emptyList(), javaSourceRoots, outputDir, outputDir,
stubsOutputDir, incrementalDataOutputDir, options, true, System.currentTimeMillis(),
stubsOutputDir, incrementalDataOutputDir, options, "", true, System.currentTimeMillis(),
KaptLogger(true), correctErrorTypes = true
) {
internal var savedStubs: String? = null
@@ -117,7 +117,7 @@ abstract class AbstractKotlinKaptContextTest : AbstractKotlinKapt3Test() {
try {
kaptRunner.doAnnotationProcessing(emptyList(), listOf(JavaKaptContextTest.simpleProcessor()),
compileClasspath = PathUtil.getJdkClassesRoots() + PathUtil.getKotlinPathsForIdeaPlugin().runtimePath,
annotationProcessingClasspath = emptyList(),
annotationProcessingClasspath = emptyList(), annotationProcessors = "",
sourcesOutputDir = sourceOutputDir, classesOutputDir = sourceOutputDir,
additionalSources = compilationUnits, withJdk = true)
@@ -89,6 +89,7 @@ class JavaKaptContextTest {
listOf(processor),
emptyList(), // compile classpath
emptyList(), // annotation processing classpath
"", // list of annotation processor qualified names
outputDir,
outputDir,
withJdk = true)