-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to merge inherited properties in kotlin from java sources.
- Loading branch information
1 parent
581e76c
commit f240e59
Showing
7 changed files
with
289 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
plugins/base/src/main/kotlin/transformers/documentables/PropertiesMergerTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.jetbrains.dokka.base.transformers.documentables | ||
|
||
import org.jetbrains.dokka.model.* | ||
import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer | ||
import org.jetbrains.kotlin.load.java.JvmAbi | ||
import org.jetbrains.kotlin.load.java.propertyNameByGetMethodName | ||
import org.jetbrains.kotlin.load.java.propertyNamesBySetMethodName | ||
import org.jetbrains.kotlin.name.Name | ||
|
||
class PropertiesMergerTransformer : PreMergeDocumentableTransformer { | ||
|
||
override fun invoke(modules: List<DModule>) = | ||
modules.map { it.copy(packages = it.packages.map { | ||
it.mergeBeansAndField().copy( | ||
classlikes = it.classlikes.map { it.mergeBeansAndField() } | ||
) | ||
}) } | ||
|
||
private fun <T : Documentable> T.mergeBeansAndField(): T = when (this) { | ||
is DClass -> { | ||
val (functions, properties) = mergePotentialBeansAndField(this.functions, this.properties) | ||
this.copy(functions = functions, properties = properties) | ||
} | ||
is DEnum -> { | ||
val (functions, properties) = mergePotentialBeansAndField(this.functions, this.properties) | ||
this.copy(functions = functions, properties = properties) | ||
} | ||
is DInterface -> { | ||
val (functions, properties) = mergePotentialBeansAndField(this.functions, this.properties) | ||
this.copy(functions = functions, properties = properties) | ||
} | ||
is DObject -> { | ||
val (functions, properties) = mergePotentialBeansAndField(this.functions, this.properties) | ||
this.copy(functions = functions, properties = properties) | ||
} | ||
is DAnnotation -> { | ||
val (functions, properties) = mergePotentialBeansAndField(this.functions, this.properties) | ||
this.copy(functions = functions, properties = properties) | ||
} | ||
is DPackage -> { | ||
val (functions, properties) = mergePotentialBeansAndField(this.functions, this.properties) | ||
this.copy(functions = functions, properties = properties) | ||
} | ||
else -> this | ||
} as T | ||
|
||
private fun DFunction.getPropertyNameForFunction() = | ||
when { | ||
JvmAbi.isGetterName(name) -> propertyNameByGetMethodName(Name.identifier(name))?.asString() | ||
JvmAbi.isSetterName(name) -> propertyNamesBySetMethodName(Name.identifier(name)).firstOrNull() | ||
?.asString() | ||
else -> null | ||
} | ||
|
||
private fun mergePotentialBeansAndField( | ||
functions: List<DFunction>, | ||
fields: List<DProperty> | ||
): Pair<List<DFunction>, List<DProperty>> { | ||
val fieldNames = fields.associateBy { it.name } | ||
val accessors = mutableMapOf<DProperty, MutableList<DFunction>>() | ||
val regularMethods = mutableListOf<DFunction>() | ||
functions.forEach { method -> | ||
val field = method.getPropertyNameForFunction()?.let { name -> fieldNames[name] } | ||
if (field != null) { | ||
accessors.getOrPut(field, ::mutableListOf).add(method) | ||
} else { | ||
regularMethods.add(method) | ||
} | ||
} | ||
return regularMethods.toList() to accessors.map { (dProperty, dFunctions) -> | ||
if (dProperty.visibility.values.all { it is KotlinVisibility.Private }) { | ||
dFunctions.flatMap { it.visibility.values }.toSet().singleOrNull()?.takeIf { | ||
it in listOf(KotlinVisibility.Public, KotlinVisibility.Protected) | ||
}?.let { visibility -> | ||
dProperty.copy( | ||
getter = dFunctions.firstOrNull { it.type == dProperty.type }, | ||
setter = dFunctions.firstOrNull { it.parameters.isNotEmpty() }, | ||
visibility = dProperty.visibility.mapValues { visibility } | ||
) | ||
} ?: dProperty | ||
} else { | ||
dProperty | ||
} | ||
} + fields.toSet().minus(accessors.keys.toSet()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
plugins/base/src/test/kotlin/superFields/DescriptorSuperPropertiesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package superFields | ||
|
||
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest | ||
import org.jetbrains.dokka.links.Callable | ||
import org.jetbrains.dokka.links.DRI | ||
import org.jetbrains.dokka.links.TypeConstructor | ||
import org.jetbrains.dokka.links.TypeReference | ||
import org.jetbrains.dokka.model.InheritedMember | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
class DescriptorSuperPropertiesTest : BaseAbstractTest() { | ||
|
||
@Test | ||
fun `kotlin inheriting java should append getter`() { | ||
testInline( | ||
""" | ||
|/src/test/A.java | ||
|package test; | ||
|public class A { | ||
| private int a = 1; | ||
| public int getA() { return a; } | ||
|} | ||
| | ||
|/src/test/B.kt | ||
|package test | ||
|class B : A {} | ||
""".trimIndent(), | ||
dokkaConfiguration { | ||
sourceSets { | ||
sourceSet { | ||
sourceRoots = listOf("src/") | ||
analysisPlatform = "jvm" | ||
name = "jvm" | ||
} | ||
} | ||
} | ||
) { | ||
this.documentablesTransformationStage = { | ||
it.packages.single().classlikes.single { it.name == "B" }.properties.single { it.name == "a" }.run { | ||
Assertions.assertNotNull(this) | ||
Assertions.assertNotNull(this.getter) | ||
Assertions.assertNull(this.setter) | ||
this.extra[InheritedMember]?.inheritedFrom?.values?.single()?.run { | ||
assertEquals( | ||
DRI(packageName = "test", classNames = "A", callable = Callable("a", params = emptyList())), | ||
this | ||
) | ||
} | ||
this.getter.run { | ||
this!!.extra[InheritedMember]?.inheritedFrom?.values?.single()?.run { | ||
assertEquals( | ||
DRI(packageName = "test", classNames = "A", callable = Callable("getA", params = emptyList())), | ||
this | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `kotlin inheriting java should append getter and setter`() { | ||
testInline( | ||
""" | ||
|/src/test/A.java | ||
|package test; | ||
|public class A { | ||
| private int a = 1; | ||
| public int getA() { return a; } | ||
| public void setA(int a) { this.a = a; } | ||
|} | ||
| | ||
|/src/test/B.kt | ||
|package test | ||
|class B : A {} | ||
""".trimIndent(), | ||
dokkaConfiguration { | ||
sourceSets { | ||
sourceSet { | ||
sourceRoots = listOf("src/") | ||
analysisPlatform = "jvm" | ||
name = "jvm" | ||
} | ||
} | ||
} | ||
) { | ||
documentablesMergingStage = { | ||
it.packages.single().classlikes.single { it.name == "B" }.properties.single { it.name == "a" }.run { | ||
Assertions.assertNotNull(this) | ||
Assertions.assertNotNull(this.getter) | ||
Assertions.assertNotNull(this.setter) | ||
this.extra[InheritedMember]?.inheritedFrom?.values?.single()?.run { | ||
assertEquals( | ||
DRI(packageName = "test", classNames = "A", callable = Callable("a", params = emptyList())), | ||
this | ||
) | ||
} | ||
this.getter.run { | ||
this!!.extra[InheritedMember]?.inheritedFrom?.values?.single()?.run { | ||
assertEquals( | ||
DRI(packageName = "test", classNames = "A", callable = Callable("getA", params = emptyList())), | ||
this | ||
) | ||
} | ||
} | ||
this.setter.run { | ||
this!!.extra[InheritedMember]?.inheritedFrom?.values?.single()?.run { | ||
assertEquals( | ||
DRI(packageName = "test", classNames = "A", | ||
callable = Callable("setA", | ||
params = listOf(TypeConstructor("kotlin.Int", emptyList())) | ||
) | ||
), | ||
this | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `kotlin inheriting java should not append anything since field is public`() { | ||
testInline( | ||
""" | ||
|/src/test/A.java | ||
|package test; | ||
|public class A { | ||
| public int a = 1; | ||
| public int getA() { return a; } | ||
| public void setA(int a) { this.a = a; } | ||
|} | ||
| | ||
|/src/test/B.kt | ||
|package test | ||
|class B : A {} | ||
""".trimIndent(), | ||
dokkaConfiguration { | ||
sourceSets { | ||
sourceSet { | ||
sourceRoots = listOf("src/") | ||
analysisPlatform = "jvm" | ||
name = "jvm" | ||
classpath += jvmStdlibPath!! | ||
} | ||
} | ||
} | ||
) { | ||
documentablesMergingStage = { | ||
it.packages.single().classlikes.single { it.name == "B" }.properties.single { it.name == "a" }.run { | ||
Assertions.assertNotNull(this) | ||
Assertions.assertNull(this.getter) | ||
Assertions.assertNull(this.setter) | ||
this.extra[InheritedMember]?.inheritedFrom?.values?.single()?.run { | ||
assertEquals( | ||
DRI(packageName = "test", classNames = "A", callable = Callable("a", params = emptyList())), | ||
this | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters