aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Java/JavaPsiDocumentationBuilder.kt
blob: 94bb0455d31ec4cbb4b195a823b7cbabcc921c28 (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
package org.jetbrains.dokka

import com.google.inject.Inject
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.*
import com.intellij.psi.impl.JavaConstantExpressionEvaluator
import com.intellij.psi.impl.source.PsiClassReferenceType
import com.intellij.psi.util.InheritanceUtil
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.asJava.elements.KtLightDeclaration
import org.jetbrains.kotlin.asJava.elements.KtLightElement
import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtModifierListOwner
import java.io.File

fun getSignature(element: PsiElement?) = when(element) {
    is PsiPackage -> element.qualifiedName
    is PsiClass -> element.qualifiedName
    is PsiField -> element.containingClass!!.qualifiedName + "$" + element.name
    is PsiMethod ->
        element.containingClass?.qualifiedName + "$" + element.name + "(" +
                element.parameterList.parameters.map { it.type.typeSignature() }.joinToString(",") + ")"
    else -> null
}

private fun PsiType.typeSignature(): String = when(this) {
    is PsiArrayType -> "Array((${componentType.typeSignature()}))"
    is PsiPrimitiveType -> "kotlin." + canonicalText.capitalize()
    is PsiClassType -> resolve()?.qualifiedName ?: className
    else -> mapTypeName(this)
}

private fun mapTypeName(psiType: PsiType): String = when (psiType) {
    is PsiPrimitiveType -> psiType.canonicalText
    is PsiClassType -> psiType.resolve()?.name ?: psiType.className
    is PsiEllipsisType -> mapTypeName(psiType.componentType)
    is PsiArrayType -> "kotlin.Array"
    else -> psiType.canonicalText
}

interface JavaDocumentationBuilder {
    fun appendFile(file: PsiJavaFile, module: DocumentationModule, packageContent: Map<String, Content>)
}

class JavaPsiDocumentationBuilder : JavaDocumentationBuilder {
    private val options: DocumentationOptions
    private val refGraph: NodeReferenceGraph
    private val docParser: JavaDocumentationParser
    private val externalDocumentationLinkResolver: ExternalDocumentationLinkResolver

    @Inject constructor(
            options: DocumentationOptions,
            refGraph: NodeReferenceGraph,
            logger: DokkaLogger,
            signatureProvider: ElementSignatureProvider,
            externalDocumentationLinkResolver: ExternalDocumentationLinkResolver
    ) {
        this.options = options
        this.refGraph = refGraph
        this.docParser = JavadocParser(refGraph, logger, signatureProvider, externalDocumentationLinkResolver)
        this.externalDocumentationLinkResolver = externalDocumentationLinkResolver
    }

    constructor(
        options: DocumentationOptions,
        refGraph: NodeReferenceGraph,
        docParser: JavaDocumentationParser,
        externalDocumentationLinkResolver: ExternalDocumentationLinkResolver
    ) {
        this.options = options
        this.refGraph = refGraph
        this.docParser = docParser
        this.externalDocumentationLinkResolver = externalDocumentationLinkResolver
    }

    override fun appendFile(file: PsiJavaFile, module: DocumentationModule, packageContent: Map<String, Content>) {
        if (skipFile(file) || file.classes.all { skipElement(it) }) {
            return
        }
        val packageNode = findOrCreatePackageNode(module, file.packageName, emptyMap(), refGraph)
        appendClasses(packageNode, file.classes)
    }

    fun appendClasses(packageNode: DocumentationNode, classes: Array<PsiClass>) {
        packageNode.appendChildren(classes) { build() }
    }

    fun register(element: PsiElement, node: DocumentationNode) {
        val signature = getSignature(element)
        if (signature != null) {
            refGraph.register(signature, node)
        }
    }

    fun link(node: DocumentationNode, element: PsiElement?) {
        val qualifiedName = getSignature(element)
        if (qualifiedName != null) {
            refGraph.link(node, qualifiedName, RefKind.Link)
        }
    }

    fun link(element: PsiElement?, node: DocumentationNode, kind: RefKind) {
        val qualifiedName = getSignature(element)
        if (qualifiedName != null) {
            refGraph.link(qualifiedName, node, kind)
        }
    }

    fun nodeForElement(element: PsiNamedElement,
                       kind: NodeKind,
                       name: String = element.name ?: "<anonymous>"): DocumentationNode {
        val (docComment, deprecatedContent, attrs, apiLevel, sdkExtSince, deprecatedLevel, artifactId, attribute) = docParser.parseDocumentation(element)
        val node = DocumentationNode(name, docComment, kind)
        if (element is PsiModifierListOwner) {
            node.appendModifiers(element)
            val modifierList = element.modifierList
            if (modifierList != null) {
                modifierList.annotations.filter { !ignoreAnnotation(it) }.forEach {
                    val annotation = it.build()
                    if (it.qualifiedName == "java.lang.Deprecated" || it.qualifiedName == "kotlin.Deprecated") {
                        node.append(annotation, RefKind.Deprecation)
                        annotation.convertDeprecationDetailsToChildren()
                    } else {
                        node.append(annotation, RefKind.Annotation)
                    }
                }
            }
        }
        if (deprecatedContent != null) {
            val deprecationNode = DocumentationNode("", deprecatedContent, NodeKind.Modifier)
            node.append(deprecationNode, RefKind.Deprecation)
        }
        if (element is PsiDocCommentOwner && element.isDeprecated && node.deprecation == null) {
            val deprecationNode = DocumentationNode("", Content.of(ContentText("Deprecated")), NodeKind.Modifier)
            node.append(deprecationNode, RefKind.Deprecation)
        }
        apiLevel?.let {
            node.append(it, RefKind.Detail)
        }
        sdkExtSince?.let {
            node.append(it, RefKind.Detail)
        }
        deprecatedLevel?.let {
            node.append(it, RefKind.Detail)
        }
        artifactId?.let {
            node.append(it, RefKind.Detail)
        }
        attrs.forEach {
            refGraph.link(node, it, RefKind.Detail)
            refGraph.link(it, node, RefKind.Owner)
        }
        attribute?.let {
            val attrName = node.qualifiedName()
            refGraph.register("Attr:$attrName", attribute)
        }
        return node
    }

    fun ignoreAnnotation(annotation: PsiAnnotation) = when(annotation.qualifiedName) {
        "java.lang.SuppressWarnings" -> true
        else -> false
    }

    fun <T : Any> DocumentationNode.appendChildren(elements: Array<T>,
                                                   kind: RefKind = RefKind.Member,
                                                   buildFn: T.() -> DocumentationNode) {
        elements.forEach {
            if (!skipElement(it)) {
                append(it.buildFn(), kind)
            }
        }
    }

    private fun skipFile(javaFile: PsiJavaFile): Boolean = options.effectivePackageOptions(javaFile.packageName).suppress

    private fun skipElement(element: Any) =
            skipElementByVisibility(element) ||
                    hasSuppressDocTag(element) ||
                    hasHideAnnotation(element) ||
                    skipElementBySuppressedFiles(element)

    private fun skipElementByVisibility(element: Any): Boolean =
        element is PsiModifierListOwner &&
                element !is PsiParameter &&
                !(options.effectivePackageOptions((element.containingFile as? PsiJavaFile)?.packageName ?: "").includeNonPublic) &&
                (element.hasModifierProperty(PsiModifier.PRIVATE) ||
                        element.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) ||
                        element.isInternal())

    private fun skipElementBySuppressedFiles(element: Any): Boolean =
            element is PsiElement && element.containingFile.virtualFile != null && File(element.containingFile.virtualFile.path).absoluteFile in options.suppressedFiles

    private fun PsiElement.isInternal(): Boolean {
        val ktElement = (this as? KtLightElement<*, *>)?.kotlinOrigin ?: return false
        return (ktElement as? KtModifierListOwner)?.hasModifier(KtTokens.INTERNAL_KEYWORD) ?: false
    }

    fun <T : Any> DocumentationNode.appendMembers(elements: Array<T>, buildFn: T.() -> DocumentationNode) =
            appendChildren(elements, RefKind.Member, buildFn)

    fun <T : Any> DocumentationNode.appendDetails(elements: Array<T>, buildFn: T.() -> DocumentationNode) =
            appendChildren(elements, RefKind.Detail, buildFn)

    fun PsiClass.build(): DocumentationNode {
        val kind = when {
            isInterface -> NodeKind.Interface
            isEnum -> NodeKind.Enum
            isAnnotationType -> NodeKind.AnnotationClass
            isException() -> NodeKind.Exception
            else -> NodeKind.Class
        }
        val node = nodeForElement(this, kind)
        superTypes.filter { !ignoreSupertype(it) }.forEach { superType ->
            node.appendType(superType, NodeKind.Supertype)
            val superClass = superType.resolve()
            if (superClass != null) {
                link(superClass, node, RefKind.Inheritor)
            }
        }

        var methodsAndConstructors = methods

        if (constructors.isEmpty()) {
            // Having no constructor represents a class that only has an implicit/default constructor
            // so we create one synthetically for documentation
            val factory = JavaPsiFacade.getElementFactory(this.project)
            methodsAndConstructors += factory.createMethodFromText("public $name() {}", this)
        }
        node.appendDetails(typeParameters) { build() }
        node.appendMembers(methodsAndConstructors) { build() }
        node.appendMembers(fields) { build() }
        node.appendMembers(innerClasses) { build() }
        register(this, node)
        return node
    }

    fun PsiClass.isException() = InheritanceUtil.isInheritor(this, "java.lang.Throwable")

    fun ignoreSupertype(psiType: PsiClassType): Boolean = false
//            psiType.isClass("java.lang.Enum") || psiType.isClass("java.lang.Object")

    fun PsiClassType.isClass(qName: String): Boolean {
        val shortName = qName.substringAfterLast('.')
        if (className == shortName) {
            val psiClass = resolve()
            return psiClass?.qualifiedName == qName
        }
        return false
    }

    fun PsiField.build(): DocumentationNode {
        val node = nodeForElement(this, nodeKind())
        node.appendType(type)

        node.appendConstantValueIfAny(this)
        register(this, node)
        return node
    }

    private fun DocumentationNode.appendConstantValueIfAny(field: PsiField) {
        val modifierList = field.modifierList ?: return
        val initializer = field.initializer ?: return
        if (modifierList.hasExplicitModifier(PsiModifier.FINAL) &&
            modifierList.hasExplicitModifier(PsiModifier.STATIC)) {
            val value = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false) ?: return
            val text = when(value) {
                is String -> "\"${StringUtil.escapeStringCharacters(value)}\""
                else -> value.toString()
            }
            append(DocumentationNode(text, Content.Empty, NodeKind.Value), RefKind.Detail)
        }
    }

    private fun PsiField.nodeKind(): NodeKind = when {
        this is PsiEnumConstant -> NodeKind.EnumItem
        else -> NodeKind.Field
    }

    fun PsiMethod.build(): DocumentationNode {
        val node = nodeForElement(this, nodeKind(), name)

        if (!isConstructor) {
            node.appendType(returnType)
        }
        node.appendDetails(parameterList.parameters) { build() }
        node.appendDetails(typeParameters) { build() }
        register(this, node)
        return node
    }

    private fun PsiMethod.nodeKind(): NodeKind = when {
        isConstructor -> NodeKind.Constructor
        else -> NodeKind.Function
    }

    fun PsiParameter.build(): DocumentationNode {
        val node = nodeForElement(this, NodeKind.Parameter)
        node.appendType(type)
        if (type is PsiEllipsisType) {
            node.appendTextNode("vararg", NodeKind.Modifier, RefKind.Detail)
        }
        return node
    }

    fun PsiTypeParameter.build(): DocumentationNode {
        val node = nodeForElement(this, NodeKind.TypeParameter)
        extendsListTypes.forEach { node.appendType(it, NodeKind.UpperBound) }
        implementsListTypes.forEach { node.appendType(it, NodeKind.UpperBound) }
        return node
    }

    fun DocumentationNode.appendModifiers(element: PsiModifierListOwner) {
        val modifierList = element.modifierList ?: return

        PsiModifier.MODIFIERS.forEach {
            if (modifierList.hasExplicitModifier(it)) {
                appendTextNode(it, NodeKind.Modifier)
            }
        }
    }

    fun DocumentationNode.appendType(psiType: PsiType?, kind: NodeKind = NodeKind.Type) {
        if (psiType == null) {
            return
        }

        val node = psiType.build(kind)
        append(node, RefKind.Detail)

        // Attempt to create an external link if the psiType is one
        if (psiType is PsiClassReferenceType) {
            val target = psiType.reference.resolve()
            if (target != null) {
                val externalLink = externalDocumentationLinkResolver.buildExternalDocumentationLink(target)
                if (externalLink != null) {
                    node.append(DocumentationNode(externalLink, Content.Empty, NodeKind.ExternalLink), RefKind.Link)
                }
            }
        }
    }

    fun PsiType.build(kind: NodeKind = NodeKind.Type): DocumentationNode {
        val name = mapTypeName(this)
        val node = DocumentationNode(name, Content.Empty, kind)
        if (this is PsiClassType) {
            node.appendDetails(parameters) { build(NodeKind.Type) }
            link(node, resolve())
        }
        if (this is PsiArrayType && this !is PsiEllipsisType) {
            node.append(componentType.build(NodeKind.Type), RefKind.Detail)
        }
        return node
    }

    fun PsiAnnotation.build(): DocumentationNode {
        val node = DocumentationNode(nameReferenceElement?.text ?: "<?>", Content.Empty, NodeKind.Annotation)
        parameterList.attributes.forEach {
            val parameter = DocumentationNode(it.name ?: "value", Content.Empty, NodeKind.Parameter)
            val value = it.value
            if (value != null) {
                val valueText = (value as? PsiLiteralExpression)?.value as? String ?: value.text
                val valueNode = DocumentationNode(valueText, Content.Empty, NodeKind.Value)
                parameter.append(valueNode, RefKind.Detail)
            }
            node.append(parameter, RefKind.Detail)
        }
        return node
    }
}

fun hasSuppressDocTag(element: Any?): Boolean {
    val declaration = (element as? KtLightDeclaration<*, *>)?.kotlinOrigin as? KtDeclaration ?: return false
    return PsiTreeUtil.findChildrenOfType(declaration.docComment, KDocTag::class.java).any { it.knownTag == KDocKnownTag.SUPPRESS }
}

/**
 * Determines if the @hide annotation is present in a Javadoc comment.
 *
 * @param element a doc element to analyze for the presence of @hide
 *
 * @return true if @hide is present, otherwise false
 *
 * Note: this does not process @hide annotations in KDoc.  For KDoc, use the @suppress tag instead, which is processed
 * by [hasSuppressDocTag].
 */
fun hasHideAnnotation(element: Any?): Boolean {
    return element is PsiDocCommentOwner && element.docComment?.run { findTagByName("hide") != null } ?: false
}