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 missing annotations in GFM and unresolved static imports #1845

Merged
merged 2 commits into from Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,6 +2,8 @@ package org.jetbrains.dokka.base.translators.psi

import com.intellij.lang.jvm.JvmModifier
import com.intellij.lang.jvm.annotation.JvmAnnotationAttribute
import com.intellij.lang.jvm.annotation.JvmAnnotationAttributeValue
import com.intellij.lang.jvm.annotation.JvmAnnotationEnumFieldValue
import com.intellij.lang.jvm.types.JvmReferenceType
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.*
Expand Down Expand Up @@ -572,13 +574,22 @@ class DefaultPsiToDocumentableTranslator(
filter { it !is KtLightAbstractAnnotation }.mapNotNull { it.toAnnotation() }

private fun JvmAnnotationAttribute.toValue(): AnnotationParameterValue = when (this) {
is PsiNameValuePair -> value?.toValue() ?: StringValue("")
is PsiNameValuePair -> value?.toValue() ?: attributeValue?.toValue() ?: StringValue("")
else -> StringValue(this.attributeName)
}.let { annotationValue ->
if (annotationValue is StringValue) annotationValue.copy(unquotedValue(annotationValue.value))
else annotationValue
}

/**
* This is a workaround for static imports from JDK like RetentionPolicy
* For some reason they are not represented in the same way than using normal import
*/
private fun JvmAnnotationAttributeValue.toValue(): AnnotationParameterValue? = when (this) {
is JvmAnnotationEnumFieldValue -> (field as? PsiElement)?.let { EnumValue(fieldName ?: "", DRI.from(it)) }
else -> null
}

private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue? = when (this) {
is PsiAnnotation -> toAnnotation()?.let { AnnotationValue(it) }
is PsiArrayInitializerMemberValue -> ArrayValue(initializers.mapNotNull { it.toValue() })
Expand Down
38 changes: 38 additions & 0 deletions plugins/base/src/test/kotlin/model/JavaTest.kt
Expand Up @@ -14,6 +14,7 @@ import org.junit.jupiter.api.Test
import utils.AbstractModelTest
import utils.assertNotNull
import utils.name
import kotlin.test.assertEquals
import org.jetbrains.dokka.links.Callable as DRICallable

class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
Expand Down Expand Up @@ -342,4 +343,41 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
}
}
}

@Test
fun `retention should work with static import`() {
inlineModelTest(
"""
|import java.lang.annotation.Retention;
|import java.lang.annotation.RetentionPolicy;
|import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
|@Retention(RUNTIME)
|public @interface JsonClass {
|};
""", configuration = configuration
) {
with((this / "java" / "JsonClass").cast<DAnnotation>()) {
val annotation = extra[Annotations]?.directAnnotations?.entries
?.firstOrNull()?.value //First sourceset
?.firstOrNull()

val expectedDri = DRI("java.lang.annotation", "Retention", null, PointingToDeclaration)
val expectedParams = "value" to EnumValue(
"RUNTIME",
DRI(
"java.lang.annotation",
"RetentionPolicy",
DRICallable("RUNTIME", null, emptyList()),
PointingToDeclaration
)
)

assertEquals(expectedDri, annotation?.dri)
assertEquals(expectedParams.first, annotation?.params?.entries?.first()?.key)
assertEquals(expectedParams.second, annotation?.params?.entries?.first()?.value)
}
}
}

}
Expand Up @@ -97,7 +97,7 @@ open class CommonmarkRenderer(
templateCommand(ResolveLinkGfmCommand(node.address)) {
buildText(node.children, pageContext, sourceSetRestriction)
}
} else Unit
} else buildText(node.children, pageContext, sourceSetRestriction)
}

override fun StringBuilder.buildNewLine() {
Expand Down