aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt
blob: 309781aad556171293a0fd031d30ef8f18fd26d8 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package org.jetbrains.dokka.kotlinAsJava.converters

import org.jetbrains.dokka.kotlinAsJava.*
import org.jetbrains.dokka.kotlinAsJava.transformers.JvmNameProvider
import org.jetbrains.dokka.kotlinAsJava.transformers.withCallableName
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.withClass
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.properties.PropertyContainer
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.kotlin.analysis.kotlin.internal.InternalKotlinAnalysisPlugin

val jvmNameProvider = JvmNameProvider()
internal const val OBJECT_INSTANCE_NAME = "INSTANCE"

internal val DProperty.isConst: Boolean
    get() = hasModifier(ExtraModifiers.KotlinOnlyModifiers.Const)

internal val DProperty.isLateInit: Boolean
    get() = hasModifier(ExtraModifiers.KotlinOnlyModifiers.LateInit)

internal val DProperty.isJvmField: Boolean
    get() = jvmField() != null

internal val DFunction.isJvmStatic: Boolean
    get() = jvmStatic() != null

private fun DProperty.hasModifier(modifier: ExtraModifiers.KotlinOnlyModifiers): Boolean =
    extra[AdditionalModifiers]
        ?.content
        ?.any { (_, modifiers) -> modifier in modifiers } == true

