Skip to content

Commit b63332b

Browse files
check abstract class
1 parent e35ff78 commit b63332b

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/parser/MetadataParser.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,25 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
7373
}
7474

7575
fun parseTableMetadata(cls: KSClassDeclaration): TableMetadata {
76-
val r = _tablesCache[cls.qualifiedName!!.asString()]
76+
val className = cls.qualifiedName!!.asString()
77+
val r = _tablesCache[className]
7778
if (r != null) {
7879
return r
7980
}
8081

8182
if (cls.classKind != CLASS && cls.classKind != INTERFACE) {
82-
val name = cls.qualifiedName!!.asString()
83-
throw IllegalStateException("$name is expected to be a class or interface but actually ${cls.classKind}.")
83+
throw IllegalStateException("$className should be a class or interface but actually ${cls.classKind}.")
8484
}
8585

8686
if (cls.classKind == INTERFACE && !cls.isSubclassOf<Entity<*>>()) {
87-
val name = cls.qualifiedName!!.asString()
88-
throw IllegalStateException("$name must extend from org.ktorm.entity.Entity.")
87+
throw IllegalStateException("$className must extend from org.ktorm.entity.Entity.")
8988
}
9089

91-
_logger.info("[ktorm-ksp-compiler] parse table metadata from entity: ${cls.qualifiedName!!.asString()}")
90+
if (cls.classKind == CLASS && cls.isAbstract()) {
91+
throw IllegalStateException("$className cannot be an abstract class.")
92+
}
93+
94+
_logger.info("[ktorm-ksp-compiler] parse table metadata from entity: $className")
9295
val table = cls.getAnnotationsByType(Table::class).first()
9396
val tableDef = TableMetadata(
9497
entityClass = cls,
@@ -111,7 +114,7 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
111114
}
112115
}
113116

114-
_tablesCache[cls.qualifiedName!!.asString()] = tableDef
117+
_tablesCache[className] = tableDef
115118
return tableDef
116119
}
117120

ktorm-ksp-compiler/src/test/kotlin/org/ktorm/ksp/compiler/parser/MetadataParserTest.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.ktorm.ksp.compiler.BaseKspTest
99
class MetadataParserTest : BaseKspTest() {
1010

1111
@Test
12-
fun testEnumClass() = kspFailing("Gender is expected to be a class or interface but actually ENUM_CLASS.", """
12+
fun testEnumClass() = kspFailing("Gender should be a class or interface but actually ENUM_CLASS.", """
1313
@Table
1414
enum class Gender { MALE, FEMALE }
1515
""".trimIndent())
@@ -23,6 +23,15 @@ class MetadataParserTest : BaseKspTest() {
2323
}
2424
""".trimIndent())
2525

26+
@Test
27+
fun testAbstractClass() = kspFailing("User cannot be an abstract class.", """
28+
@Table
29+
abstract class User {
30+
var id: Int = 0
31+
var name: String = ""
32+
}
33+
""".trimIndent())
34+
2635
@Test
2736
fun testClassIgnoreProperties() = runKotlin("""
2837
@Table(ignoreProperties = ["name"])

0 commit comments

Comments
 (0)