diff options
Diffstat (limited to 'plugins')
3 files changed, 77 insertions, 3 deletions
diff --git a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocClasslikeTemplateMapTest.kt b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocClasslikeTemplateMapTest.kt index f03b10b4..2fb1ae0e 100644 --- a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocClasslikeTemplateMapTest.kt +++ b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocClasslikeTemplateMapTest.kt @@ -316,6 +316,33 @@ internal class JavadocClasslikeTemplateMapTest : AbstractJavadocTemplateMapTest( } } + @Test + fun `class with top-level const`() { + dualTestTemplateMapInline( + kotlin = + """ + /src/Test.kt + package com.test.package0 + + const val TEST_VAL = "test" + """, + java = + """ + /src/com/test/package0/TestKt.java + package com.test.package0; + + public final class TestKt { + public static final String TEST_VAL = "test"; + } + """ + ) { + val map = singlePageOfType<JavadocClasslikePageNode>().templateMap + val properties = assertIsInstance<List<*>>(map["properties"]) + val property = assertIsInstance<Map<String, Any?>>(properties.first()) + assertEquals("public final static <a href=https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>String</a> <a href=TestKt.html#TEST_VAL>TEST_VAL</a>", "${property["modifiers"]} ${property["signature"]}") + } + } + private fun assertParameterNode(node: Map<String, Any?>, expectedName: String, expectedType: String, expectedDescription: String){ assertEquals(expectedName, node["name"]) assertEquals(expectedType, node["type"]) diff --git a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt index 5112ae0b..0f7d5c7a 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt @@ -23,6 +23,13 @@ private fun <T : WithSources> List<T>.groupedByLocation() = } // TODO: first() does not look reasonable }) { it.second } +private val DProperty.isConst: Boolean + get() = extra[AdditionalModifiers] + ?.content + ?.any { (_, modifiers) -> + ExtraModifiers.KotlinOnlyModifiers.Const in modifiers + } == true + internal fun DPackage.asJava(): DPackage { @Suppress("UNCHECKED_CAST") val syntheticClasses = ((properties + functions) as List<WithSources>) @@ -31,10 +38,12 @@ internal fun DPackage.asJava(): DPackage { DClass( dri = dri.withClass(syntheticClassName), name = syntheticClassName, - properties = nodes.filterIsInstance<DProperty>().map { it.asJava() }, + properties = nodes.filterIsInstance<DProperty>().map { it.asJava(true) }, constructors = emptyList(), functions = ( - nodes.filterIsInstance<DProperty>() + nodes + .filterIsInstance<DProperty>() + .filterNot { it.isConst } .flatMap { it.javaAccessors() } + nodes.filterIsInstance<DFunction>() .map { it.asJava(syntheticClassName) }), // TODO: methods are static and receiver is a param @@ -71,7 +80,13 @@ internal fun DProperty.asJava(isTopLevel: Boolean = false, relocateToClass: Stri dri.withClass(relocateToClass) }, modifier = javaModifierFromSetter(), - visibility = visibility.mapValues { it.value.propertyVisibilityAsJava() }, + visibility = visibility.mapValues { + if (isTopLevel && isConst) { + JavaVisibility.Public + } else { + it.value.propertyVisibilityAsJava() + } + }, type = type.asJava(), // TODO: check setter = null, getter = null, // Removing getters and setters as they will be available as functions diff --git a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt index 7def1111..5430ea88 100644 --- a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt +++ b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test import matchers.content.* import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.jdk +import org.junit.Assert import signatures.renderedContent import signatures.signature import utils.A @@ -372,6 +373,37 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() { } } } + + @Test + fun `const in top level`() { + val writerPlugin = TestOutputWriterPlugin() + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + externalDocumentationLinks = listOf( + DokkaConfiguration.ExternalDocumentationLink.jdk(8), + stdlibExternalDocumentationLink + ) + } + } + } + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/Test.kt + |package kotlinAsJavaPlugin + | + |const val FIRST = "String" + """.trimMargin(), + configuration, + pluginOverrides = listOf(writerPlugin), + cleanupOutput = true + ) { + renderingStage = { _, _ -> + Assert.assertNull(writerPlugin.writer.contents["root/kotlinAsJavaPlugin/-test-kt/get-f-i-r-s-t.html"]) + } + } + } } private val ContentNode.mainContents: List<ContentNode> |