From abda4bfb57818448554b34cb4715243dfb2a5ba5 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Wed, 24 Jan 2018 00:06:51 +0300 Subject: [PATCH] Kapt: Remove comments inside enum values (KT-22350) --- .../stubs/ClassFileToSourceStubConverter.kt | 2 +- .../kotlin/kapt3/stubs/KDocCommentKeeper.kt | 55 ++++++++++++++++--- .../testData/converter/comments.kt | 17 ++++++ .../testData/converter/comments.txt | 17 ++++++ 4 files changed, 81 insertions(+), 10 deletions(-) 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 ac39d5cb2d5..c8f9fa1b920 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 @@ -175,7 +175,7 @@ class ClassFileToSourceStubConverter( val classes = JavacList.of(classDeclaration) val topLevel = treeMaker.TopLevelJava9Aware(packageClause, nonEmptyImports + classes) - topLevel.docComments = kdocCommentKeeper.docCommentTable + topLevel.docComments = kdocCommentKeeper.getDocTable(topLevel) KaptJavaFileObject(topLevel, classDeclaration).apply { topLevel.sourcefile = this diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt index 52f69037266..c77e26bee5f 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt @@ -23,19 +23,53 @@ import com.sun.tools.javac.parser.Tokens import com.sun.tools.javac.tree.DCTree import com.sun.tools.javac.tree.DocCommentTable import com.sun.tools.javac.tree.JCTree +import com.sun.tools.javac.tree.TreeScanner import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor -import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.kapt3.KaptContext import org.jetbrains.kotlin.kdoc.lexer.KDocTokens import org.jetbrains.kotlin.kdoc.psi.api.KDoc import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.tree.FieldNode import org.jetbrains.org.objectweb.asm.tree.MethodNode class KDocCommentKeeper(private val kaptContext: KaptContext<*>) { - val docCommentTable: DocCommentTable = KaptDocCommentTable() + private val docCommentTable = KaptDocCommentTable() + + fun getDocTable(file: JCTree.JCCompilationUnit): DocCommentTable { + val map = docCommentTable.takeIf { it.map.isNotEmpty() } ?: return docCommentTable + + // Enum values with doc comments are rendered incorrectly in javac pretty print, + // so we delete the comments. + file.accept(object : TreeScanner() { + var removeComments = false + + override fun visitVarDef(def: JCTree.JCVariableDecl) { + if (!removeComments && (def.modifiers.flags and Opcodes.ACC_ENUM.toLong()) != 0L) { + map.removeComment(def) + + removeComments = true + super.visitVarDef(def) + removeComments = false + return + } + + super.visitVarDef(def) + } + + override fun scan(tree: JCTree?) { + if (removeComments && tree != null) { + map.removeComment(tree) + } + + super.scan(tree) + } + }) + + return docCommentTable + } fun saveKDocComment(tree: JCTree, node: Any) { val origin = kaptContext.origins[node] ?: return @@ -65,10 +99,6 @@ class KDocCommentKeeper(private val kaptContext: KaptContext<*>) { docCommentTable.putComment(tree, KDocComment(extractCommentText(docComment))) } - fun saveComment(tree: JCTree, comment: String) { - docCommentTable.putComment(tree, KDocComment(comment)) - } - private fun extractCommentText(docComment: KDoc): String { return docComment.children.dropWhile { it is PsiWhiteSpace || it.isKDocStart() } .dropLastWhile { it is PsiWhiteSpace || it.isKDocEnd() } @@ -86,8 +116,11 @@ private class KDocComment(val body: String) : Tokens.Comment { override fun isDeprecated() = false } -private class KaptDocCommentTable : DocCommentTable { - private val table = mutableMapOf() +private class KaptDocCommentTable(map: Map = emptyMap()) : DocCommentTable { + private val table = map.toMutableMap() + + val map: Map + get() = table override fun hasComment(tree: JCTree) = tree in table override fun getComment(tree: JCTree) = table[tree] @@ -96,6 +129,10 @@ private class KaptDocCommentTable : DocCommentTable { override fun getCommentTree(tree: JCTree): DCTree.DCDocComment? = null override fun putComment(tree: JCTree, c: Tokens.Comment) { - table.put(tree, c) + table[tree] = c + } + + fun removeComment(tree: JCTree) { + table.remove(tree) } } \ No newline at end of file diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/comments.kt b/plugins/kapt3/kapt3-compiler/testData/converter/comments.kt index 89da2e6cb4d..08dfc918730 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/comments.kt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/comments.kt @@ -42,4 +42,21 @@ annotation class Anno class Test4 { // method simple comment fun method() {} +} + +enum class EnumError { + One { + override fun doIt() = "" + + class OneOne { + /** Documentation for 'test'. */ + fun test() {} + } + }, + Two { + /** Documentation for 'doIt'. */ + override fun doIt() = "" + }; + + abstract fun doIt(): String } \ No newline at end of file diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt b/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt index 5b4de600fa2..62f00e5fd59 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt @@ -10,6 +10,23 @@ public abstract @interface Anno { //////////////////// +import java.lang.System; + +@kotlin.Metadata() +public enum EnumError { + /*public static final*/ One /* = new EnumError() */, + /*public static final*/ Two /* = new EnumError() */; + + @org.jetbrains.annotations.NotNull() + public abstract java.lang.String doIt(); + + EnumError() { + } +} + +//////////////////// + + import java.lang.System; /**