aboutsummaryrefslogtreecommitdiff
path: root/plugins/base
diff options
context:
space:
mode:
authorMarcin Aman <marcin.aman@gmail.com>2021-04-14 15:16:45 +0200
committerGitHub <noreply@github.com>2021-04-14 15:16:45 +0200
commit38270a3b8783857bd0bba4b9422b19b4de507e4c (patch)
tree71273a5c15db755ac264c6bddb164de40bbca2c1 /plugins/base
parentacd9234be6909a69f89600e9c8ddde642d36b1cf (diff)
downloaddokka-38270a3b8783857bd0bba4b9422b19b4de507e4c.tar.gz
dokka-38270a3b8783857bd0bba4b9422b19b4de507e4c.tar.bz2
dokka-38270a3b8783857bd0bba4b9422b19b4de507e4c.zip
Fix missing annotations in GFM and unresolved static imports (#1845)
* Fix missing unresolved links in GFM * Fix missing links to elements imported as static
Diffstat (limited to 'plugins/base')
-rw-r--r--plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt13
-rw-r--r--plugins/base/src/test/kotlin/model/JavaTest.kt38
2 files changed, 50 insertions, 1 deletions
diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
index 84510824..cd2bbfc6 100644
--- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
+++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
@@ -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.*
@@ -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() })
diff --git a/plugins/base/src/test/kotlin/model/JavaTest.kt b/plugins/base/src/test/kotlin/model/JavaTest.kt
index 72342962..da45a920 100644
--- a/plugins/base/src/test/kotlin/model/JavaTest.kt
+++ b/plugins/base/src/test/kotlin/model/JavaTest.kt
@@ -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") {
@@ -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)
+ }
+ }
+ }
+
}