aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java/src/main/kotlin/transformers
diff options
context:
space:
mode:
authorMarcin Aman <marcin.aman@gmail.com>2020-12-29 14:38:19 +0100
committerGitHub <noreply@github.com>2020-12-29 14:38:19 +0100
commitf5e7cffbebb66b989c64bdda61e1f7809ddc6068 (patch)
treeea0ef4b550f9a55681a915d5cb4d9f1beca22041 /plugins/kotlin-as-java/src/main/kotlin/transformers
parente55f3b015faec3f62e829a2aa5984e4bd6d5037e (diff)
downloaddokka-f5e7cffbebb66b989c64bdda61e1f7809ddc6068.tar.gz
dokka-f5e7cffbebb66b989c64bdda61e1f7809ddc6068.tar.bz2
dokka-f5e7cffbebb66b989c64bdda61e1f7809ddc6068.zip
Parsing of JvmName (#1675)
* Parsing of JvmName * Make JvmName processor run after KaJ
Diffstat (limited to 'plugins/kotlin-as-java/src/main/kotlin/transformers')
-rw-r--r--plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt96
-rw-r--r--plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameProvider.kt36
-rw-r--r--plugins/kotlin-as-java/src/main/kotlin/transformers/KotlinAsJavaDocumentableTransformer.kt2
-rw-r--r--plugins/kotlin-as-java/src/main/kotlin/transformers/withCallableName.kt5
4 files changed, 138 insertions, 1 deletions
diff --git a/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt
new file mode 100644
index 00000000..3fb26034
--- /dev/null
+++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt
@@ -0,0 +1,96 @@
+package org.jetbrains.dokka.kotlinAsJava.transformers
+
+import org.jetbrains.dokka.model.*
+import org.jetbrains.dokka.plugability.DokkaContext
+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) })
+ }
+
+ private fun <T : Documentable> transform(documentable: T): T =
+ with(documentable) {
+ when (this) {
+ is DPackage -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ is DFunction -> {
+ val name = jvmNameProvider.nameFor(this)
+ copy(
+ dri = documentable.dri.withCallableName(name),
+ name = name
+ )
+ }
+ is DProperty -> transformGetterAndSetter(this)
+ is DClasslike -> transformClassLike(this)
+ is DEnumEntry -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ else -> {
+ context.logger.warn("Failed to translate a JvmName for ${this.javaClass.canonicalName}")
+ this
+ }
+ }
+ } as T
+
+ private fun transformClassLike(documentable: DClasslike): DClasslike =
+ with(documentable) {
+ when (this) {
+ is DClass -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ is DAnnotation -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ is DObject -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ is DEnum -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ is DInterface -> copy(
+ functions = functions.map { transform(it) },
+ properties = properties.map { transform(it) },
+ classlikes = classlikes.map { transform(it) },
+ )
+ }
+ }
+
+ private fun transformGetterAndSetter(entry: DProperty): DProperty =
+ with(entry) {
+ copy(
+ setter = jvmNameProvider.nameForSetter(this)?.let { setterName ->
+ setter?.let { setter ->
+ setter.copy(
+ dri = setter.dri.withCallableName(setterName),
+ name = setterName
+ )
+ }
+ },
+ getter = jvmNameProvider.nameForGetter(this)?.let { getterName ->
+ getter?.let { getter ->
+ getter.copy(
+ dri = getter.dri.withCallableName(getterName),
+ name = getterName
+ )
+ }
+ })
+ }
+} \ 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
new file mode 100644
index 00000000..4e0ff7d7
--- /dev/null
+++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameProvider.kt
@@ -0,0 +1,36 @@
+package org.jetbrains.dokka.kotlinAsJava.transformers
+
+import org.jetbrains.dokka.kotlinAsJava.directlyAnnotatedJvmName
+import org.jetbrains.dokka.kotlinAsJava.jvmNameAsString
+import org.jetbrains.dokka.model.*
+import org.jetbrains.dokka.model.properties.WithExtraProperties
+import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
+
+data class Name(val fqName: String){
+ val name = fqName.substringAfterLast(".")
+}
+
+class JvmNameProvider {
+ fun <T> nameFor(entry: T): String where T : Documentable, T : WithExtraProperties<out Documentable> =
+ entry.directlyAnnotatedJvmName()?.jvmNameAsString()
+ ?: entry.name
+ ?: throw IllegalStateException("Failed to provide a name for ${entry.javaClass.canonicalName}")
+
+ fun <T> nameForSyntheticClass(entry: T): Name where T : WithSources, T : WithExtraProperties<out Documentable>, T: Documentable {
+ val name = entry.extra[Annotations]?.let {
+ it.fileLevelAnnotations.entries.firstNotNullResult { (_, annotations) ->
+ annotations.jvmNameAnnotation()?.jvmNameAsString()
+ }
+ } ?: entry.sources.entries.first().value.path.split("/").last().split(".").first().capitalize() + "Kt"
+ return Name("${entry.dri.packageName}.$name")
+ }
+
+ fun nameForGetter(entry: DProperty): String? =
+ entry.getter?.directlyAnnotatedJvmName()?.jvmNameAsString()
+
+ fun nameForSetter(entry: DProperty): String? =
+ entry.setter?.directlyAnnotatedJvmName()?.jvmNameAsString()
+
+ private fun List<Annotations.Annotation>.jvmNameAnnotation(): Annotations.Annotation? =
+ firstOrNull { it.isJvmName() }
+} \ No newline at end of file
diff --git a/plugins/kotlin-as-java/src/main/kotlin/transformers/KotlinAsJavaDocumentableTransformer.kt b/plugins/kotlin-as-java/src/main/kotlin/transformers/KotlinAsJavaDocumentableTransformer.kt
index 8b07670f..5916a11c 100644
--- a/plugins/kotlin-as-java/src/main/kotlin/transformers/KotlinAsJavaDocumentableTransformer.kt
+++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/KotlinAsJavaDocumentableTransformer.kt
@@ -8,4 +8,4 @@ import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer
class KotlinAsJavaDocumentableTransformer : DocumentableTransformer {
override fun invoke(original: DModule, context: DokkaContext): DModule =
original.copy(packages = original.packages.map { it.asJava() })
-}
+} \ No newline at end of file
diff --git a/plugins/kotlin-as-java/src/main/kotlin/transformers/withCallableName.kt b/plugins/kotlin-as-java/src/main/kotlin/transformers/withCallableName.kt
new file mode 100644
index 00000000..30f6a217
--- /dev/null
+++ b/plugins/kotlin-as-java/src/main/kotlin/transformers/withCallableName.kt
@@ -0,0 +1,5 @@
+package org.jetbrains.dokka.kotlinAsJava.transformers
+
+import org.jetbrains.dokka.links.DRI
+
+internal fun DRI.withCallableName(newName: String): DRI = copy(callable = callable?.copy(name = newName)) \ No newline at end of file