Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix java translation #1674

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation("org.jsoup:jsoup:1.12.1")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.1")
implementation("com.google.guava:guava:30.0-jre")

val coroutines_version: String by project
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version")
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/model/Documentable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ fun <T> SourceSetDependent<T>?.orEmpty(): SourceSetDependent<T> = this ?: emptyM

interface DocumentableSource {
val path: String

fun hasSamePath(other: DocumentableSource?) = path == other?.path
}

data class TypeConstructorWithKind(val typeConstructor: TypeConstructor, val kind: ClassKind)
57 changes: 56 additions & 1 deletion core/src/main/kotlin/model/additionalExtras.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jetbrains.dokka.model

import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.Multimap
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.properties.ExtraProperty
import org.jetbrains.dokka.model.properties.MergeStrategy
import org.jetbrains.dokka.model.properties.WithExtraProperties

class AdditionalModifiers(val content: SourceSetDependent<Set<ExtraModifiers>>) : ExtraProperty<Documentable> {
companion object : ExtraProperty.Key<Documentable, AdditionalModifiers> {
Expand All @@ -21,6 +24,12 @@ class AdditionalModifiers(val content: SourceSetDependent<Set<ExtraModifiers>>)

fun SourceSetDependent<Set<ExtraModifiers>>.toAdditionalModifiers() = AdditionalModifiers(this)

fun WithExtraProperties<out Documentable>.hasAdditionalModifier(modifier: ExtraModifiers): SourceSetDependent<Boolean>? {
return extra[AdditionalModifiers]?.content?.mapValues { (_, modifiers) ->
modifier in modifiers
}
}

class Annotations(val content: SourceSetDependent<List<Annotation>>) : ExtraProperty<Documentable> {
companion object : ExtraProperty.Key<Documentable, Annotations> {
override fun mergeStrategyFor(left: Annotations, right: Annotations): MergeStrategy<Documentable> =
Expand All @@ -41,12 +50,25 @@ class Annotations(val content: SourceSetDependent<List<Annotation>>) : ExtraProp

fun SourceSetDependent<List<Annotations.Annotation>>.toAnnotations() = Annotations(this)

fun WithExtraProperties<out Documentable>.hasAnnotation(name: String): SourceSetDependent<Boolean>? {
return extra[Annotations]?.content?.mapValues { (_, annotations) ->
annotations.any { annotation -> annotation.dri.classNames == name }
}
}

sealed class AnnotationParameterValue
data class AnnotationValue(val annotation: Annotations.Annotation) : AnnotationParameterValue()
data class ArrayValue(val value: List<AnnotationParameterValue>) : AnnotationParameterValue()
data class EnumValue(val enumName: String, val enumDri: DRI) : AnnotationParameterValue()
data class ClassValue(val className: String, val classDRI: DRI) : AnnotationParameterValue()
data class StringValue(val value: String) : AnnotationParameterValue()
data class StringValue(val value: String) : AnnotationParameterValue() {
val unquotedValue: String
get() = if (value.startsWith('"') && value.endsWith('"')) {
if (value.length == 2) "" else value.substring(1, value.lastIndex)
} else {
value
}
}


object PrimaryConstructorExtra : ExtraProperty<DFunction>, ExtraProperty.Key<DFunction, PrimaryConstructorExtra> {
Expand All @@ -72,4 +94,37 @@ data class ConstructorValues(val values: SourceSetDependent<List<String>>) : Ext
}

override val key: ExtraProperty.Key<DEnumEntry, ConstructorValues> = ConstructorValues
}

fun SourceSetDependent<Multimap<DocumentableSource, Annotations.Annotation>>.toFileAnnotations() =
FileAnnotations(this)

data class FileAnnotations(val content: SourceSetDependent<Multimap<DocumentableSource, Annotations.Annotation>>) : ExtraProperty<DPackage> {
companion object : ExtraProperty.Key<DPackage, FileAnnotations> {
override fun mergeStrategyFor(left: FileAnnotations, right: FileAnnotations): MergeStrategy<DPackage> {
val content = left.content.toMutableMap()
right.content.forEach { (sourceSet, fileAnnotations) ->
val existing = content[sourceSet]
if (existing == null) {
content[sourceSet] = fileAnnotations
} else {
content[sourceSet] = existing + fileAnnotations
}
}
return MergeStrategy.Replace(FileAnnotations(content))
}
}

override val key: ExtraProperty.Key<DPackage, *> = FileAnnotations
}

private operator fun <K, V> Multimap<out K, V>.plus(multimap: Multimap<K, V>): Multimap<K, V> =
ArrayListMultimap.create(this).apply { putAll(multimap) }

data class FromCompanionObject(val container: WithCompanion) : ExtraProperty<Documentable> {
companion object : ExtraProperty.Key<Documentable, FromCompanionObject> {
override fun mergeStrategyFor(left: FromCompanionObject,right: FromCompanionObject) = MergeStrategy.Remove
}

override val key: ExtraProperty.Key<Documentable, *> = FromCompanionObject
}
4 changes: 4 additions & 0 deletions core/src/main/kotlin/model/defaultValues.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.dokka.model

import org.jetbrains.dokka.model.properties.ExtraProperty
import org.jetbrains.dokka.model.properties.MergeStrategy
import org.jetbrains.dokka.model.properties.WithExtraProperties

class DefaultValue(val value: String): ExtraProperty<Documentable> {
companion object : ExtraProperty.Key<Documentable, DefaultValue> {
Expand All @@ -11,3 +12,6 @@ class DefaultValue(val value: String): ExtraProperty<Documentable> {
override val key: ExtraProperty.Key<Documentable, *>
get() = Companion
}

val WithExtraProperties<out Documentable>.hasDefaultValue: Boolean
get() = extra[DefaultValue] != null
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package org.jetbrains.dokka.analysis
import com.intellij.psi.PsiNamedElement
import org.jetbrains.dokka.model.DocumentableSource
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.load.kotlin.toSourceElement

class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor) : DocumentableSource {
override val path = descriptor.toSourceElement.containingFile.toString()
}

class AnnotationDescriptorDocumentableSource(val descriptor: AnnotationDescriptor) : DocumentableSource {
override val path = descriptor.source.containingFile.toString()
}

class PsiDocumentableSource(val psi: PsiNamedElement) : DocumentableSource {
override val path = psi.containingFile.virtualFile.path
}
1 change: 1 addition & 0 deletions plugins/base/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
api(project(":kotlin-analysis"))
implementation("org.jsoup:jsoup:1.12.1")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1")
implementation("com.google.guava:guava:30.0-jre")
testImplementation(project(":plugins:base:base-test-utils"))
testImplementation(project(":core:content-matcher-test-utils"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jetbrains.dokka.base.translators.descriptors

import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.Multimap
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet
import org.jetbrains.dokka.analysis.AnnotationDescriptorDocumentableSource
import org.jetbrains.dokka.analysis.DescriptorDocumentableSource
import org.jetbrains.dokka.analysis.DokkaResolutionFacade
import org.jetbrains.dokka.analysis.KotlinAnalysis
Expand Down Expand Up @@ -75,14 +78,22 @@ class DefaultDescriptorToDocumentableTranslator(
val packageFragments = environment.getSourceFiles().asSequence()
.map { it.packageFqName }
.distinct()
.distinct()
.mapNotNull { facade.resolveSession.getPackageFragment(it) }
.toList()
val fileAnnotations = ArrayListMultimap.create<FqName, AnnotationDescriptor>()
environment
.getSourceFiles()
.forEach { sourceFile ->
fileAnnotations.putAll(sourceFile.packageFqName, facade.resolveSession.getFileAnnotations(sourceFile))
}

return DokkaDescriptorVisitor(sourceSet, kotlinAnalysis[sourceSet].facade, context.logger).run {
packageFragments.mapNotNull { it.safeAs<PackageFragmentDescriptor>() }.parallelMap {
visitPackageFragmentDescriptor(
it,
DRIWithPlatformInfo(DRI.topLevel, emptyMap())
DRIWithPlatformInfo(DRI.topLevel, emptyMap()),
fileAnnotations[it.fqName]
)
}
}.let {
Expand Down Expand Up @@ -121,7 +132,8 @@ private class DokkaDescriptorVisitor(

suspend fun visitPackageFragmentDescriptor(
descriptor: PackageFragmentDescriptor,
parent: DRIWithPlatformInfo
parent: DRIWithPlatformInfo,
fileAnnotations: Collection<AnnotationDescriptor>
): DPackage {
val name = descriptor.fqName.asString().takeUnless { it.isBlank() } ?: ""
val driWithPlatform = DRI(packageName = name).withEmptyInfo()
Expand All @@ -141,7 +153,14 @@ private class DokkaDescriptorVisitor(
classlikes = classlikes.await(),
typealiases = typealiases.await(),
documentation = descriptor.resolveDescriptorData(),
sourceSets = setOf(sourceSet)
sourceSets = setOf(sourceSet),
extra = if (fileAnnotations.isEmpty()) {
PropertyContainer.empty()
} else {
PropertyContainer.withAll(
fileAnnotations.toAnnotationsWithSource().toSourceSetDependent().toFileAnnotations()
)
}
)
}
}
Expand Down Expand Up @@ -782,8 +801,8 @@ private class DokkaDescriptorVisitor(

private suspend fun org.jetbrains.kotlin.descriptors.annotations.Annotations.getPresentableName(): String? =
map { it.toAnnotation() }.singleOrNull { it.dri.classNames == "ParameterName" }?.params?.get("name")
.safeAs<StringValue>()?.value?.drop(1)
?.dropLast(1) // Dropping enclosing doublequotes because we don't want to have it in our custom signature serializer
// Dropping enclosing doublequotes because we don't want to have it in our custom signature serializer
.safeAs<StringValue>()?.unquotedValue

private suspend fun KotlinType.toBound(): Bound = when (this) {

Expand Down Expand Up @@ -941,12 +960,29 @@ private class DokkaDescriptorVisitor(
)
}

private suspend fun Collection<AnnotationDescriptor>.toAnnotationsWithSource(): Multimap<DocumentableSource, Annotations.Annotation> {
val annotationsWithSource = parallelMap { annotationDescriptor ->
annotationDescriptor.toAnnotation() to AnnotationDescriptorDocumentableSource(annotationDescriptor)
}
return ArrayListMultimap.create<DocumentableSource, Annotations.Annotation>().apply {
annotationsWithSource.forEach { (annotation, source) ->
put(source, annotation)
}
}
}

private suspend fun PropertyDescriptor.getAnnotationsWithBackingField(): List<Annotations.Annotation> =
getAnnotations() + (backingField?.getAnnotations() ?: emptyList())

private fun List<Annotations.Annotation>.toAdditionalExtras() = mapNotNull {
try {
ExtraModifiers.valueOf(it.dri.classNames?.toLowerCase() ?: "")
val annotationClassName = it.dri.classNames
if (annotationClassName == "JvmStatic") {
// Handle JvmStatic case
ExtraModifiers.JavaOnlyModifiers.Static
} else {
ExtraModifiers.valueOf(annotationClassName?.toLowerCase() ?: "")
}
} catch (e: IllegalArgumentException) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,39 @@ class DefaultDescriptorToDocumentableTranslatorTest : BaseAbstractTest() {
}
}
}

@Test
fun `file annotations`() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/main/kotlin")
analysisPlatform = "jvm"
classpath += listOfNotNull(jvmStdlibPath)
}
}
}

testInline(
"""
|/src/main/kotlin/sample/XD.kt
|@file:JvmName("B")
|
|package sample
|
|fun a(): Int = 1
""".trimIndent(),
configuration
) {
documentablesMergingStage = { module ->
val pkg = module.packages.single()
val fileAnnotations = pkg.extra[FileAnnotations]
Assert.assertNotNull(fileAnnotations)
val annotation = fileAnnotations!!.content.values.single().values().single()
Assert.assertEquals("JvmName", annotation.dri.classNames)
val value = annotation.params["name"] as StringValue
Assert.assertEquals("B", value.unquotedValue)
}
}
}
}
Loading