aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/kotlin-as-java')
-rw-r--r--plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt21
-rw-r--r--plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt32
2 files changed, 50 insertions, 3 deletions
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>