Skip to content

Commit 1be3b09

Browse files
committed
refactor(SuperTableClass): rename
1 parent 878526a commit 1be3b09

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
9797

9898
_logger.info("[ktorm-ksp-compiler] parse table metadata from entity: $className")
9999
val table = cls.getAnnotationsByType(Table::class).first()
100-
val (superClass, superTableClasses) = parseSuperTableClass(cls)
101-
val allPropertyNamesOfSuperTables = superTableClasses.flatMap { it.getProperties(emptySet()) }.map { it.simpleName.asString() }
100+
101+
val (finalSuperClass, allSuperTableClasses) = parseSuperTableClass(cls)
102+
val shouldIgnorePropertyNames = allSuperTableClasses.flatMap { it.getProperties(emptySet()) }.map { it.simpleName.asString() }
102103

103104
val tableMetadata = TableMetadata(
104105
entityClass = cls,
@@ -108,9 +109,9 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
108109
schema = table.schema.ifEmpty { _options["ktorm.schema"] }?.takeIf { it.isNotEmpty() },
109110
tableClassName = table.className.ifEmpty { _codingNamingStrategy.getTableClassName(cls) },
110111
entitySequenceName = table.entitySequenceName.ifEmpty { _codingNamingStrategy.getEntitySequenceName(cls) },
111-
ignoreProperties = table.ignoreProperties.toSet() + allPropertyNamesOfSuperTables, // ignore properties of super tables
112+
ignoreProperties = table.ignoreProperties.toSet() + shouldIgnorePropertyNames,
112113
columns = ArrayList(),
113-
superClass = superClass
114+
superClass = finalSuperClass
114115
)
115116

116117
val columns = tableMetadata.columns as MutableList
@@ -283,14 +284,14 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
283284
}
284285

285286
/**
286-
* @return the super table class and all class be annotated with [SuperTableClass] in the inheritance hierarchy.
287+
* @return the final super table class and all super table classes in the inheritance hierarchy.
287288
*/
288289
@OptIn(KotlinPoetKspPreview::class)
289290
private fun parseSuperTableClass(cls: KSClassDeclaration): Pair<ClassName, Set<KSClassDeclaration>> {
290-
val superTableClassAnnPair = cls.findAllAnnotationsInInheritanceHierarchy(SuperTableClass::class.qualifiedName!!)
291+
val entityAnnPairs = cls.findAnnotationsInHierarchy(SuperTableClass::class.qualifiedName!!)
291292

292293
// if there is no SuperTableClass annotation, return the default super table class based on the class kind.
293-
if (superTableClassAnnPair.isEmpty()) {
294+
if (entityAnnPairs.isEmpty()) {
294295
return if (cls.classKind == INTERFACE) {
295296
org.ktorm.schema.Table::class.asClassName() to emptySet()
296297
} else {
@@ -299,13 +300,13 @@ internal class MetadataParser(resolver: Resolver, environment: SymbolProcessorEn
299300
}
300301

301302
// SuperTableClass annotation can only be used on interface
302-
if (superTableClassAnnPair.map { it.first }.any { it.classKind != INTERFACE }) {
303+
if (entityAnnPairs.map { it.first }.any { it.classKind != INTERFACE }) {
303304
val msg = "SuperTableClass annotation can only be used on interface."
304305
throw IllegalArgumentException(msg)
305306
}
306307

307308
// find the last annotation in the inheritance hierarchy
308-
val superTableClasses = superTableClassAnnPair
309+
val superTableClasses = entityAnnPairs
309310
.map { it.second }
310311
.map { it.arguments.single { it.name?.asString() == SuperTableClass::value.name } }
311312
.map { it.value as KSType }

ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/util/KspExtensions.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,18 @@ internal fun KSClassDeclaration.findSuperTypeReference(name: String): KSTypeRefe
8787
* Find all annotations with the given name in the inheritance hierarchy of this class.
8888
*
8989
* @param name the qualified name of the annotation.
90+
* @return a list of pairs, each pair contains the class declaration and the annotation.
9091
*/
91-
internal fun KSClassDeclaration.findAllAnnotationsInInheritanceHierarchy(name: String): List<Pair<KSClassDeclaration, KSAnnotation>> {
92-
val rst = mutableListOf<Pair<KSClassDeclaration, KSAnnotation>>()
92+
internal fun KSClassDeclaration.findAnnotationsInHierarchy(name: String): List<Pair<KSClassDeclaration, KSAnnotation>> {
93+
val pairs = mutableListOf<Pair<KSClassDeclaration, KSAnnotation>>()
9394

9495
fun KSClassDeclaration.collectAnnotations() {
95-
rst += annotations.filter { it.annotationType.resolve().declaration.qualifiedName?.asString() == name }.map { this to it }
96+
pairs += annotations.filter { it.annotationType.resolve().declaration.qualifiedName?.asString() == name }.map { this to it }
9697
superTypes.forEach { (it.resolve().declaration as KSClassDeclaration).collectAnnotations() }
9798
}
9899

99100
collectAnnotations()
100-
return rst
101+
return pairs
101102
}
102103

103104

ktorm-ksp-compiler/src/test/kotlin/org/ktorm/ksp/compiler/generator/SuperTableClassTest.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ class SuperTableClassTest : BaseKspTest() {
213213
interface CEntity : AEntity<CEntity>, BEntity<CEntity> {
214214
var c: Int
215215
}
216-
217-
fun run() {
218-
assert(CTable::class.isSubclassOf(ATable::class))
219-
}
220216
""".trimIndent()
221217
)
222218

0 commit comments

Comments
 (0)