diff options
author | Marcin Aman <maman@virtuslab.com> | 2020-07-06 17:00:13 +0200 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-07-13 14:48:27 +0200 |
commit | 6377d964819f937bd4a7633488d69edc327542d8 (patch) | |
tree | 5e52cf65cfce31de9a1500d08a9bfe25deb00a90 /plugins/kotlin-as-java | |
parent | d180633e91afd76de788a0bd1bfe75fe09f19aea (diff) | |
download | dokka-6377d964819f937bd4a7633488d69edc327542d8.tar.gz dokka-6377d964819f937bd4a7633488d69edc327542d8.tar.bz2 dokka-6377d964819f937bd4a7633488d69edc327542d8.zip |
Java sources in Kotlin-As-Java plugin should have original property visibilites
Diffstat (limited to 'plugins/kotlin-as-java')
3 files changed, 127 insertions, 16 deletions
diff --git a/plugins/kotlin-as-java/build.gradle.kts b/plugins/kotlin-as-java/build.gradle.kts index bda876c1..bd08ab92 100644 --- a/plugins/kotlin-as-java/build.gradle.kts +++ b/plugins/kotlin-as-java/build.gradle.kts @@ -11,6 +11,9 @@ publishing { dependencies { implementation(project(":plugins:base")) + testImplementation(project(":plugins:base")) + testImplementation(project(":plugins:base:test-utils")) + testImplementation(project(":test-tools")) } configureBintrayPublication("kotlinAsJavaPlugin")
\ No newline at end of file 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 1ca4ef51..24d99995 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt @@ -67,12 +67,8 @@ internal fun DProperty.asJava(isTopLevel: Boolean = false, relocateToClass: Stri } else { dri.withClass(relocateToClass) }, - modifier = if (setter == null) { - sourceSets.map { it to JavaModifier.Final }.toMap() - } else { - sourceSets.map { it to JavaModifier.Empty }.toMap() - }, - visibility = visibility.mapValues { JavaVisibility.Private }, + modifier = javaModifierFromSetter(), + visibility = visibility.mapValues { it.value.propertyVisibilityAsJava() }, type = type.asJava(), // TODO: check setter = null, getter = null, // Removing getters and setters as they will be available as functions @@ -85,6 +81,15 @@ internal fun DProperty.asJava(isTopLevel: Boolean = false, relocateToClass: Stri else extra ) +internal fun DProperty.javaModifierFromSetter() = + modifier.mapValues { + when { + it.value is JavaModifier -> it.value + setter == null -> JavaModifier.Final + else -> JavaModifier.Empty + } + } + internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClass: String? = null): List<DFunction> = listOfNotNull( getter?.copy( @@ -94,11 +99,7 @@ internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClas dri.withClass(relocateToClass) }, name = "get" + name.capitalize(), - modifier = if (setter == null) { - sourceSets.map { it to JavaModifier.Final }.toMap() - } else { - sourceSets.map { it to JavaModifier.Empty }.toMap() - }, + modifier = javaModifierFromSetter(), visibility = visibility.mapValues { JavaVisibility.Public }, type = type.asJava(), // TODO: check extra = if (isTopLevel) getter!!.extra + @@ -116,11 +117,7 @@ internal fun DProperty.javaAccessors(isTopLevel: Boolean = false, relocateToClas dri.withClass(relocateToClass) }, name = "set" + name.capitalize(), - modifier = if (setter == null) { - sourceSets.map { it to JavaModifier.Final }.toMap() - } else { - sourceSets.map { it to JavaModifier.Empty }.toMap() - }, + modifier = javaModifierFromSetter(), visibility = visibility.mapValues { JavaVisibility.Public }, type = type.asJava(), // TODO: check extra = if (isTopLevel) setter!!.extra + setter!!.extra.mergeAdditionalModifiers( @@ -249,6 +246,10 @@ internal fun DParameter.asJava(): DParameter = copy( name = if (name.isNullOrBlank()) "\$self" else name ) +internal fun Visibility.propertyVisibilityAsJava(): Visibility = + if(this is JavaVisibility) this + else JavaVisibility.Private + internal fun String.getAsPrimitive(): JvmPrimitiveType? = org.jetbrains.kotlin.builtins.PrimitiveType.values() .find { it.typeFqName.asString() == this } ?.let { JvmPrimitiveType.get(it) } diff --git a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt index 96446201..ba513bf2 100644 --- a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt +++ b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt @@ -1,9 +1,11 @@ package kotlinAsJavaPlugin +import org.jetbrains.dokka.model.dfs import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest import org.jetbrains.kotlin.utils.addToStdlib.cast import org.junit.jupiter.api.Test +import matchers.content.* class KotlinAsJavaPluginTest : AbstractCoreTest() { @@ -130,6 +132,111 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { } } + @Test + fun `public kotlin properties should have a getter with same visibilities`(){ + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + } + } + } + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/Test.kt + |package kotlinAsJavaPlugin + | + |class Test { + | public val publicProperty: String = "" + |} + """, + configuration, + cleanupOutput = true + ) { + pagesTransformationStage = { rootPageNode -> + val propertyGetter = rootPageNode.dfs { it is MemberPageNode && it.name == "getPublicProperty" } as? MemberPageNode + assert(propertyGetter != null) + propertyGetter!!.content.assertNode { + group { + header(1) { + +"getPublicProperty" + } + } + divergentGroup { + divergentInstance { + divergent { + group { + +"final" + group { + link { + +" String" + } + } + group { + link { + +"getPublicProperty" + } + +"()" + } + } + } + } + } + } + } + } + } + + @Test + fun `java properties should keep its modifiers`(){ + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + } + } + } + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/TestJ.java + |package kotlinAsJavaPlugin + | + |class TestJ { + | public Int publicProperty = 1; + |} + """, + configuration, + cleanupOutput = true + ) { + pagesGenerationStage = { root -> + val testClass = root.dfs { it.name == "TestJ" } as? ClasslikePageNode + assert(testClass != null) + (testClass!!.content as ContentGroup).children.last().assertNode { + group { + header(2){ + +"Properties" + } + table { + group { + link { + +"publicProperty" + } + platformHinted { + group { + +"public Int" + link { + +"publicProperty" + } + } + } + } + } + } + } + } + } + } + private fun <T> Collection<T>.assertCount(n: Int, prefix: String = "") = assert(count() == n) { "${prefix}Expected $n, got ${count()}" } |