diff options
Diffstat (limited to 'plugins/kotlin-as-java')
4 files changed, 114 insertions, 17 deletions
diff --git a/plugins/kotlin-as-java/build.gradle.kts b/plugins/kotlin-as-java/build.gradle.kts index f03a28c6..2c7f1e01 100644 --- a/plugins/kotlin-as-java/build.gradle.kts +++ b/plugins/kotlin-as-java/build.gradle.kts @@ -5,6 +5,7 @@ dependencies { testImplementation(project(":plugins:base")) testImplementation(project(":plugins:base:base-test-utils")) testImplementation(project(":core:content-matcher-test-utils")) + testImplementation("org.jsoup:jsoup:1.12.1") } registerDokkaArtifactPublication("kotlinAsJavaPlugin") { 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 d79bd7b1..38d904e4 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/converters/KotlinToJavaConverter.kt @@ -175,20 +175,33 @@ internal fun DClass.asJava(): DClass = copy( private fun DTypeParameter.asJava(): DTypeParameter = copy( variantTypeParameter = variantTypeParameter.withDri(dri.possiblyAsJava()), - bounds = bounds.map { it.asJava() } + bounds = bounds.map { it.asJava() as Bound } ) -private fun Bound.asJava(): Bound = when (this) { +private fun Projection.asJava(): Projection = when(this) { + is Star -> Star + is Covariance<*> -> copy(inner.asJava()) + is Contravariance<*> -> copy(inner.asJava()) + is Invariance<*> -> copy(inner.asJava()) + is Bound -> asJava() +} + +private fun Bound.asJava(): Bound = when(this) { + is TypeParameter -> copy(dri.possiblyAsJava()) is TypeConstructor -> copy( - dri = dri.possiblyAsJava() + dri = dri.possiblyAsJava(), + projections = projections.map { it.asJava() } ) is TypeAliased -> copy( + typeAlias = typeAlias.asJava(), inner = inner.asJava() ) - is Nullable -> copy( - inner = inner.asJava() - ) - else -> this + is Nullable -> copy(inner.asJava()) + is PrimitiveJavaType -> this + is Void -> this + is JavaObject -> this + is Dynamic -> this + is UnresolvedBound -> this } internal fun DEnum.asJava(): DEnum = copy( diff --git a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt index d24fa428..1d62a206 100644 --- a/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt +++ b/plugins/kotlin-as-java/src/main/kotlin/signatures/JavaSignatureProvider.kt @@ -79,10 +79,10 @@ class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogge c.supertypes.map { (p, dris) -> val (classes, interfaces) = dris.partition { it.kind == JavaClassKindTypes.CLASS } list(classes, prefix = " extends ", sourceSets = setOf(p)) { - link(it.typeConstructor.dri.sureClassNames, it.typeConstructor.dri, sourceSets = setOf(p)) + signatureForProjection(it.typeConstructor) } - list(interfaces, prefix = " implements ", sourceSets = setOf(p)){ - link(it.typeConstructor.dri.sureClassNames, it.typeConstructor.dri, sourceSets = setOf(p)) + list(interfaces, prefix = " implements ", sourceSets = setOf(p)) { + signatureForProjection(it.typeConstructor) } } } @@ -160,7 +160,12 @@ class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogge } is Variance<*> -> group(styles = emptySet()) { - text("$p ".takeIf { it.isNotBlank() } ?: "") // TODO: "super" && "extends" + val variance = when(p) { + is Covariance<*> -> "? extends " + is Contravariance<*> -> "? super " + is Invariance<*> -> "" + } + text(variance) signatureForProjection(p.inner) } diff --git a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt index 3dbe5888..c6dc9a25 100644 --- a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt +++ b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt @@ -6,6 +6,14 @@ import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest import org.jetbrains.kotlin.utils.addToStdlib.cast import org.junit.jupiter.api.Test import matchers.content.* +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.jdk +import signatures.renderedContent +import signatures.signature +import utils.A +import utils.Span +import utils.TestOutputWriterPlugin +import utils.match class KotlinAsJavaPluginTest : AbstractCoreTest() { @@ -31,7 +39,7 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { |fun testF3(to: TestObj) = to |fun <T : Char> testF4(t: T) = listOf(t) |val testV = 1 - """, + """.trimMargin(), configuration, cleanupOutput = true ) { @@ -68,7 +76,7 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { | |fun testF(i: Int) = i |val testV = 1 - """, + """.trimMargin(), configuration, cleanupOutput = true ) { @@ -109,7 +117,7 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { |class TestJ { | int testF(int i) { return i; } |} - """, + """.trimMargin(), configuration, cleanupOutput = true ) { @@ -149,7 +157,7 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { |class Test { | public val publicProperty: String = "" |} - """, + """.trimMargin(), configuration, cleanupOutput = true ) { @@ -202,7 +210,7 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { |class TestJ { | public Int publicProperty = 1; |} - """, + """.trimMargin(), configuration, cleanupOutput = true ) { @@ -252,7 +260,7 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { | open class A { } | interface B | class C : A(), B - """, + """.trimMargin(), configuration, cleanupOutput = true ) { @@ -290,6 +298,76 @@ class KotlinAsJavaPluginTest : AbstractCoreTest() { private fun <T> Collection<T>.assertCount(n: Int, prefix: String = "") = assert(count() == n) { "${prefix}Expected $n, got ${count()}" } + @Test + fun `typealias`() { + val writerPlugin = TestOutputWriterPlugin() + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/") + externalDocumentationLinks = listOf(DokkaConfiguration.ExternalDocumentationLink.jdk(8)) + } + } + } + testInline( + """ + |/src/main/kotlin/kotlinAsJavaPlugin/Test.kt + |package kotlinAsJavaPlugin + | + |typealias XD = Int + |class ABC { + | fun someFun(xd: XD): Int = 1 + |} + """.trimMargin(), + configuration, + pluginOverrides = listOf(writerPlugin), + cleanupOutput = true + ) { + renderingStage = { _, _ -> + writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").signature().first().match( + "final ", A("Integer"), A("someFun"), "(", A("Integer"), A("xd"), ")", Span() + ) + } + } + } + + @Test + fun `typealias with generic`() { + 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 + | + |typealias XD<B, A> = Map<A, B> + | + |class ABC { + | fun someFun(xd: XD<Int, String>) = 1 + |} + """.trimMargin(), + configuration, + pluginOverrides = listOf(writerPlugin), + cleanupOutput = true + ) { + renderingStage = { _, _ -> + writerPlugin.writer.renderedContent("root/kotlinAsJavaPlugin/-a-b-c/some-fun.html").signature().first().match( + "final ", A("Integer"), A("someFun"), "(", A("Map"), "<", A("String"), + ", ", A("Integer"), ">", A("xd"), ")", Span() + ) + } + } + } } private val ContentNode.mainContents: List<ContentNode> |