From c6c167390209a844dd4efde6518f8944361314e8 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Fri, 2 Sep 2016 22:53:25 +0300 Subject: [PATCH] Kapt: load all annotations, even if annotation processors does not require it explicitly. Some annotation processors may want to process some more annotations (see DbFlow, Database annotation). Blacklist some common-used Java and Kotlin annotations instead (like Deprecated, Nullable or Metadata). (cherry picked from commit 6856a7c) --- .../AnnotationProcessingExtension.kt | 4 +-- .../annotation/processing/RoundAnnotations.kt | 16 +++++++--- .../testData/elements/GetAllMembers.kt | 4 ++- .../testData/elements/GetPackageOf.kt | 4 ++- .../testData/elements/IsDeprecated.kt | 3 ++ .../elements/IsFunctionalInterface.kt | 3 ++ .../testData/elements/Overrides.kt | 4 ++- .../testData/elements/Overrides2.kt | 4 ++- .../kotlinWrappers/kotlinAnnotations.kt | 22 +++++++++++++ .../kotlinWrappers/kotlinAnnotations.txt | 31 +++++++++++++++++++ .../testData/processors/Erasure2.kt | 4 ++- .../testData/processors/ErasureSimple.kt | 4 ++- .../testData/processors/KotlinAnnotations.kt | 25 +++++++++++++++ .../testData/processors/TypeArguments.kt | 4 ++- .../testData/processors/TypeArguments2.kt | 4 ++- .../test/processor/ElementsTests.kt | 2 +- .../test/processor/ProcessorTests.kt | 6 +++- .../KotlinModelWrappersTestGenerated.java | 6 ++++ 18 files changed, 132 insertions(+), 18 deletions(-) create mode 100644 plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.kt create mode 100644 plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.txt create mode 100644 plugins/annotation-processing/testData/processors/KotlinAnnotations.kt diff --git a/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingExtension.kt b/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingExtension.kt index e8cd36d3c9b..c0837b8b422 100755 --- a/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingExtension.kt +++ b/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/AnnotationProcessingExtension.kt @@ -164,14 +164,12 @@ abstract class AbstractAnnotationProcessingExtension( } private fun KotlinProcessingEnvironment.doAnnotationProcessing(files: Collection): ProcessingResult { - val allSupportedAnnotationFqNames = run initializeProcessors@ { + run initializeProcessors@ { processors.forEach { it.init(this) } log { "Initialized processors: " + processors.joinToString { it.javaClass.name } } - processors.flatMapTo(mutableSetOf()) { it.supportedAnnotationTypes } } val firstRoundAnnotations = RoundAnnotations( - allSupportedAnnotationFqNames, incrementalCompilationComponents?.getSourceRetentionAnnotationHandler(), bindingContext, createTypeMapper()) diff --git a/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/RoundAnnotations.kt b/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/RoundAnnotations.kt index 5cbfec422bb..e6cda54ecac 100644 --- a/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/RoundAnnotations.kt +++ b/plugins/annotation-processing/src/org/jetbrains/kotlin/annotation/processing/RoundAnnotations.kt @@ -29,23 +29,29 @@ import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.BindingContext internal class RoundAnnotations( - val supportedAnnotationFqNames: Set, val sourceRetentionAnnotationHandler: SourceRetentionAnnotationHandler?, val bindingContext: BindingContext, val typeMapper: KotlinTypeMapper ) { + private companion object { + private val BLACKLISTED_ANNOTATATIONS = listOf( + "java.lang.Deprecated", "kotlin.Deprecated", // Deprecated annotations + "java.lang.annotation.", // Java annotations + "org.jetbrains.annotations.", // Nullable/NotNull, ReadOnly, Mutable + "kotlin.jvm.", "kotlin.Metadata" // Kotlin annotations from runtime + ) + } + private val mutableAnnotationsMap = mutableMapOf>() private val mutableAnalyzedClasses = mutableSetOf() - private val acceptsAnyAnnotation = "*" in supportedAnnotationFqNames - val annotationsMap: Map> get() = mutableAnnotationsMap val analyzedClasses: Set get() = mutableAnalyzedClasses - fun copy() = RoundAnnotations(supportedAnnotationFqNames, sourceRetentionAnnotationHandler, bindingContext, typeMapper) + fun copy() = RoundAnnotations(sourceRetentionAnnotationHandler, bindingContext, typeMapper) fun analyzeFiles(files: Collection) = files.forEach { analyzeFile(it) } @@ -105,7 +111,7 @@ internal class RoundAnnotations( } } - if (!acceptsAnyAnnotation && fqName !in supportedAnnotationFqNames) continue + if (BLACKLISTED_ANNOTATATIONS.any { fqName.startsWith(it) }) continue mutableAnnotationsMap.getOrPut(fqName, { mutableListOf() }).add(declaration) // Add only top-level classes diff --git a/plugins/annotation-processing/testData/elements/GetAllMembers.kt b/plugins/annotation-processing/testData/elements/GetAllMembers.kt index 0ef9208ed64..f25f9608527 100644 --- a/plugins/annotation-processing/testData/elements/GetAllMembers.kt +++ b/plugins/annotation-processing/testData/elements/GetAllMembers.kt @@ -1,4 +1,6 @@ -@Deprecated("") +annotation class Anno + +@Anno class MyClass { var myProperty: String = "A" diff --git a/plugins/annotation-processing/testData/elements/GetPackageOf.kt b/plugins/annotation-processing/testData/elements/GetPackageOf.kt index 2c5f13cf013..ca40c542457 100644 --- a/plugins/annotation-processing/testData/elements/GetPackageOf.kt +++ b/plugins/annotation-processing/testData/elements/GetPackageOf.kt @@ -1,6 +1,8 @@ package test -@Deprecated("") +annotation class Anno + +@Anno class MyClass interface MyInterface \ No newline at end of file diff --git a/plugins/annotation-processing/testData/elements/IsDeprecated.kt b/plugins/annotation-processing/testData/elements/IsDeprecated.kt index 9bec4cb4ab3..5ae12b7b99c 100644 --- a/plugins/annotation-processing/testData/elements/IsDeprecated.kt +++ b/plugins/annotation-processing/testData/elements/IsDeprecated.kt @@ -1,3 +1,6 @@ +annotation class Anno + +@Anno @Deprecated("") class Depr diff --git a/plugins/annotation-processing/testData/elements/IsFunctionalInterface.kt b/plugins/annotation-processing/testData/elements/IsFunctionalInterface.kt index 78b59ba86ae..5e36530ebf7 100644 --- a/plugins/annotation-processing/testData/elements/IsFunctionalInterface.kt +++ b/plugins/annotation-processing/testData/elements/IsFunctionalInterface.kt @@ -1,3 +1,6 @@ +annotation class Anno + +@Anno class A { interface AA { fun onClick(o: Any) diff --git a/plugins/annotation-processing/testData/elements/Overrides.kt b/plugins/annotation-processing/testData/elements/Overrides.kt index a3a8b7cab9b..8a96cd7ba0d 100644 --- a/plugins/annotation-processing/testData/elements/Overrides.kt +++ b/plugins/annotation-processing/testData/elements/Overrides.kt @@ -1,4 +1,6 @@ -@Deprecated("") +annotation class Anno + +@Anno open class Parent { open fun a() {} } diff --git a/plugins/annotation-processing/testData/elements/Overrides2.kt b/plugins/annotation-processing/testData/elements/Overrides2.kt index c3c62e5f232..51ce8660843 100644 --- a/plugins/annotation-processing/testData/elements/Overrides2.kt +++ b/plugins/annotation-processing/testData/elements/Overrides2.kt @@ -1,4 +1,6 @@ -@Deprecated("") +annotation class Anno + +@Anno interface Intf { fun a() } diff --git a/plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.kt b/plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.kt new file mode 100644 index 00000000000..9377d1948f7 --- /dev/null +++ b/plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.kt @@ -0,0 +1,22 @@ +// Test + +class Test { + @kotlin.jvm.Transient + val f: String = "" + + // explicitly + @org.jetbrains.annotations.NotNull + fun a() {} + + @kotlin.jvm.Synchronized + fun b() {} + + @kotlin.jvm.JvmOverloads + fun c(a: Int = 3, b: String = "") {} + + @java.lang.Deprecated + fun d() {} + + @kotlin.Deprecated("") + fun e() {} +} \ No newline at end of file diff --git a/plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.txt b/plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.txt new file mode 100644 index 00000000000..4cd7a94c13f --- /dev/null +++ b/plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.txt @@ -0,0 +1,31 @@ +public final class Test { + @kotlin.jvm.Transient + @org.jetbrains.annotations.NotNull + private final transient java.lang.String f + + @org.jetbrains.annotations.NotNull + public final java.lang.String getF() + + @org.jetbrains.annotations.NotNull + public final void a() + + @kotlin.jvm.Synchronized + public final synchronized void b() + + @kotlin.jvm.JvmOverloads + public final void c(int a, java.lang.String b) + + @kotlin.jvm.JvmOverloads + public void c(int a) + + @kotlin.jvm.JvmOverloads + public void c() + + @java.lang.Deprecated + public final void d() + + @kotlin.Deprecated(message = "") + public final void e() + + public void () +} diff --git a/plugins/annotation-processing/testData/processors/Erasure2.kt b/plugins/annotation-processing/testData/processors/Erasure2.kt index ec6d6270f7a..ba1e6dccbb7 100644 --- a/plugins/annotation-processing/testData/processors/Erasure2.kt +++ b/plugins/annotation-processing/testData/processors/Erasure2.kt @@ -2,7 +2,9 @@ abstract class Base { fun baseF(): A = null!! } -@Deprecated("") +annotation class Anno + +@Anno class Test : Base() { fun a(): String = "" fun b(i: String, b: CharSequence) {} diff --git a/plugins/annotation-processing/testData/processors/ErasureSimple.kt b/plugins/annotation-processing/testData/processors/ErasureSimple.kt index b66e20d43e3..3173669eabb 100644 --- a/plugins/annotation-processing/testData/processors/ErasureSimple.kt +++ b/plugins/annotation-processing/testData/processors/ErasureSimple.kt @@ -1,4 +1,6 @@ -@Deprecated("") +annotation class Anno + +@Anno class Test { fun a(): Int = 5 fun b(): Unit {} diff --git a/plugins/annotation-processing/testData/processors/KotlinAnnotations.kt b/plugins/annotation-processing/testData/processors/KotlinAnnotations.kt new file mode 100644 index 00000000000..2cee342a12e --- /dev/null +++ b/plugins/annotation-processing/testData/processors/KotlinAnnotations.kt @@ -0,0 +1,25 @@ +annotation class Anno + +@Anno +class AnnoAnnotated + +class Test { + @kotlin.jvm.Transient + val f: String = "" + + // explicitly + @org.jetbrains.annotations.NotNull + fun a() {} + + @kotlin.jvm.Synchronized + fun b() {} + + @kotlin.jvm.JvmOverloads + fun c(a: Int = 3, b: String = "") {} + + @java.lang.Deprecated + fun d() {} + + @kotlin.Deprecated("") + fun e() {} +} \ No newline at end of file diff --git a/plugins/annotation-processing/testData/processors/TypeArguments.kt b/plugins/annotation-processing/testData/processors/TypeArguments.kt index 99e87f29f5a..43bf6bf997e 100644 --- a/plugins/annotation-processing/testData/processors/TypeArguments.kt +++ b/plugins/annotation-processing/testData/processors/TypeArguments.kt @@ -1,4 +1,6 @@ -@Deprecated("") +annotation class Anno + +@Anno class A : B(), C open class B diff --git a/plugins/annotation-processing/testData/processors/TypeArguments2.kt b/plugins/annotation-processing/testData/processors/TypeArguments2.kt index 9708c8ab09e..d8a08aab9d1 100644 --- a/plugins/annotation-processing/testData/processors/TypeArguments2.kt +++ b/plugins/annotation-processing/testData/processors/TypeArguments2.kt @@ -2,7 +2,9 @@ interface I abstract class A : I -@Deprecated("") +annotation class Anno + +@Anno class B : A() class C : A() \ No newline at end of file diff --git a/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ElementsTests.kt b/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ElementsTests.kt index 8b4ce228209..36809af49c9 100644 --- a/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ElementsTests.kt +++ b/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ElementsTests.kt @@ -127,7 +127,7 @@ class ElementsTests : AbstractProcessorTest() { assertEquals(packageElement, testPackageElement) assertFalse(packageElement.isUnnamed) val classes = packageElement.enclosedElements.filterIsInstance() - assertEquals(2, classes.size) + assertEquals(3, classes.size) } fun testGetArrayType() = test("GetPackageOf", "*") { set, roundEnv, env -> diff --git a/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ProcessorTests.kt b/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ProcessorTests.kt index 410a2a2b8e3..c44b870a455 100644 --- a/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ProcessorTests.kt +++ b/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/processor/ProcessorTests.kt @@ -190,7 +190,11 @@ class ProcessorTests : AbstractProcessorTest() { fun testIncrementalDataSimple() = incrementalDataTest( "IncrementalDataSimple", - "i Intf, i Test, i Test2, i Test3, i Test8") + "i Intf, i Test, i Test2, i Test3, i Test6, i Test7, i Test8") + + fun testIncrementalDataKotlinAnnotations() = incrementalDataTest( + "KotlinAnnotations", + "i AnnoAnnotated") private fun getKapt2Extension() = AnalysisCompletedHandlerExtension.getInstances(myEnvironment.project) .firstIsInstance() diff --git a/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/wrappers/KotlinModelWrappersTestGenerated.java b/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/wrappers/KotlinModelWrappersTestGenerated.java index 56440ca9519..f8b8ae1bab2 100644 --- a/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/wrappers/KotlinModelWrappersTestGenerated.java +++ b/plugins/plugins-tests/tests/org/jetbrains/kotlin/annotation/processing/test/wrappers/KotlinModelWrappersTestGenerated.java @@ -47,6 +47,12 @@ public class KotlinModelWrappersTestGenerated extends AbstractKotlinModelWrapper doTest(fileName); } + @TestMetadata("kotlinAnnotations.kt") + public void testKotlinAnnotations() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/kotlinAnnotations.kt"); + doTest(fileName); + } + @TestMetadata("metaAnnotations.kt") public void testMetaAnnotations() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("plugins/annotation-processing/testData/kotlinWrappers/metaAnnotations.kt");