Remove source annotations when copy class with kapt2

Writing source annotations enables incremental compilation for kapt2.
However they are not needed in bytecode, so we remove them when
copying classes.

# Conflicts:
#	compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt
#	compiler/frontend.java/src/org/jetbrains/kotlin/config/JVMConfigurationKeys.java
This commit is contained in:
Alexey Tsvetkov
2016-09-06 15:55:03 +03:00
parent 5534350fd6
commit 6ebb50751c
25 changed files with 377 additions and 78 deletions
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper
import org.jetbrains.kotlin.config.APPEND_JAVA_SOURCE_ROOTS_HANDLER_KEY
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.fileClasses.NoResolveFileClassesProvider
import org.jetbrains.kotlin.incremental.components.SourceRetentionAnnotationHandler
import org.jetbrains.kotlin.java.model.elements.JeTypeElement
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents
@@ -51,10 +52,10 @@ class ClasspathBasedAnnotationProcessingExtension(
javaSourceRoots: List<File>,
verboseOutput: Boolean,
incrementalDataFile: File?,
incrementalCompilationComponents: IncrementalCompilationComponents?
sourceRetentionAnnotationHandler: SourceRetentionAnnotationHandler?
) : AbstractAnnotationProcessingExtension(generatedSourcesOutputDir,
classesOutputDir, javaSourceRoots, verboseOutput,
incrementalDataFile, incrementalCompilationComponents) {
incrementalDataFile, sourceRetentionAnnotationHandler) {
override fun loadAnnotationProcessors(): List<Processor> {
val classLoader = URLClassLoader(annotationProcessingClasspath.map { it.toURI().toURL() }.toTypedArray())
return ServiceLoader.load(Processor::class.java, classLoader).toList()
@@ -67,7 +68,7 @@ abstract class AbstractAnnotationProcessingExtension(
val javaSourceRoots: List<File>,
val verboseOutput: Boolean,
val incrementalDataFile: File? = null,
val incrementalCompilationComponents: IncrementalCompilationComponents? = null
val sourceRetentionAnnotationHandler: SourceRetentionAnnotationHandler? = null
) : AnalysisCompletedHandlerExtension {
private companion object {
val LINE_SEPARATOR = System.getProperty("line.separator") ?: "\n"
@@ -170,7 +171,7 @@ abstract class AbstractAnnotationProcessingExtension(
}
val firstRoundAnnotations = RoundAnnotations(
incrementalCompilationComponents?.getSourceRetentionAnnotationHandler(),
sourceRetentionAnnotationHandler,
bindingContext,
createTypeMapper())
@@ -122,11 +122,11 @@ class AnnotationProcessingComponentRegistrar : ComponentRegistrar {
// Annotations with the "SOURCE" retention will be written to class files
project.putUserData(IS_KAPT2_ENABLED_KEY, true)
val incrementalCompilationComponents = configuration[JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS]
val sourceRetentionAnnotationHandler = configuration[JVMConfigurationKeys.SOURCE_RETENTION_ANNOTATION_HANDLER]
val annotationProcessingExtension = ClasspathBasedAnnotationProcessingExtension(
classpath, generatedOutputDirFile, classesOutputDir, javaRoots, verboseOutput,
incrementalDataFile, incrementalCompilationComponents)
incrementalDataFile, sourceRetentionAnnotationHandler)
AnalysisCompletedHandlerExtension.registerExtension(project, annotationProcessingExtension)
}
@@ -43,7 +43,7 @@ import javax.lang.model.element.*
class AnnotationProcessingExtensionForTests(
val processors: List<Processor>
) : AbstractAnnotationProcessingExtension(createTempDir(), createTempDir(), listOf(), true,
createIncrementalDataFile(), StubIncrementalCompilationComponents()) {
createIncrementalDataFile(), SourceRetentionAnnotationHandlerImpl()) {
override fun loadAnnotationProcessors() = processors
private companion object {
@@ -55,14 +55,6 @@ class AnnotationProcessingExtensionForTests(
deleteOnExit()
}
}
private class StubIncrementalCompilationComponents : IncrementalCompilationComponents {
private val sourceRetentionAnnotationHandler = SourceRetentionAnnotationHandlerImpl()
override fun getIncrementalCache(target: TargetId) = null!!
override fun getLookupTracker() = null!!
override fun getSourceRetentionAnnotationHandler() = sourceRetentionAnnotationHandler
}
}
abstract class AbstractProcessorTest : AbstractBytecodeTextTest() {
@@ -233,10 +233,8 @@ class ProcessorTests : AbstractProcessorTest() {
fun testSourceRetention() {
test("SourceRetention", "*") { set, roundEnv, env -> }
val ext = getKapt2Extension()
val incrementalCompilationComponents = ext.incrementalCompilationComponents
assertNotNull(incrementalCompilationComponents)
val annotationHandler = incrementalCompilationComponents!!.getSourceRetentionAnnotationHandler()
val annotations = (annotationHandler as SourceRetentionAnnotationHandlerImpl).sourceRetentionAnnotations.sorted()
val annotationHandler = ext.sourceRetentionAnnotationHandler as SourceRetentionAnnotationHandlerImpl
val annotations = annotationHandler.sourceRetentionAnnotations.sorted()
assertEquals("Source1, Source2, Source3, Source4, Test5\$Source5", annotations.joinToString())
}