aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src
diff options
context:
space:
mode:
authorIgnat Beresnev <ignat.beresnev@jetbrains.com>2022-06-30 16:03:56 +0200
committerGitHub <noreply@github.com>2022-06-30 16:03:56 +0200
commite3c76cba8e9838b3ab2635261e3c143690fbc0c8 (patch)
tree4bf5884f7cabfb37ba60bb05335f152c4d3ba1e2 /plugins/base/src
parent76334ec6e8d66b377fc9c37187725f8d267d5ba2 (diff)
downloaddokka-e3c76cba8e9838b3ab2635261e3c143690fbc0c8.tar.gz
dokka-e3c76cba8e9838b3ab2635261e3c143690fbc0c8.tar.bz2
dokka-e3c76cba8e9838b3ab2635261e3c143690fbc0c8.zip
Mark synthetic methods generated for Java enums as obvious (#2554)
Fixes #2548
Diffstat (limited to 'plugins/base/src')
-rw-r--r--plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt13
-rw-r--r--plugins/base/src/test/kotlin/enums/JavaEnumTest.kt66
-rw-r--r--plugins/base/src/test/kotlin/model/JavaTest.kt5
3 files changed, 75 insertions, 9 deletions
diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
index f64eb261..500b0728 100644
--- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
+++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt
@@ -112,9 +112,6 @@ class DefaultPsiToDocumentableTranslator(
private val PsiClassType.shouldBeIgnored: Boolean
get() = isClass("java.lang.Enum") || isClass("java.lang.Object")
- private val DRI.isObvious: Boolean
- get() = packageName == "java.lang" && (classNames == "Object" || classNames == "Enum")
-
private fun PsiClassType.isClass(qName: String): Boolean {
val shortName = qName.substringAfterLast('.')
if (className == shortName) {
@@ -446,7 +443,7 @@ class DefaultPsiToDocumentableTranslator(
(psi.annotations.toList()
.toListOfAnnotations() + it.toListOfAnnotations()).toSourceSetDependent()
.toAnnotations(),
- ObviousMember.takeIf { inheritedFrom != null && inheritedFrom.isObvious },
+ ObviousMember.takeIf { psi.isObvious(inheritedFrom) },
psi.throwsList.toDriList().takeIf { it.isNotEmpty() }
?.let { CheckedExceptions(it.toSourceSetDependent()) }
)
@@ -454,6 +451,14 @@ class DefaultPsiToDocumentableTranslator(
)
}
+ private fun PsiMethod.isObvious(inheritedFrom: DRI? = null): Boolean {
+ return this is SyntheticElement || inheritedFrom?.isObvious() == true
+ }
+
+ private fun DRI.isObvious(): Boolean {
+ return packageName == "java.lang" && (classNames == "Object" || classNames == "Enum")
+ }
+
private fun PsiReferenceList.toDriList() = referenceElements.mapNotNull { it?.resolve()?.let { DRI.from(it) } }
private fun PsiModifierListOwner.additionalExtras() = listOfNotNull(
diff --git a/plugins/base/src/test/kotlin/enums/JavaEnumTest.kt b/plugins/base/src/test/kotlin/enums/JavaEnumTest.kt
new file mode 100644
index 00000000..369fbe79
--- /dev/null
+++ b/plugins/base/src/test/kotlin/enums/JavaEnumTest.kt
@@ -0,0 +1,66 @@
+package enums
+
+import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
+import org.jetbrains.dokka.model.DEnum
+import org.jetbrains.dokka.model.ObviousMember
+import org.junit.jupiter.api.Test
+import utils.TestOutputWriterPlugin
+import kotlin.test.assertEquals
+import kotlin.test.assertNotNull
+
+class JavaEnumTest : BaseAbstractTest() {
+
+ private val configuration = dokkaConfiguration {
+ sourceSets {
+ sourceSet {
+ sourceRoots = listOf("src/")
+ }
+ }
+ }
+
+ @Test
+ fun `should mark synthetic functions generated for Kotlin as obvious`() {
+ val writerPlugin = TestOutputWriterPlugin()
+ testInline(
+ """
+ |/src/main/java/basic/JavaEnum.java
+ |package testpackage
+ |
+ |public enum JavaEnum {
+ | ONE, TWO
+ |}
+ """.trimMargin(),
+ configuration,
+ pluginOverrides = listOf(writerPlugin)
+ ) {
+ documentablesCreationStage = { modules ->
+ val pckg = modules.flatMap { it.packages }.single { it.packageName == "testpackage" }
+ val enum = pckg.children.single { it is DEnum } as DEnum
+
+ // there's two with the same name, one inherited from
+ // java.lang.Enum and one is synthetic for Kotlin interop
+ enum.functions.filter { it.name == "valueOf" }.let { valueOfMethods ->
+ assertEquals(2, valueOfMethods.size)
+
+ val valueOfFromKotlin = valueOfMethods[0]
+ assertEquals(
+ "testpackage/JavaEnum/valueOf/#java.lang.String/PointingToDeclaration/",
+ valueOfFromKotlin.dri.toString()
+ )
+ assertNotNull(valueOfFromKotlin.extra[ObviousMember])
+
+ val valueOfFromJava = valueOfMethods[1]
+ assertEquals(
+ "java.lang/Enum/valueOf/#java.lang.Class<T>#java.lang.String/PointingToDeclaration/",
+ valueOfFromJava.dri.toString()
+ )
+ assertNotNull(valueOfFromJava.extra[ObviousMember])
+ }
+
+ val valuesMethod = enum.functions.single { it.name == "values" }
+ assertEquals("testpackage/JavaEnum/values/#/PointingToDeclaration/", valuesMethod.dri.toString())
+ assertNotNull(valuesMethod.extra[ObviousMember])
+ }
+ }
+ }
+}
diff --git a/plugins/base/src/test/kotlin/model/JavaTest.kt b/plugins/base/src/test/kotlin/model/JavaTest.kt
index 748ef190..0abaf1b1 100644
--- a/plugins/base/src/test/kotlin/model/JavaTest.kt
+++ b/plugins/base/src/test/kotlin/model/JavaTest.kt
@@ -334,11 +334,6 @@ class JavaTest : AbstractModelTest("/src/main/kotlin/java/Test.java", "java") {
with((this / "java" / "E").cast<DEnum>()) {
name equals "E"
entries counts 1
- functions.sortedBy { it.name }.filter { it.name == "valueOf" || it.name == "values" }.map { it.dri } equals listOf(
- DRI("java", "E", DRICallable("valueOf", null, listOf(JavaClassReference("java.lang.String"))), PointingToDeclaration),
- DRI("java", "E", DRICallable("values", null, emptyList()), PointingToDeclaration),
- )
-
with((this / "Foo").cast<DEnumEntry>()) {
name equals "Foo"
}