Incremental KAPT - handle inherited annotations

Handle annotations with @Inherited. This is important for
the aggregating APs, as the current implementation passes all sources
annotated with claimed aggregating annotations.

Issue is https://youtrack.jetbrains.com/issue/KT-23880
This commit is contained in:
Ivan Gavrilovic
2019-03-15 17:38:31 +00:00
committed by Alexey Tsvetkov
parent 9f14daa682
commit 2f3d234516
12 changed files with 105 additions and 20 deletions
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.kapt.base.test.org.jetbrains.kotlin.kapt3.base.incremental
import org.jetbrains.kotlin.kapt3.base.incremental.JavaClassCacheManager
import org.jetbrains.kotlin.kapt3.base.incremental.MentionedTypesTaskListener
import org.junit.Assert.assertEquals
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
private val MY_TEST_DIR = File("plugins/kapt3/kapt3-base/testData/runner/incremental/complex/inherited")
class TestInheritedAnnotation {
companion object {
@ClassRule
@JvmField
var tmp = TemporaryFolder()
private lateinit var cache: JavaClassCacheManager
private lateinit var generatedSources: File
@JvmStatic
@BeforeClass
fun setUp() {
val classpathHistory = tmp.newFolder()
cache = JavaClassCacheManager(tmp.newFolder(), classpathHistory)
generatedSources = tmp.newFolder()
cache.close()
classpathHistory.resolve("0").createNewFile()
val processor = SimpleProcessor().toAggregating()
val srcFiles = listOf(
"BaseClass.java",
"InheritableAnnotation.java",
"ExtendsBase.java"
).map { File(MY_TEST_DIR, it) }
runAnnotationProcessing(
srcFiles,
listOf(processor),
generatedSources
) { trees -> MentionedTypesTaskListener(cache.javaCache, trees) }
cache.updateCache(listOf(processor))
}
}
@Test
fun testAnnotationInherited() {
val shouldInheritAnnotation = cache.javaCache.getStructure(MY_TEST_DIR.resolve("ExtendsBase.java"))!!
assertEquals(setOf("test.ExtendsBase"), shouldInheritAnnotation.getDeclaredTypes())
assertEquals(setOf("test.InheritableAnnotation"), shouldInheritAnnotation.getMentionedAnnotations())
assertEquals(emptySet<String>(), shouldInheritAnnotation.getPrivateTypes())
assertEquals(
setOf(
"test.ExtendsBase",
"test.BaseClass"
), shouldInheritAnnotation.getMentionedTypes()
)
assertEquals(emptyMap<String, String>(), shouldInheritAnnotation.getDefinedConstants())
}
}
@@ -73,7 +73,7 @@ class TestSimpleIncrementalAptCache {
srcFiles,
listOf(processor),
generatedSources
) { trees -> MentionedTypesTaskListener(cache.javaCache, trees) }
) { elementUtils -> MentionedTypesTaskListener(cache.javaCache, elementUtils) }
cache.updateCache(listOf(processor))
}
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.kapt.base.test.org.jetbrains.kotlin.kapt3.base.incremental
import com.sun.source.util.TaskListener
import com.sun.source.util.Trees
import com.sun.tools.javac.api.JavacTaskImpl
import org.jetbrains.kotlin.kapt3.base.incremental.DeclaredProcType
import org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor
@@ -18,6 +17,7 @@ import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Elements
import javax.tools.StandardLocation
import javax.tools.ToolProvider
@@ -27,7 +27,7 @@ fun runAnnotationProcessing(
srcFiles: List<File>,
processor: List<IncrementalProcessor>,
generatedSources: File,
listener: (Trees) -> TaskListener? = { null }
listener: (Elements) -> TaskListener? = { null }
) {
val compiler = ToolProvider.getSystemJavaCompiler()
compiler.getStandardFileManager(null, null, null).use { fileManager ->
@@ -42,7 +42,7 @@ fun runAnnotationProcessing(
javaSrcs
) as JavacTaskImpl
val taskListener = listener(Trees.instance(compilationTask))
val taskListener = listener(compilationTask.elements)
taskListener?.let { compilationTask.addTaskListener(it) }
compilationTask.setProcessors(processor)