-
Notifications
You must be signed in to change notification settings - Fork 392
/
PropertiesMergerTransformer.kt
118 lines (107 loc) · 5.1 KB
/
PropertiesMergerTransformer.kt
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
package org.jetbrains.dokka.base.transformers.documentables
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName
import org.jetbrains.kotlin.load.java.propertyNamesBySetMethodName
import org.jetbrains.kotlin.name.Name
/**
* This transformer is used to merge the backing fields and accessors (getters and setters)
* obtained from Java sources. This way, we could generate more coherent documentation,
* since the model is now aware of the relationship between accessors and the fields.
* This way if we generate Kotlin output we get rid of spare getters and setters,
* and from Kotlin-as-Java perspective we can collect accessors of each property.
*/
class PropertiesMergerTransformer : PreMergeDocumentableTransformer {
override fun invoke(modules: List<DModule>) =
modules.map { it.copy(packages = it.packages.map {
it.mergeAccessorsAndField().copy(
classlikes = it.classlikes.map { it.mergeAccessorsAndField() }
)
}) }
private fun <T : WithScope> T.mergeAccessorsAndField(): T {
val (functions, properties) = mergePotentialAccessorsAndField(this.functions, this.properties)
return when (this) {
is DClass -> {
this.copy(functions = functions, properties = properties)
}
is DEnum -> {
this.copy(functions = functions, properties = properties)
}
is DInterface -> {
this.copy(functions = functions, properties = properties)
}
is DObject -> {
this.copy(functions = functions, properties = properties)
}
is DAnnotation -> {
this.copy(functions = functions, properties = properties)
}
is DPackage -> {
this.copy(functions = functions, properties = properties)
}
else -> this
} as T
}
/**
* This is copied from here
* [org.jetbrains.dokka.base.translators.psi.DefaultPsiToDocumentableTranslator.DokkaPsiParser.getPropertyNameForFunction]
* we should consider if we could unify that.
* TODO: Revisit that
*/
private fun DFunction.getPropertyNameForFunction() =
when {
JvmAbi.isGetterName(name) -> propertyNameByGetMethodName(Name.identifier(name))?.asString()
JvmAbi.isSetterName(name) -> propertyNamesBySetMethodName(Name.identifier(name)).firstOrNull()
?.asString()
else -> null
}
/**
* This is loosely copied from here
* [org.jetbrains.dokka.base.translators.psi.DefaultPsiToDocumentableTranslator.DokkaPsiParser.splitFunctionsAndAccessors]
* we should consider if we could unify that.
* TODO: Revisit that
*/
private fun mergePotentialAccessorsAndField(
functions: List<DFunction>,
fields: List<DProperty>
): Pair<List<DFunction>, List<DProperty>> {
val fieldNames = fields.associateBy { it.name }
// Regular methods are methods that are not getters or setters
val regularMethods = mutableListOf<DFunction>()
// Accessors are methods that are getters or setters
val accessors = mutableMapOf<DProperty, MutableList<DFunction>>()
functions.forEach { method ->
val field = method.getPropertyNameForFunction()?.let { name -> fieldNames[name] }
if (field != null) {
accessors.getOrPut(field, ::mutableListOf).add(method)
} else {
regularMethods.add(method)
}
}
// Properties are triples of field and its getters and/or setters.
// They are wrapped up in DProperty class,
// so we copy accessors into its dedicated DProperty data class properties
val propertiesWithAccessors = accessors.map { (dProperty, dFunctions) ->
if (dProperty.visibility.values.all { it is KotlinVisibility.Private }) {
dFunctions.flatMap { it.visibility.values }.toSet().singleOrNull()?.takeIf {
it in listOf(KotlinVisibility.Public, KotlinVisibility.Protected)
}?.let { visibility ->
dProperty.copy(
getter = dFunctions.firstOrNull { it.type == dProperty.type },
setter = dFunctions.firstOrNull { it.parameters.isNotEmpty() },
visibility = dProperty.visibility.mapValues { visibility }
)
} ?: dProperty
} else {
dProperty
}
}
// The above logic is driven by accessors list
// Therefore, if there was no getter or setter, we missed processing the field itself.
// To include them, we collect all fields that have no accessors
val remainingFields = fields.toSet().minus(accessors.keys.toSet())
val allProperties = propertiesWithAccessors + remainingFields
return regularMethods.toList() to allProperties
}
}