Skip to content

Commit

Permalink
Support companion objects
Browse files Browse the repository at this point in the history
  • Loading branch information
natario1 committed Sep 8, 2024
1 parent 0acb0bc commit acb5242
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion knee-compiler-plugin/src/main/kotlin/utils/PoetUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ fun IrClass.asTypeSpec(rename: ((String) -> String)? = null): TypeSpec.Builder {
val name = codegenName.map { rename?.invoke(it) ?: it }.asString()
return when (kind) {
ClassKind.ENUM_CLASS -> TypeSpec.enumBuilder(name)
ClassKind.OBJECT -> TypeSpec.objectBuilder(name)
ClassKind.OBJECT -> when {
isCompanion -> TypeSpec.companionObjectBuilder(if (name == "Companion") null else name)
else -> TypeSpec.objectBuilder(name)
}
ClassKind.INTERFACE -> TypeSpec.interfaceBuilder(name)
ClassKind.ANNOTATION_CLASS -> TypeSpec.annotationBuilder(name)
ClassKind.ENUM_ENTRY -> error("Enum entries ($this) can't become a TypeSpec.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ class ObjectTests {
check(TopLevelObject.value == 15) { TopLevelObject.value }
}

@Test
fun testInner_property() {
ObjectParent.InnerObject.value = 15
check(ObjectParent.InnerObject.value == 15) { ObjectParent.InnerObject.value }
}

@Test
fun testCompanion_property() {
ObjectParent.value = 15
check(ObjectParent.value == 15) { ObjectParent.value }
}

}
10 changes: 10 additions & 0 deletions tests/test-classes/src/backendMain/kotlin/ObjectDefinitions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ object TopLevelObject {
@Knee fun decrement() { value -= 1 }
@Knee override fun toString(): String = "TopLevelObject($value)"
}

class ObjectParent {
@KneeObject object InnerObject {
@Knee var value: Int = 0
}

@KneeObject companion object {
@Knee var value: Int = 0
}
}

0 comments on commit acb5242

Please sign in to comment.