From f5b7797255576e5f1c230e2ca3fcb5f4e602387c Mon Sep 17 00:00:00 2001 From: Goooler Date: Mon, 21 Feb 2022 22:01:06 +0800 Subject: Code cleanups (#2165) --- .../src/main/kotlin/KotlinAsJavaPlugin.kt | 2 - .../kotlin/converters/KotlinToJavaConverter.kt | 55 ++++++++++++---------- .../main/kotlin/signatures/JavaSignatureUtils.kt | 4 +- .../main/kotlin/transformers/JvmNameProvider.kt | 2 +- .../KotlinAsJavaDocumentableToPageTranslator.kt | 11 +++-- .../kotlin/translators/KotlinAsJavaPageCreator.kt | 1 - 6 files changed, 38 insertions(+), 37 deletions(-) (limited to 'plugins/kotlin-as-java/src') diff --git a/plugins/kotlin-as-java/src/main/kotlin/KotlinAsJavaPlugin.kt b/plugins/kotlin-as-java/src/main/kotlin/KotlinAsJavaPlugin.kt index 531ff59f..aae6f570 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/KotlinAsJavaPlugin.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/KotlinAsJavaPlugin.kt @@ -2,13 +2,11 @@ package org.jetbrains.dokka.kotlinAsJava import org.jetbrains.dokka.CoreExtensions import org.jetbrains.dokka.base.DokkaBase -import org.jetbrains.dokka.base.DokkaBaseConfiguration import org.jetbrains.dokka.kotlinAsJava.signatures.JavaSignatureProvider import org.jetbrains.dokka.kotlinAsJava.transformers.JvmNameDocumentableTransformer import org.jetbrains.dokka.kotlinAsJava.transformers.KotlinAsJavaDocumentableTransformer import org.jetbrains.dokka.kotlinAsJava.translators.KotlinAsJavaDocumentableToPageTranslator import org.jetbrains.dokka.plugability.DokkaPlugin -import org.jetbrains.dokka.plugability.configuration class KotlinAsJavaPlugin : DokkaPlugin() { val kotlinAsJavaDocumentableTransformer by extending { diff --git a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt index 2a0762ba..1e3bd800 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt @@ -54,14 +54,14 @@ internal fun DPackage.asJava(): DPackage { classlikes = emptyList(), sources = emptyMap(), expectPresentInSet = null, - visibility = sourceSets.map { - it to JavaVisibility.Public - }.toMap(), + visibility = sourceSets.associateWith { + JavaVisibility.Public + }, companion = null, generics = emptyList(), supertypes = emptyMap(), documentation = emptyMap(), - modifier = sourceSets.map { it to JavaModifier.Final }.toMap(), + modifier = sourceSets.associateWith { JavaModifier.Final }, sourceSets = sourceSets, isExpectActual = false, extra = PropertyContainer.empty() @@ -98,9 +98,9 @@ internal fun DProperty.asJava(isTopLevel: Boolean = false, relocateToClass: Stri getter = null, // Removing getters and setters as they will be available as functions extra = if (isTopLevel) extra + extra.mergeAdditionalModifiers( - sourceSets.map { - it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) - }.toMap() + sourceSets.associateWith { + setOf(ExtraModifiers.JavaOnlyModifiers.Static) + } ) else extra ) @@ -138,9 +138,9 @@ internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClas type = getter.type.asJava(), extra = if (isTopLevel) getter.extra + getter.extra.mergeAdditionalModifiers( - sourceSets.map { - it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) - }.toMap() + sourceSets.associateWith { + setOf(ExtraModifiers.JavaOnlyModifiers.Static) + } ) else getter.extra ) @@ -167,9 +167,9 @@ internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClas visibility = visibility.mapValues { JavaVisibility.Public }, type = Void, extra = if (isTopLevel) setter.extra + setter.extra.mergeAdditionalModifiers( - sourceSets.map { - it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) - }.toMap() + sourceSets.associateWith { + setOf(ExtraModifiers.JavaOnlyModifiers.Static) + } ) else setter.extra ) @@ -187,16 +187,16 @@ private fun DFunction.asJava( name = newName, type = type.asJava(), modifier = if (modifier.all { (_, v) -> v is KotlinModifier.Final } && isConstructor) - sourceSets.map { it to JavaModifier.Empty }.toMap() - else sourceSets.map { it to modifier.values.first() }.toMap(), + sourceSets.associateWith { JavaModifier.Empty } + else sourceSets.associateWith { modifier.values.first() }, parameters = listOfNotNull(receiver?.asJava()) + parameters.map { it.asJava() }, visibility = visibility.map { (sourceSet, visibility) -> Pair(sourceSet, visibility.asJava()) }.toMap(), receiver = null, extra = if (isTopLevel) { extra + extra.mergeAdditionalModifiers( - sourceSets.map { - it to setOf(ExtraModifiers.JavaOnlyModifiers.Static) - }.toMap() + sourceSets.associateWith { + setOf(ExtraModifiers.JavaOnlyModifiers.Static) + } ) } else { extra @@ -253,7 +253,11 @@ internal fun DClasslike.asJava(): DClasslike = when (this) { internal fun DClass.asJava(): DClass = copy( constructors = constructors .filterNot { it.hasJvmSynthetic() } - .flatMap { it.asJava(dri.classNames ?: name) }, // name may not always be valid here, however classNames should always be not null + .flatMap { + it.asJava( + dri.classNames ?: name + ) + }, // name may not always be valid here, however classNames should always be not null functions = functionsInJava(), properties = properties .filterNot { it.hasJvmSynthetic() } @@ -261,9 +265,8 @@ internal fun DClass.asJava(): DClass = copy( classlikes = classlikes.map { it.asJava() }, generics = generics.map { it.asJava() }, supertypes = supertypes.mapValues { it.value.map { it.asJava() } }, - modifier = if (modifier.all { (_, v) -> v is KotlinModifier.Empty }) sourceSets.map { it to JavaModifier.Final } - .toMap() - else sourceSets.map { it to modifier.values.first() }.toMap() + modifier = if (modifier.all { (_, v) -> v is KotlinModifier.Empty }) sourceSets.associateWith { JavaModifier.Final } + else sourceSets.associateWith { modifier.values.first() } ) internal fun DClass.functionsInJava(): List = @@ -343,13 +346,13 @@ internal fun DObject.asJava(): DObject = copy( .map { it.asJava() } + DProperty( name = "INSTANCE", - modifier = sourceSets.map { it to JavaModifier.Final }.toMap(), + modifier = sourceSets.associateWith { JavaModifier.Final }, dri = dri.copy(callable = Callable("INSTANCE", null, emptyList())), documentation = emptyMap(), sources = emptyMap(), - visibility = sourceSets.map { - it to JavaVisibility.Public - }.toMap(), + visibility = sourceSets.associateWith { + JavaVisibility.Public + }, type = GenericTypeConstructor(dri, emptyList()), setter = null, getter = null, diff --git a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt index a50fdf68..61a31627 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureUtils.kt @@ -29,8 +29,8 @@ object JavaSignatureUtils : JvmSignatureUtils { annotationsInlineWithIgnored(d, ignoredAnnotations, strategy, listBrackets, classExtension) override fun WithExtraProperties.modifiers() = - extra[AdditionalModifiers]?.content?.entries?.map { + extra[AdditionalModifiers]?.content?.entries?.associate { it.key to it.value.filterIsInstance().toSet() - }?.toMap() ?: emptyMap() + } ?: emptyMap() } \ No newline at end of file diff --git a/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameProvider.kt b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameProvider.kt index 31252ae0..c928ea67 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameProvider.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameProvider.kt @@ -18,7 +18,7 @@ class JvmNameProvider { fun nameForSyntheticClass(entry: T): Name where T : WithSources, T : WithExtraProperties, T : Documentable { val name: String = (entry.fileLevelJvmName()?.params?.get("name") as? StringValue)?.value - ?: entry.sources.entries.first().value.path.split("/").last().split(".").first().capitalize() + "Kt" + ?: (entry.sources.entries.first().value.path.split("/").last().split(".").first().capitalize() + "Kt") return Name("${entry.dri.packageName}.$name") } diff --git a/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaDocumentableToPageTranslator.kt b/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaDocumentableToPageTranslator.kt index f1aae2dc..80c3a3de 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaDocumentableToPageTranslator.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaDocumentableToPageTranslator.kt @@ -2,13 +2,14 @@ package org.jetbrains.dokka.kotlinAsJava.translators import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.DokkaBaseConfiguration -import org.jetbrains.dokka.base.signatures.SignatureProvider -import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.transformers.documentation.DocumentableToPageTranslator import org.jetbrains.dokka.model.DModule import org.jetbrains.dokka.pages.ModulePageNode -import org.jetbrains.dokka.plugability.* +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.configuration +import org.jetbrains.dokka.plugability.plugin +import org.jetbrains.dokka.plugability.query +import org.jetbrains.dokka.plugability.querySingle +import org.jetbrains.dokka.transformers.documentation.DocumentableToPageTranslator import org.jetbrains.dokka.utilities.DokkaLogger class KotlinAsJavaDocumentableToPageTranslator(context: DokkaContext) : DocumentableToPageTranslator { diff --git a/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaPageCreator.kt b/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaPageCreator.kt index ef075153..ad3c8b0e 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaPageCreator.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/translators/KotlinAsJavaPageCreator.kt @@ -4,7 +4,6 @@ import org.jetbrains.dokka.base.DokkaBaseConfiguration import org.jetbrains.dokka.base.signatures.SignatureProvider import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter import org.jetbrains.dokka.base.translators.documentables.DefaultPageCreator -import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.base.transformers.pages.tags.CustomTagContentProvider import org.jetbrains.dokka.model.DProperty import org.jetbrains.dokka.pages.MemberPageNode -- cgit