From 93520158b4be59f98015d07e8fecaa7e5a49fd99 Mon Sep 17 00:00:00 2001 From: Marcin Aman Date: Wed, 20 Jan 2021 10:30:50 +0100 Subject: Bring back displaying jvm name when KaJ is not present (#1702) --- .../DefaultDescriptorToDocumentableTranslator.kt | 18 +++++------- .../annotations/ContentForAnnotationsTest.kt | 12 ++++---- .../content/annotations/FileLevelJvmNameTest.kt | 4 +-- .../transformers/JvmNameDocumentableTransformer.kt | 21 ++++++++++++-- .../kotlin-as-java/src/test/kotlin/JvmNameTest.kt | 32 ++++++++++++++++++++-- 5 files changed, 63 insertions(+), 24 deletions(-) (limited to 'plugins') diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 00c9a8e1..1ac519a0 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -23,10 +23,10 @@ import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.transformers.sources.AsyncSourceToDocumentableTranslator import org.jetbrains.dokka.utilities.DokkaLogger -import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor -import org.jetbrains.kotlin.builtins.isBuiltinExtensionFunctionalType import org.jetbrains.dokka.utilities.parallelMap import org.jetbrains.dokka.utilities.parallelMapNotNull +import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor +import org.jetbrains.kotlin.builtins.isBuiltinExtensionFunctionalType import org.jetbrains.kotlin.builtins.isExtensionFunctionType import org.jetbrains.kotlin.builtins.isSuspendFunctionTypeOrSubtype import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface @@ -55,7 +55,6 @@ import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.safeAs -import java.lang.IllegalStateException import java.nio.file.Paths import org.jetbrains.kotlin.resolve.constants.AnnotationValue as ConstantsAnnotationValue import org.jetbrains.kotlin.resolve.constants.ArrayValue as ConstantsArrayValue @@ -931,21 +930,18 @@ private class DokkaDescriptorVisitor( else -> StringValue(unquotedValue(toString())) } - private suspend fun AnnotationDescriptor.toAnnotation(scope: Annotations.AnnotationScope = Annotations.AnnotationScope.DIRECT): Annotations.Annotation { - val dri = DRI.from(annotationClass as DeclarationDescriptor) - return Annotations.Annotation( + private suspend fun AnnotationDescriptor.toAnnotation(scope: Annotations.AnnotationScope = Annotations.AnnotationScope.DIRECT): Annotations.Annotation = + Annotations.Annotation( DRI.from(annotationClass as DeclarationDescriptor), allValueArguments.map { it.key.asString() to it.value.toValue() }.filter { it.second != null }.toMap() as Map, - mustBeDocumented(dri), + mustBeDocumented(), scope ) - } - private fun AnnotationDescriptor.mustBeDocumented(dri: DRI): Boolean = - if (dri.isJvmName()) false - else annotationClass!!.annotations.hasAnnotation(FqName("kotlin.annotation.MustBeDocumented")) + private fun AnnotationDescriptor.mustBeDocumented(): Boolean = + annotationClass?.annotations?.hasAnnotation(FqName("kotlin.annotation.MustBeDocumented")) ?: false private suspend fun PropertyDescriptor.getAnnotationsWithBackingField(): List = getAnnotations() + (backingField?.getAnnotations() ?: emptyList()) diff --git a/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt b/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt index d88d9505..164e5f4a 100644 --- a/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt +++ b/plugins/base/src/test/kotlin/content/annotations/ContentForAnnotationsTest.kt @@ -1,19 +1,19 @@ package content.annotations import matchers.content.* -import org.jetbrains.dokka.pages.ContentPage -import org.jetbrains.dokka.pages.PackagePageNode import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.Annotations import org.jetbrains.dokka.model.StringValue +import org.jetbrains.dokka.pages.ContentPage +import org.jetbrains.dokka.pages.PackagePageNode import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult import org.junit.jupiter.api.Test import utils.ParamAttributes import utils.bareSignature import utils.propertySignature import kotlin.test.assertEquals -import kotlin.test.assertFalse +import kotlin.test.assertTrue class ContentForAnnotationsTest : BaseAbstractTest() { @@ -243,7 +243,7 @@ class ContentForAnnotationsTest : BaseAbstractTest() { dri = DRI("kotlin.jvm", "JvmName"), params = mapOf("name" to StringValue(name)), scope = Annotations.AnnotationScope.DIRECT, - mustBeDocumented = false + mustBeDocumented = true ) val property = modules.flatMap { it.packages }.flatMap { it.properties }.first() @@ -255,11 +255,11 @@ class ContentForAnnotationsTest : BaseAbstractTest() { } assertEquals(expectedAnnotation("xd"), getterAnnotation) - assertFalse(getterAnnotation?.mustBeDocumented!!) + assertTrue(getterAnnotation?.mustBeDocumented!!) assertEquals(Annotations.AnnotationScope.DIRECT, getterAnnotation.scope) assertEquals(expectedAnnotation("asd"), setterAnnotation) - assertFalse(setterAnnotation?.mustBeDocumented!!) + assertTrue(setterAnnotation?.mustBeDocumented!!) assertEquals(Annotations.AnnotationScope.DIRECT, setterAnnotation.scope) } } diff --git a/plugins/base/src/test/kotlin/content/annotations/FileLevelJvmNameTest.kt b/plugins/base/src/test/kotlin/content/annotations/FileLevelJvmNameTest.kt index f2fd518c..ff54f8a7 100644 --- a/plugins/base/src/test/kotlin/content/annotations/FileLevelJvmNameTest.kt +++ b/plugins/base/src/test/kotlin/content/annotations/FileLevelJvmNameTest.kt @@ -74,7 +74,7 @@ class FileLevelJvmNameTest : BaseAbstractTest() { dri = DRI("kotlin.jvm", "JvmName"), params = mapOf("name" to StringValue("CustomJvmName")), scope = Annotations.AnnotationScope.FILE, - mustBeDocumented = false + mustBeDocumented = true ) val function = modules.flatMap { it.packages }.first().functions.first() val annotation = function.extra[Annotations]?.fileLevelAnnotations?.entries?.first()?.value?.single() @@ -97,7 +97,7 @@ class FileLevelJvmNameTest : BaseAbstractTest() { dri = DRI("kotlin.jvm", "JvmName"), params = mapOf("name" to StringValue("CustomJvmName")), scope = Annotations.AnnotationScope.FILE, - mustBeDocumented = false + mustBeDocumented = true ) val properties = modules.flatMap { it.packages }.first().properties.first() val annotation = properties.extra[Annotations]?.fileLevelAnnotations?.entries?.first()?.value?.single() diff --git a/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt index 3fb26034..c49e08f0 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.kotlinAsJava.transformers import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer @@ -25,7 +26,8 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { val name = jvmNameProvider.nameFor(this) copy( dri = documentable.dri.withCallableName(name), - name = name + name = name, + extra = extra.withoutJvmName() ) } is DProperty -> transformGetterAndSetter(this) @@ -42,6 +44,17 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { } } as T + private fun PropertyContainer.withoutJvmName(): PropertyContainer { + val annotationsWithoutJvmName = get(Annotations)?.let { annotations -> + annotations.copy((annotations.directAnnotations).map { (sourceset, annotations) -> + sourceset to annotations.filterNot { it.isJvmName() } + }.toMap() + annotations.fileLevelAnnotations) + } + val extraWithoutAnnotations: PropertyContainer = minus(Annotations) + + return extraWithoutAnnotations.addAll(listOfNotNull(annotationsWithoutJvmName)) + } + private fun transformClassLike(documentable: DClasslike): DClasslike = with(documentable) { when (this) { @@ -80,7 +93,8 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { setter?.let { setter -> setter.copy( dri = setter.dri.withCallableName(setterName), - name = setterName + name = setterName, + extra = setter.extra.withoutJvmName() ) } }, @@ -88,7 +102,8 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { getter?.let { getter -> getter.copy( dri = getter.dri.withCallableName(getterName), - name = getterName + name = getterName, + extra = getter.extra.withoutJvmName() ) } }) diff --git a/plugins/kotlin-as-java/src/test/kotlin/JvmNameTest.kt b/plugins/kotlin-as-java/src/test/kotlin/JvmNameTest.kt index 4c7e2445..1da2316c 100644 --- a/plugins/kotlin-as-java/src/test/kotlin/JvmNameTest.kt +++ b/plugins/kotlin-as-java/src/test/kotlin/JvmNameTest.kt @@ -4,8 +4,12 @@ import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest import org.jetbrains.dokka.links.Callable import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.links.TypeConstructor +import org.jetbrains.dokka.model.Annotations +import org.jetbrains.dokka.model.DClass +import org.jetbrains.dokka.model.isJvmName import org.junit.jupiter.api.Test import kotlin.test.assertEquals +import kotlin.test.assertNull class JvmNameTest : BaseAbstractTest() { val configuration = dokkaConfiguration { @@ -95,7 +99,8 @@ class JvmNameTest : BaseAbstractTest() { params = listOf(TypeConstructor("kotlin.String", emptyList())) ) ) - val function = module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first { it.name == "asd"} + val function = + module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first { it.name == "asd" } assertEquals(expectedSetterDri, function.dri) assertEquals("asd", function.name) } @@ -127,7 +132,8 @@ class JvmNameTest : BaseAbstractTest() { params = emptyList() ) ) - val function = module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first { it.name == "xd"} + val function = + module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first { it.name == "xd" } assertEquals(expectedGetterDri, function.dri) assertEquals("xd", function.name) } @@ -155,4 +161,26 @@ class JvmNameTest : BaseAbstractTest() { } } } + + @Test + fun `jvmName extra should be removed after the name swap`() { + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt + |package kotlinAsJavaPlugin + |@JvmName("CustomJvmName") + |fun sample(): String = "" + """.trimMargin(), + configuration, + ) { + documentablesTransformationStage = { module -> + val classLike = module.packages.flatMap { it.classlikes }.first() as DClass + assertNull( + classLike.extra[Annotations]?.directAnnotations?.flatMap { it.value } + ?.map { it.dri } + ?.firstOrNull { it.isJvmName() } + ) + } + } + } } \ No newline at end of file -- cgit