Skip to content

Commit

Permalink
check abstract class
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed May 5, 2024
1 parent e35ff78 commit b63332b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,25 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
}

fun parseTableMetadata(cls: KSClassDeclaration): TableMetadata {
val r = _tablesCache[cls.qualifiedName!!.asString()]
val className = cls.qualifiedName!!.asString()
val r = _tablesCache[className]
if (r != null) {
return r
}

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

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

_logger.info("[ktorm-ksp-compiler] parse table metadata from entity: ${cls.qualifiedName!!.asString()}")
if (cls.classKind == CLASS && cls.isAbstract()) {
throw IllegalStateException("$className cannot be an abstract class.")
}

_logger.info("[ktorm-ksp-compiler] parse table metadata from entity: $className")
val table = cls.getAnnotationsByType(Table::class).first()
val tableDef = TableMetadata(
entityClass = cls,
Expand All @@ -111,7 +114,7 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
}
}

_tablesCache[cls.qualifiedName!!.asString()] = tableDef
_tablesCache[className] = tableDef
return tableDef
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.ktorm.ksp.compiler.BaseKspTest
class MetadataParserTest : BaseKspTest() {

@Test
fun testEnumClass() = kspFailing("Gender is expected to be a class or interface but actually ENUM_CLASS.", """
fun testEnumClass() = kspFailing("Gender should be a class or interface but actually ENUM_CLASS.", """
@Table
enum class Gender { MALE, FEMALE }
""".trimIndent())
Expand All @@ -23,6 +23,15 @@ class MetadataParserTest : BaseKspTest() {
}
""".trimIndent())

@Test
fun testAbstractClass() = kspFailing("User cannot be an abstract class.", """
@Table
abstract class User {
var id: Int = 0
var name: String = ""
}
""".trimIndent())

@Test
fun testClassIgnoreProperties() = runKotlin("""
@Table(ignoreProperties = ["name"])
Expand Down

0 comments on commit b63332b

Please sign in to comment.