From 558c58b13d0b05537d1986696fb778cf30888f4b Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak <32793002+BarkingBad@users.noreply.github.com> Date: Mon, 14 Feb 2022 16:57:07 +0100 Subject: Expose Kotlin-as-Java methods (#2351) Allow using these methods not only as part of transformers --- plugins/kotlin-as-java/api/kotlin-as-java.api | 9 ++++ .../src/main/kotlin/transformToJava.kt | 29 ++++++++++++ .../transformers/JvmNameDocumentableTransformer.kt | 54 +++++++++++----------- 3 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 plugins/kotlin-as-java/src/main/kotlin/transformToJava.kt (limited to 'plugins') diff --git a/plugins/kotlin-as-java/api/kotlin-as-java.api b/plugins/kotlin-as-java/api/kotlin-as-java.api index f941582e..159243ed 100644 --- a/plugins/kotlin-as-java/api/kotlin-as-java.api +++ b/plugins/kotlin-as-java/api/kotlin-as-java.api @@ -6,6 +6,15 @@ public final class org/jetbrains/dokka/kotlinAsJava/KotlinAsJavaPlugin : org/jet public final fun getKotlinAsJavaDocumentableTransformer ()Lorg/jetbrains/dokka/plugability/Extension; } +public final class org/jetbrains/dokka/kotlinAsJava/TransformToJavaKt { + public static final fun transformToJava (Lorg/jetbrains/dokka/model/DClasslike;Lorg/jetbrains/dokka/plugability/DokkaContext;)Lorg/jetbrains/dokka/model/DClasslike; + public static final fun transformToJava (Lorg/jetbrains/dokka/model/DFunction;Lorg/jetbrains/dokka/plugability/DokkaContext;Ljava/lang/String;Z)Ljava/util/List; + public static final fun transformToJava (Lorg/jetbrains/dokka/model/DPackage;Lorg/jetbrains/dokka/plugability/DokkaContext;)Lorg/jetbrains/dokka/model/DPackage; + public static final fun transformToJava (Lorg/jetbrains/dokka/model/DProperty;Lorg/jetbrains/dokka/plugability/DokkaContext;ZLjava/lang/String;)Lorg/jetbrains/dokka/model/DProperty; + public static synthetic fun transformToJava$default (Lorg/jetbrains/dokka/model/DFunction;Lorg/jetbrains/dokka/plugability/DokkaContext;Ljava/lang/String;ZILjava/lang/Object;)Ljava/util/List; + public static synthetic fun transformToJava$default (Lorg/jetbrains/dokka/model/DProperty;Lorg/jetbrains/dokka/plugability/DokkaContext;ZLjava/lang/String;ILjava/lang/Object;)Lorg/jetbrains/dokka/model/DProperty; +} + public final class org/jetbrains/dokka/kotlinAsJava/converters/KotlinToJavaConverterKt { public static final fun getJvmNameProvider ()Lorg/jetbrains/dokka/kotlinAsJava/transformers/JvmNameProvider; } diff --git a/plugins/kotlin-as-java/src/main/kotlin/transformToJava.kt b/plugins/kotlin-as-java/src/main/kotlin/transformToJava.kt new file mode 100644 index 00000000..69df21ee --- /dev/null +++ b/plugins/kotlin-as-java/src/main/kotlin/transformToJava.kt @@ -0,0 +1,29 @@ +package org.jetbrains.dokka.kotlinAsJava + +import org.jetbrains.dokka.kotlinAsJava.converters.asJava +import org.jetbrains.dokka.kotlinAsJava.transformers.JvmNameDocumentableTransformer +import org.jetbrains.dokka.model.DClasslike +import org.jetbrains.dokka.model.DFunction +import org.jetbrains.dokka.model.DPackage +import org.jetbrains.dokka.model.DProperty +import org.jetbrains.dokka.plugability.DokkaContext + +private val JVM_NAME_DOCUMENTABLE_TRANSFORMER by lazy { + JvmNameDocumentableTransformer() +} + +fun DPackage.transformToJava(context: DokkaContext): DPackage { + return JVM_NAME_DOCUMENTABLE_TRANSFORMER.transform(this.asJava(), context) +} + +fun DClasslike.transformToJava(context: DokkaContext): DClasslike { + return JVM_NAME_DOCUMENTABLE_TRANSFORMER.transform(this.asJava(), context) +} + +fun DFunction.transformToJava(context: DokkaContext, containingClassName: String, isTopLevel: Boolean = false): List { + return this.asJava(containingClassName, isTopLevel).map { JVM_NAME_DOCUMENTABLE_TRANSFORMER.transform(it, context) } +} + +fun DProperty.transformToJava(context: DokkaContext, isTopLevel: Boolean = false, relocateToClass: String? = null): DProperty { + return JVM_NAME_DOCUMENTABLE_TRANSFORMER.transform(this.asJava(isTopLevel, relocateToClass), context) +} 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 c49e08f0..28cb3453 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt @@ -7,20 +7,18 @@ import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer class JvmNameDocumentableTransformer : DocumentableTransformer { private val jvmNameProvider = JvmNameProvider() - private lateinit var context: DokkaContext override fun invoke(original: DModule, context: DokkaContext): DModule { - this.context = context - return original.copy(packages = original.packages.map { transform(it) }) + return original.copy(packages = original.packages.map { transform(it, context) }) } - private fun transform(documentable: T): T = + internal fun transform(documentable: T, context: DokkaContext): T = with(documentable) { when (this) { is DPackage -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) is DFunction -> { val name = jvmNameProvider.nameFor(this) @@ -31,11 +29,11 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { ) } is DProperty -> transformGetterAndSetter(this) - is DClasslike -> transformClassLike(this) + is DClasslike -> transformClassLike(this, context) is DEnumEntry -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) else -> { context.logger.warn("Failed to translate a JvmName for ${this.javaClass.canonicalName}") @@ -55,33 +53,33 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { return extraWithoutAnnotations.addAll(listOfNotNull(annotationsWithoutJvmName)) } - private fun transformClassLike(documentable: DClasslike): DClasslike = + private fun transformClassLike(documentable: DClasslike, context: DokkaContext): DClasslike = with(documentable) { when (this) { is DClass -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) is DAnnotation -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) is DObject -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) is DEnum -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) is DInterface -> copy( - functions = functions.map { transform(it) }, - properties = properties.map { transform(it) }, - classlikes = classlikes.map { transform(it) }, + functions = functions.map { transform(it, context) }, + properties = properties.map { transform(it, context) }, + classlikes = classlikes.map { transform(it, context) }, ) } } @@ -108,4 +106,4 @@ class JvmNameDocumentableTransformer : DocumentableTransformer { } }) } -} \ No newline at end of file +} -- cgit