Skip to content

Commit 9f1b0d9

Browse files
Fixed #262 (#278)
1 parent 75c1a66 commit 9f1b0d9

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

compiler-plugin/src/main/kotlin/tech/mappie/ir/preprocessing/DefinitionsCollector.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import tech.mappie.ir.util.BaseVisitor
1616
import tech.mappie.exceptions.MappiePanicException.Companion.panic
1717
import tech.mappie.ir.resolving.MappieDefinition
1818
import tech.mappie.ir.resolving.RequestResolverContext
19+
import tech.mappie.ir.util.isSubclassOf
20+
import tech.mappie.ir.util.mappieSuperType
21+
import tech.mappie.ir.util.mappieType
1922

2023
// TODO: we should collect al publicly visible, and add those during resolving that are visible from the current scope.
2124
class DefinitionsCollector(val context: MappieContext) {
@@ -64,8 +67,10 @@ class ProjectMappieDefinitionsCollector(val context: MappieContext) : BaseVisito
6467
override fun visitClass(declaration: IrClass, data: Unit) =
6568
buildList {
6669
if (context.shouldGenerateCode(declaration)) {
67-
(declaration.superTypes.single() as? IrSimpleType)?.let {
68-
add(MappieDefinition(declaration))
70+
context(context) {
71+
declaration.mappieSuperType()?.let {
72+
add(MappieDefinition(declaration))
73+
}
6974
}
7075
}
7176
addAll(declaration.declarations.filterIsInstance<IrClass>().flatMap { it.accept(data) })

compiler-plugin/src/main/kotlin/tech/mappie/ir/util/Ir.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ import org.jetbrains.kotlin.ir.types.getClass
2121
import org.jetbrains.kotlin.ir.util.SYNTHETIC_OFFSET
2222
import org.jetbrains.kotlin.ir.util.isNullable
2323
import org.jetbrains.kotlin.ir.util.erasedUpperBound
24+
import org.jetbrains.kotlin.ir.util.isStrictSubtypeOfClass
2425
import org.jetbrains.kotlin.name.Name
26+
import tech.mappie.MappieContext
27+
import tech.mappie.referenceMappieClass
2528
import tech.mappie.util.*
2629

30+
context (context: MappieContext)
31+
fun IrClass.mappieSuperType(): IrType? =
32+
allSuperTypes().singleOrNull { it.isStrictSubtypeOfClass(context.referenceMappieClass()) }
33+
2734
fun IrClass.isSubclassOf(clazz: IrClassSymbol) =
2835
allSuperTypes().any { it.erasedUpperBound == clazz.defaultType.getClass()!! }
2936

compiler-plugin/src/test/kotlin/tech/mappie/testing/MapperInheritanceTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@ class MapperInheritanceTest : MappieTestCase() {
3030
.isEqualTo(Output("test"))
3131
}
3232
}
33+
34+
@Test
35+
fun `inherit with interface mapper should succeed`() {
36+
compile {
37+
file("Mapper.kt",
38+
"""
39+
import tech.mappie.api.ObjectMappie
40+
import tech.mappie.testing.MapperInheritanceTest.*
41+
42+
class Mapper : ObjectMappie<Input, Output>(), AutoCloseable {
43+
override fun close() { }
44+
}
45+
"""
46+
)
47+
} satisfies {
48+
isOk()
49+
hasNoWarningsOrErrors()
50+
51+
assertThat(objectMappie<Input, Output>().map(Input("test")))
52+
.isEqualTo(Output("test"))
53+
}
54+
}
3355
}

website/src/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ changelog:
99
- "Added support for kotlinx-datetime to JVM mappers, which can be imported via a separate dependency."
1010
- "Fixed check of runtime usage of of mapping DSL for multiple object mappie."
1111
- "[#264](https://github.com/Mr-Mappie/mappie/issues/264) Fix compilation error with ObjectMappie5."
12+
- "[#262](https://github.com/Mr-Mappie/mappie/issues/262) Fixed compilation error when mapper inherits from more than a Mappie subclass."
1213
- "Minor performance enhancements - by @ErwinOlie."
1314
- "Fix for missing reporting when both a mapper and its generated mapper have issues - by @ErwinOlie."
1415
- "Improved reporting of type parameters - by @ErwinOlie."

0 commit comments

Comments
 (0)