aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java/src/main/kotlin/transformers/JvmNameDocumentableTransformer.kt
blob: d937c8410c8f1a4b3bd2324fcff8ff777d81b704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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

class JvmNameDocumentableTransformer : DocumentableTransformer {
    private val jvmNameProvider = JvmNameProvider()

    override fun invoke(original: DModule, context: DokkaContext): DModule {
        return original.copy(packages = original.packages.map { transform(it, context) })
    }

    internal fun <T : Documentable> transform(documentable: T, context: DokkaContext): T {
        val transformResult = with(documentable) {
            when (this) {
                is DPackage -> copy(
                    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)
                    copy(
                        dri = documentable.dri.withCallableName(name),
                        name = name,
                        extra = extra.withoutJvmName()
                    )
                }
                is DProperty -> transformGetterAndSetter(this)
                is DClasslike -> transformClassLike(this, context)
                is DEnumEntry -> copy(
                    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}")
                    this
                }
            }
        }
        @Suppress("UNCHECKED_CAST")
        return transformResult as T
    }

    private fun PropertyContainer<DFunction>.withoutJvmName(): PropertyContainer<DFunction> {
        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<DFunction> = minus(Annotations)

        return extraWithoutAnnotations.addAll(listOfNotNull(annotationsWithoutJvmName))
    }

    private fun transformClassLike(documentable: DClasslike, context: DokkaContext): DClasslike =
        with(documentable) {
            when (this) {
                is DClass -> copy(
                    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, context) },
                    properties = properties.map { transform(it, context) },
                    classlikes = classlikes.map { transform(it, context) },
                )
                is DObject -> copy(
                    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, context) },
                    properties = properties.map { transform(it, context) },
                    classlikes = classlikes.map { transform(it, context) },
                )
                is DInterface -> copy(
                    functions = functions.map { transform(it, context) },
                    properties = properties.map { transform(it, context) },
                    classlikes = classlikes.map { transform(it, context) },
                )
            }
        }

    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,
                            extra = setter.extra.withoutJvmName()
                        )
                    }
                },
                getter = jvmNameProvider.nameForGetter(this)?.let { getterName ->
                    getter?.let { getter ->
                        getter.copy(
                            dri = getter.dri.withCallableName(getterName),
                            name = getterName,
                            extra = getter.extra.withoutJvmName()
                        )
                    }
                })
        }
}