diff --git a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/KaptContext.kt b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/KaptContext.kt index 5b8af760d28..264324babcb 100644 --- a/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/KaptContext.kt +++ b/plugins/kapt3/kapt3-base/src/org/jetbrains/kotlin/kapt3/base/KaptContext.kt @@ -150,7 +150,9 @@ open class KaptContext(val options: KaptOptions, val withJdk: Boolean, val logge } compiler = JavaCompiler.instance(context) as KaptJavaCompiler - compiler.keepComments = true + if (options.flags[KaptFlag.KEEP_KDOC_COMMENTS_IN_STUBS]) { + compiler.keepComments = true + } ClassReader.instance(context).saveParameterNames = true diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt index 3d363c91f3f..feddf55cbaa 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/ClassFileToSourceStubConverter.kt @@ -130,7 +130,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati private val signatureParser = SignatureParser(treeMaker) - private val kdocCommentKeeper = KDocCommentKeeper(kaptContext) + private val kdocCommentKeeper = if (keepKdocComments) KDocCommentKeeper(kaptContext) else null private val importsFromRoot by lazy(::collectImportsFromRootPackage) @@ -209,7 +209,9 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati val classes = JavacList.of(classDeclaration) val topLevel = treeMaker.TopLevelJava9Aware(packageClause, nonEmptyImports + classes) - topLevel.docComments = kdocCommentKeeper.getDocTable(topLevel) + if (kdocCommentKeeper != null) { + topLevel.docComments = kdocCommentKeeper.getDocTable(topLevel) + } KaptJavaFileObject(topLevel, classDeclaration).apply { topLevel.sourcefile = this @@ -440,11 +442,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati superTypes.superClass, superTypes.interfaces, enumValues + sortedFields + sortedMethods + nestedClasses - ).also { - if (keepKdocComments) { - it.keepKdocComments(clazz) - } - } + ).keepKdocCommentsIfNecessary(clazz) } private class MemberData(val name: String, val descriptor: String, val position: KotlinPosition?) @@ -734,11 +732,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati lineMappings.registerField(containingClass, field) val initializer = explicitInitializer ?: convertPropertyInitializer(containingClass, field) - return treeMaker.VarDef(modifiers, treeMaker.name(name), typeExpression, initializer).also { - if (keepKdocComments) { - it.keepKdocComments(field) - } - } + return treeMaker.VarDef(modifiers, treeMaker.name(name), typeExpression, initializer).keepKdocCommentsIfNecessary(field) } private fun convertPropertyInitializer(containingClass: ClassNode, field: FieldNode): JCExpression? { @@ -986,11 +980,7 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati modifiers, treeMaker.name(name), returnType, genericSignature.typeParameters, genericSignature.parameterTypes, genericSignature.exceptionTypes, body, defaultValue - ).keepSignature(lineMappings, method).also { - if (keepKdocComments) { - it.keepKdocComments(method) - } - } + ).keepSignature(lineMappings, method).keepKdocCommentsIfNecessary(method) } private fun isIgnored(annotations: List?): Boolean { @@ -1455,8 +1445,8 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati else -> null } - private fun T.keepKdocComments(node: Any): T { - kdocCommentKeeper.saveKDocComment(this, node) + private fun T.keepKdocCommentsIfNecessary(node: Any): T { + kdocCommentKeeper?.saveKDocComment(this, node) return this }