Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,23 @@ private const val DEFAULT_COMPANION_NAME = "Companion"

internal fun DObject?.staticFunctionsForJava(): List<DFunction> {
if (this == null) return emptyList()
return functions.filter { it.isJvmStatic }
val propFunctions = properties.flatMap { listOfNotNull(it.getter, it.setter) }
return (functions + propFunctions).filter { it.isJvmStatic }
}

internal fun DObject?.staticPropertyAccessorsForJava(): List<DFunction> {
if (this == null) return emptyList()
val staticProps = properties.filter { it.isThisOrAccessorsStatic }
.flatMap { listOfNotNull(it.getter, it.setter) }
.map {
it.copy(
extra = PropertyContainer.withAll(sourceSets.map {
mapOf(it to setOf(ExtraModifiers.JavaOnlyModifiers.Static)).toAdditionalModifiers()
})
)
}
val propFunctions = properties.flatMap { listOfNotNull(it.getter, it.setter) }.filter { it.isJvmStatic }
return (staticProps + propFunctions).distinctBy { it.dri }
}

/**
Expand All @@ -21,7 +37,9 @@ internal fun DObject?.staticFunctionsForJava(): List<DFunction> {
*/
internal fun DObject?.staticPropertiesForJava(): List<DProperty> {
if (this == null) return emptyList()
return properties.filter { it.isJvmField || it.isConst || it.isLateInit }
return properties.filter {
it.isJvmField || it.isConst || it.isLateInit || it.isThisOrAccessorsStatic
}
}

internal fun DObject.companionInstancePropertyForJava(): DProperty? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ internal val DProperty.isJvmField: Boolean
internal val DFunction.isJvmStatic: Boolean
get() = jvmStatic() != null

internal val DProperty.isThisOrAccessorsStatic: Boolean
get() = jvmStatic() != null || listOfNotNull(getter, setter).all { it.isJvmStatic }

private fun DProperty.hasModifier(modifier: ExtraModifiers.KotlinOnlyModifiers): Boolean =
extra[AdditionalModifiers]
?.content
Expand Down Expand Up @@ -309,6 +312,7 @@ public class KotlinToJavaConverter(
.flatMap { property -> listOfNotNull(property.getter, property.setter) }
.plus(functions)
.plus(companion.staticFunctionsForJava())
.plus(companion.staticPropertyAccessorsForJava())
.filterNot { it.hasJvmSynthetic() }
.flatMap { it.asJava(it.dri.classNames ?: it.name) }

Expand Down Expand Up @@ -498,7 +502,7 @@ public class KotlinToJavaConverter(

return asJava(
excludedProps = staticPropertiesForJava(),
excludedFunctions = staticFunctionsForJava()
excludedFunctions = staticFunctionsForJava() + staticPropertyAccessorsForJava()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,35 @@ class CompanionAsJavaTest : BaseAbstractTest() {
}
}

@Test
fun `companion object with only jvmStatic property should not be rendered`() {
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|import kotlin.properties.Delegates
|
|class MyClass {
| companion object $COMPANION_NAME {
| @JvmStatic var delegatedProp: String by Delegates.notNull<String>()
| }
|}
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val parentClass = module.findClass("MyClass")

val getter = parentClass.findFunction("getDelegatedProp")
assertNotNull(getter, "Parent class should contains the companion jvmStatic getter")
assertIsStatic(getter)
val setter = parentClass.findFunction("setDelegatedProp")
assertNotNull(setter, "Parent class should contains the companion jvmStatic setter")
assertCompanionNotRendered(parentClass)
}
}
}

@Test
fun `companion object with nested classes is rendered`() {
testInline(
Expand Down