-
Notifications
You must be signed in to change notification settings - Fork 391
/
ClassGraphBuilder.kt
66 lines (60 loc) · 2.71 KB
/
ClassGraphBuilder.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
package org.jetbrains.dokka.base.transformers.documentables.utils
import kotlinx.coroutines.*
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.analysis.DescriptorDocumentableSource
import org.jetbrains.dokka.analysis.from
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.utilities.parallelForEach
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
import java.util.concurrent.ConcurrentHashMap
/**
* The class allows lo build a full class hierarchy via descriptors
*/
class ClassGraphBuilder {
suspend operator fun invoke(original: DModule,): SourceSetDependent<Map<DRI, List<DRI>>> = coroutineScope{
val map = original.sourceSets.associateWith { ConcurrentHashMap<DRI, List<DRI>>() }
original.packages.parallelForEach{ visitDocumentable(it, map) }
map
}
private suspend fun collectSupertypesFromKotlinType(
DRIWithKType: Pair<DRI, KotlinType>,
sourceSet: DokkaConfiguration.DokkaSourceSet,
supersMap: SourceSetDependent<MutableMap<DRI, List<DRI>>>
): Unit = coroutineScope {
val supertypes = DRIWithKType.second.immediateSupertypes().filterNot { it.isAnyOrNullableAny() }
val supertypesDRIWithKType = supertypes.mapNotNull { supertype ->
supertype.constructor.declarationDescriptor?.let {
DRI.from(it) to supertype
}
}
supersMap[sourceSet]?.let {
if (it[DRIWithKType.first] == null) {
// another thread can rewrite the same value, but it isn't a problem
it[DRIWithKType.first] = supertypesDRIWithKType.map { it.first }
supertypesDRIWithKType.parallelForEach { collectSupertypesFromKotlinType(it, sourceSet, supersMap) }
}
}
}
private suspend fun visitDocumentable(
documentable: Documentable,
supersMap: SourceSetDependent<MutableMap<DRI, List<DRI>>>
): Unit = coroutineScope {
if (documentable is WithScope) {
documentable.classlikes.
parallelForEach{ visitDocumentable(it, supersMap) }
}
if(documentable is DClasslike) {
documentable.sources.forEach { (sourceSet, source) ->
if (source is DescriptorDocumentableSource) {
val descriptor = source.descriptor as ClassDescriptor
val type = descriptor.defaultType
collectSupertypesFromKotlinType( documentable.dri to type, sourceSet, supersMap)
}
}
}
}
}