class KotlinToJavaConverter(
    private val context: DokkaContext
) {
    private val kotlinToJavaMapper by lazy {
        context.plugin<InternalKotlinAnalysisPlugin>().querySingle { kotlinToJavaService }
    }

    internal fun DPackage.asJava(): DPackage {
        val syntheticClasses =
            (properties.map { jvmNameProvider.nameForSyntheticClass(it) to it }
                    + functions.map { jvmNameProvider.nameForSyntheticClass(it) to it })
                .groupBy({ it.first }) { it.second }
                .map { (syntheticClassName, nodes) ->
                    DClass(
                        dri = dri.withClass(syntheticClassName.name),
                        name = syntheticClassName.name,
                        properties = nodes
                            .filterIsInstance<DProperty>()
                            .filterNot { it.hasJvmSynthetic() }
                            .map { it.asJava(true) },
                        constructors = emptyList(),
                        functions = (
                                nodes
                                    .filterIsInstance<DProperty>()
                                    .filterNot { it.isConst || it.isJvmField || it.hasJvmSynthetic() }
                                    .flatMap { it.javaAccessors(relocateToClass = syntheticClassName.name) } +
                                        nodes
                                            .filterIsInstance<DFunction>()
                                            .flatMap { it.asJava(syntheticClassName.name, true) })
                            .filterNot { it.hasJvmSynthetic() },
                        classlikes = emptyList(),
                        sources = emptyMap(),
                        expectPresentInSet = null,
                        visibility = sourceSets.associateWith {
                            JavaVisibility.Public
                        },
                        companion = null,
                        generics = emptyList(),
                        supertypes = emptyMap(),
                        documentation = emptyMap(),
                        modifier = sourceSets.associateWith { JavaModifier.Final },
                        sourceSets = sourceSets,
                        isExpectActual = false,
                        extra = PropertyContainer.empty()
                    )
                }

        return copy(
            functions = emptyList(),
            properties = emptyList(),
            classlikes = classlikes.map { it.asJava() } + syntheticClasses,
            typealiases = emptyList()
        )
    }

    internal fun DProperty.asJava(
        isTopLevel: Boolean = false,
        relocateToClass: String? = null,
        isFromObjectOrCompanion: Boolean = false
    ) =
        copy(
            dri = if (relocateToClass.isNullOrBlank()) {
                dri
            } else {
                dri.withClass(relocateToClass)
            },
            modifier = javaModifierFromSetter(),
            visibility = visibility.mapValues {
                if (isConst || isJvmField || (getter == null && setter == null) || (isFromObjectOrCompanion && isLateInit)) {
                    it.value.asJava()
                } else {
                    it.value.propertyVisibilityAsJava()
                }
            },
            type = type.asJava(), // TODO: check
            setter = null,
            getter = null, // Removing getters and setters as they will be available as functions
            extra = if (isTopLevel || isConst || (isFromObjectOrCompanion && isJvmField) || (isFromObjectOrCompanion && isLateInit))
                extra + extra.mergeAdditionalModifiers(
                    sourceSets.associateWith {
                        setOf(ExtraModifiers.JavaOnlyModifiers.Static)
                    }
                )
            else extra
        )

    internal fun Visibility.asJava() =
        when (this) {
            is JavaVisibility -> this
            is KotlinVisibility.Public, KotlinVisibility.Internal -> JavaVisibility.Public
            is KotlinVisibility.Private -> JavaVisibility.Private
            is KotlinVisibility.Protected -> JavaVisibility.Protected
        }

    internal fun DProperty.javaModifierFromSetter() =
        modifier.mapValues {
            when {
                it.value is JavaModifier -> it.value
                setter == null -> JavaModifier.Final
                else -> JavaModifier.Empty
            }
        }

    internal fun DProperty.javaAccessors(
        isTopLevel: Boolean = false,
        relocateToClass: String? = null
    ): List<DFunction> =
        listOfNotNull(
            getter?.let { getter ->
                val name = "get" + name.capitalize()
                getter.copy(
                    dri = if (relocateToClass.isNullOrBlank()) {
                        getter.dri
                    } else {
                        getter.dri.withClass(relocateToClass)
                    }.withCallableName(name),
                    name = name,
                    modifier = javaModifierFromSetter(),
                    visibility = visibility.mapValues { JavaVisibility.Public },
                    type = getter.type.asJava(),
                    extra = if (isTopLevel) getter.extra +
                            getter.extra.mergeAdditionalModifiers(
                                sourceSets.associateWith {
                                    setOf(ExtraModifiers.JavaOnlyModifiers.Static)
                                }
                            )
                    else getter.extra
                )
            },
            setter?.let { setter ->
                val name = "set" + name.capitalize()
                val baseDRI = (if (relocateToClass.isNullOrBlank()) {
                    setter.dri
                } else {
                    setter.dri.withClass(relocateToClass)
                }).withCallableName(name)
                setter.copy(
                    dri = baseDRI,
                    name = name,
                    parameters = setter.parameters.map {
                        it.copy(
                            dri = baseDRI.copy(
                                target = it.dri.target,
                                extra = it.dri.extra
                            ), type = it.type.asJava()
                        )
                    },
                    modifier = javaModifierFromSetter(),
                    visibility = visibility.mapValues { JavaVisibility.Public },
                    type = Void,
                    extra = if (isTopLevel) setter.extra + setter.extra.mergeAdditionalModifiers(
                        sourceSets.associateWith {
                            setOf(ExtraModifiers.JavaOnlyModifiers.Static)
                        }
                    )
                    else setter.extra
                )
            }
        )

    private fun DFunction.asJava(
        containingClassName: String,
        newName: String,
        parameters: List<DParameter>,
        isTopLevel: Boolean = false
    ): DFunction {
        return copy(
            dri = dri.copy(classNames = containingClassName, callable = dri.callable?.copy(name = newName)),
            name = newName,
            type = type.asJava(),
            modifier = if (modifier.all { (_, v) -> v is KotlinModifier.Final } && isConstructor)
                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 || isJvmStatic) {
                extra + extra.mergeAdditionalModifiers(
                    sourceSets.associateWith {
                        setOf(ExtraModifiers.JavaOnlyModifiers.Static)
                    }
                )
            } else {
                extra
            }
        )
    }

    private fun DFunction.withJvmOverloads(
        containingClassName: String,
        newName: String,
        isTopLevel: Boolean = false
    ): List<DFunction>? {
        val (paramsWithDefaults, paramsWithoutDefaults) = parameters
            .withIndex()
            .partition { (_, p) -> p.extra[DefaultValue] != null }
        return paramsWithDefaults
            .runningFold(paramsWithoutDefaults) { acc, param -> (acc + param) }
            .map { params ->
                asJava(
                    containingClassName,
                    newName,
                    params
                        .sortedBy(IndexedValue<DParameter>::index)
                        .map { it.value },
                    isTopLevel
                )
            }
            .reversed()
            .takeIf { it.isNotEmpty() }
    }

    internal fun DFunction.asJava(containingClassName: String, isTopLevel: Boolean = false): List<DFunction> {
        val newName = when {
            isConstructor -> containingClassName
            else -> name
        }
        val baseFunction = asJava(containingClassName, newName, parameters, isTopLevel)
        return if (hasJvmOverloads()) {
            withJvmOverloads(containingClassName, newName, isTopLevel) ?: listOf(baseFunction)
        } else {
            listOf(baseFunction)
        }
    }

    internal fun DClasslike.asJava(): DClasslike = when (this) {
        is DClass -> asJava()
        is DEnum -> asJava()
        is DAnnotation -> asJava()
        is DObject -> asJava()
        is DInterface -> asJava()
        else -> throw IllegalArgumentException("$this shouldn't be here")
    }

    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
        functions = functionsInJava(),
        properties = propertiesInJava(),
        classlikes = classlikesInJava(),
        generics = generics.map { it.asJava() },
        companion = companion?.companionAsJava(),
        supertypes = supertypes.mapValues { it.value.map { it.asJava() } },
        modifier = if (modifier.all { (_, v) -> v is KotlinModifier.Empty }) sourceSets.associateWith { JavaModifier.Final }
        else sourceSets.associateWith { modifier.values.first() }
    )

    /**
     * Companion objects requires some custom logic for rendering as Java.
     * They are excluded from usual classlikes rendering and added after.
     */
    internal fun DClass.classlikesInJava(): List<DClasslike> {
        val classlikes = classlikes
            .filter { it.name != companion?.name }
            .map { it.asJava() }

        val companionAsJava = companion?.companionAsJava()
        return if (companionAsJava != null) classlikes.plus(companionAsJava) else classlikes
    }


    internal fun DClass.functionsInJava(): List<DFunction> =
        properties
            .filter { !it.isJvmField && !it.hasJvmSynthetic() }
            .flatMap { property -> listOfNotNull(property.getter, property.setter) }
            .plus(functions)
            .plus(companion.staticFunctionsForJava())
            .filterNot { it.hasJvmSynthetic() }
            .flatMap { it.asJava(it.dri.classNames ?: it.name) }

    internal fun DClass.propertiesInJava(): List<DProperty> {
        val propertiesFromCompanion = companion
            .staticPropertiesForJava()
            .filterNot { it.hasJvmSynthetic() }
            .map { it.asJava(isFromObjectOrCompanion = true) }
        val companionInstanceProperty = companion?.companionInstancePropertyForJava()
        val ownProperties = properties
            .filterNot { it.hasJvmSynthetic() }
            .map { it.asJava() }

        return propertiesFromCompanion + ownProperties + listOfNotNull(companionInstanceProperty)
    }

    private fun DTypeParameter.asJava(): DTypeParameter = copy(
        variantTypeParameter = variantTypeParameter.withDri(dri.possiblyAsJava()),
        bounds = bounds.map { it.asJava() }
    )

    private fun Projection.asJava(): Projection = when (this) {
        is Star -> Star
        is Covariance<*> -> copy(inner.asJava())
        is Contravariance<*> -> copy(inner.asJava())
        is Invariance<*> -> copy(inner.asJava())
        is Bound -> asJava()
    }

    private fun Bound.asJava(): Bound = when (this) {
        is TypeParameter -> copy(dri.possiblyAsJava())
        is GenericTypeConstructor -> copy(
            dri = dri.possiblyAsJava(),
            projections = projections.map { it.asJava() }
        )

        is FunctionalTypeConstructor -> copy(
            dri = dri.possiblyAsJava(),
            projections = projections.map { it.asJava() }
        )

        is TypeAliased -> copy(
            typeAlias = typeAlias.asJava(),
            inner = inner.asJava()
        )

        is Nullable -> copy(inner.asJava())
        is DefinitelyNonNullable -> copy(inner.asJava())
        is PrimitiveJavaType -> this
        is Void -> this
        is JavaObject -> this
        is Dynamic -> this
        is UnresolvedBound -> this
    }

    internal fun DEnum.asJava(): DEnum = copy(
        constructors = constructors.flatMap { it.asJava(dri.classNames ?: name) },
        functions = functions
            .plus(
                properties
                    .filter { !it.isJvmField && !it.hasJvmSynthetic() }
                    .flatMap { listOf(it.getter, it.setter) }
            )
            .filterNotNull()
            .filterNot { it.hasJvmSynthetic() }
            .flatMap { it.asJava(dri.classNames ?: name) },
        properties = properties
            .filterNot { it.hasJvmSynthetic() }
            .map { it.asJava() },
        classlikes = classlikes.map { it.asJava() },
        supertypes = supertypes.mapValues { it.value.map { it.asJava() } }
//    , entries = entries.map { it.asJava() }
    )

    /**
     * Parameters [excludedProps] and [excludedFunctions] used for rendering companion objects
     * where some members (that lifted to outer class) are not rendered
     */
    internal fun DObject.asJava(
        excludedProps: List<DProperty> = emptyList(),
        excludedFunctions: List<DFunction> = emptyList()
    ): DObject = copy(
        functions = functions
            .plus(
                properties
                    .filterNot { it in excludedProps }
                    .filter { !it.isJvmField && !it.isConst && !it.isLateInit && !it.hasJvmSynthetic() }
                    .flatMap { listOf(it.getter, it.setter) }
            )
            .filterNotNull()
            .filterNot { it in excludedFunctions }
            .filterNot { it.hasJvmSynthetic() }
            .flatMap { it.asJava(dri.classNames ?: name.orEmpty()) },
        properties = properties
            .filterNot { it.hasJvmSynthetic() }
            .filterNot { it in excludedProps }
            .map { it.asJava(isFromObjectOrCompanion = true) } +
                DProperty(
                    name = OBJECT_INSTANCE_NAME,
                    modifier = sourceSets.associateWith { JavaModifier.Final },
                    dri = dri.copy(callable = Callable(OBJECT_INSTANCE_NAME, null, emptyList())),
                    documentation = emptyMap(),
                    sources = emptyMap(),
                    visibility = sourceSets.associateWith {
                        JavaVisibility.Public
                    },
                    type = GenericTypeConstructor(dri, emptyList()),
                    setter = null,
                    getter = null,
                    sourceSets = sourceSets,
                    receiver = null,
                    generics = emptyList(),
                    expectPresentInSet = expectPresentInSet,
                    isExpectActual = false,
                    extra = PropertyContainer.withAll(sourceSets.map {
                        mapOf(it to setOf(ExtraModifiers.JavaOnlyModifiers.Static)).toAdditionalModifiers()
                    })
                ),
        classlikes = classlikes.map { it.asJava() },
        supertypes = supertypes.mapValues { it.value.map { it.asJava() } }
    )

    internal fun DInterface.asJava(): DInterface = copy(
        functions = functions
            .plus(
                properties
                    .filter { it.jvmField() == null && !it.hasJvmSynthetic() }
                    .flatMap { listOf(it.getter, it.setter) }
            )
            .filterNotNull()
            .filterNot { it.hasJvmSynthetic() }
            .flatMap { it.asJava(dri.classNames ?: name) },
        properties = emptyList(),
        classlikes = classlikes.map { it.asJava() }, // TODO: public static final class DefaultImpls with impls for methods
        generics = generics.map { it.asJava() },
        supertypes = supertypes.mapValues { it.value.map { it.asJava() } }
    )

    internal fun DAnnotation.asJava(): DAnnotation = copy(
        properties = properties.map { it.asJava() },
        constructors = emptyList(),
        classlikes = classlikes.map { it.asJava() }
    ) // TODO investigate if annotation class can have methods and properties not from constructor

    internal fun DParameter.asJava(): DParameter = copy(
        type = type.asJava(),
        name = if (name.isNullOrBlank()) "\$self" else name
    )

    internal fun Visibility.propertyVisibilityAsJava(): Visibility =
        if (this is JavaVisibility) this
        else JavaVisibility.Private

    private fun TypeConstructor.possiblyAsJava(): TypeConstructor = when (this) {
        is GenericTypeConstructor -> copy(dri = this.dri.possiblyAsJava())
        is FunctionalTypeConstructor -> copy(dri = this.dri.possiblyAsJava())
    }


    internal fun TypeConstructorWithKind.asJava(): TypeConstructorWithKind =
        TypeConstructorWithKind(
            typeConstructor = typeConstructor.possiblyAsJava(),
            kind = kind.asJava()
        )

    internal fun ClassKind.asJava(): ClassKind {
        return when (this) {
            is JavaClassKindTypes -> this
            KotlinClassKindTypes.CLASS -> JavaClassKindTypes.CLASS
            KotlinClassKindTypes.INTERFACE -> JavaClassKindTypes.INTERFACE
            KotlinClassKindTypes.ENUM_CLASS -> JavaClassKindTypes.ENUM_CLASS
            KotlinClassKindTypes.ENUM_ENTRY -> JavaClassKindTypes.ENUM_ENTRY
            KotlinClassKindTypes.ANNOTATION_CLASS -> JavaClassKindTypes.ANNOTATION_CLASS
            KotlinClassKindTypes.OBJECT -> JavaClassKindTypes.CLASS
            else -> throw IllegalStateException("Non exchaustive match while trying to convert $this to Java")
        }
    }

    private fun <T : Documentable> PropertyContainer<T>.mergeAdditionalModifiers(second: SourceSetDependent<Set<ExtraModifiers>>) =
        this[AdditionalModifiers]?.squash(AdditionalModifiers(second)) ?: AdditionalModifiers(second)

    private fun AdditionalModifiers.squash(second: AdditionalModifiers) =
        AdditionalModifiers(content + second.content)

    internal fun DObject.companionAsJava(): DObject? {
        if (hasNothingToRender()) return null

        return asJava(
            excludedProps = staticPropertiesForJava(),
            excludedFunctions = staticFunctionsForJava()
        )
    }

    private fun DRI.possiblyAsJava(): DRI {
        return kotlinToJavaMapper.findAsJava(this) ?: this
    }
}