Skip to content

Commit

Permalink
refactor reflect usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed May 6, 2019
1 parent 54f01a1 commit 75ba1f4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions ktorm-core/src/main/kotlin/me/liuwj/ktorm/entity/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.lang.reflect.Proxy
import java.sql.SQLException
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure

/**
* 实体类必须实现的接口,为实体类注入通用的操作方法
Expand Down Expand Up @@ -113,8 +114,7 @@ interface Entity<E : Entity<E>> : Serializable {
*/
@Suppress("UNCHECKED_CAST")
operator fun invoke(): E {
val entityClass = referencedKotlinType.classifier as KClass<E>
return create(entityClass, null, null) as E
return create(referencedKotlinType.jvmErasure, null, null) as E
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package me.liuwj.ktorm.entity

import me.liuwj.ktorm.schema.*
import me.liuwj.ktorm.schema.Column
import me.liuwj.ktorm.schema.NestedBinding
import me.liuwj.ktorm.schema.ReferenceBinding
import me.liuwj.ktorm.schema.Table
import java.util.*
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmErasure

internal fun EntityImplementation.getPrimaryKeyValue(fromTable: Table<*>): Any? {
val primaryKey = fromTable.primaryKey ?: error("Table ${fromTable.tableName} doesn't have a primary key.")
Expand Down Expand Up @@ -42,7 +45,7 @@ internal fun EntityImplementation.setColumnValue(column: Column<*>, value: Any?,
is ReferenceBinding -> {
var child = this.getProperty(binding.onProperty.name) as Entity<*>?
if (child == null) {
child = Entity.create(binding.onProperty.returnType.classifier as KClass<*>, fromTable = binding.referenceTable)
child = Entity.create(binding.onProperty.returnType.jvmErasure, fromTable = binding.referenceTable)
this.setProperty(binding.onProperty.name, child, forceSet)
}

Expand All @@ -54,7 +57,7 @@ internal fun EntityImplementation.setColumnValue(column: Column<*>, value: Any?,
if (i != binding.properties.lastIndex) {
var child = curr.getProperty(prop.name) as Entity<*>?
if (child == null) {
child = Entity.create(prop.returnType.classifier as KClass<*>, parent = curr)
child = Entity.create(prop.returnType.jvmErasure, parent = curr)
curr.setProperty(prop.name, child, forceSet)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import me.liuwj.ktorm.expression.BinaryExpression
import me.liuwj.ktorm.expression.BinaryExpressionType
import me.liuwj.ktorm.expression.QuerySourceExpression
import me.liuwj.ktorm.schema.*
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmErasure

/**
* 根据 ID 批量获取实体对象,会自动 left join 所有的引用表
Expand Down Expand Up @@ -167,7 +167,7 @@ private fun QueryRowSet.retrieveColumn(column: Column<*>, intoEntity: Entity<*>,

when {
skipReferences -> {
val child = Entity.create(binding.onProperty.returnType.classifier as KClass<*>, fromTable = rightTable)
val child = Entity.create(binding.onProperty.returnType.jvmErasure, fromTable = rightTable)
child.implementation.setColumnValue(primaryKey, columnValue)
intoEntity[binding.onProperty.name] = child
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.util.*
import kotlin.collections.LinkedHashMap
import kotlin.collections.LinkedHashSet
import kotlin.reflect.KClass
import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.jvm.jvmName
import kotlin.reflect.jvm.kotlinFunction

Expand Down Expand Up @@ -72,7 +73,7 @@ internal class EntityImplementation(
if (result != null || prop.returnType.isMarkedNullable) {
return result
} else {
val defValue = (prop.returnType.classifier as KClass<*>).defaultValue
val defValue = prop.returnType.jvmErasure.defaultValue
this.setProperty(prop.name, defValue)
return defValue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.lang.reflect.Proxy
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure

@PublishedApi
internal class ColumnBindingHandler(val properties: MutableList<KProperty1<*, *>>) : InvocationHandler {
Expand All @@ -27,7 +28,7 @@ internal class ColumnBindingHandler(val properties: MutableList<KProperty1<*, *>

properties += prop

val returnType = prop.returnType.classifier as KClass<*>
val returnType = prop.returnType.jvmErasure
return when {
returnType.isSubclassOf(Entity::class) -> createProxy(returnType, properties)
returnType.java.isPrimitive -> returnType.defaultValue
Expand Down
14 changes: 4 additions & 10 deletions ktorm-core/src/main/kotlin/me/liuwj/ktorm/schema/Reflects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.javaSetter
import kotlin.reflect.jvm.jvmErasure
import kotlin.reflect.jvm.jvmName

/**
Expand Down Expand Up @@ -44,19 +45,12 @@ abstract class TypeReference<T> {
}

private fun findSuperclassTypeArgument(cls: KClass<*>): KType {
val supertype = cls.supertypes.first {
val rawClass = it.classifier as? KClass<*>
if (rawClass == null) {
false
} else {
!rawClass.java.isInterface
}
}
val supertype = cls.supertypes.first { !it.jvmErasure.java.isInterface }

if (supertype.arguments.isEmpty()) {
if (supertype.classifier != TypeReference::class) {
if (supertype.jvmErasure != TypeReference::class) {
// Try to climb up the hierarchy until meet something useful.
return findSuperclassTypeArgument(supertype.classifier as KClass<*>)
return findSuperclassTypeArgument(supertype.jvmErasure)
} else {
throw IllegalStateException("Could not find the referenced type of class $javaClass")
}
Expand Down
3 changes: 2 additions & 1 deletion ktorm-core/src/main/kotlin/me/liuwj/ktorm/schema/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure

/**
* SQL 数据表
Expand All @@ -30,7 +31,7 @@ open class Table<E : Entity<E>>(
private val _columns = LinkedHashMap<String, Column<*>>()
private var _primaryKeyName: String? = null

val entityClass: KClass<E>? = entityClass ?: (referencedKotlinType.classifier as? KClass<E>)?.takeIf { it != Nothing::class }
val entityClass: KClass<E>? = entityClass ?: (referencedKotlinType.jvmErasure as KClass<E>).takeIf { it != Nothing::class }

val columns: List<Column<*>> get() = _columns.values.toList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.time.LocalDate
import java.util.*
import kotlin.reflect.jvm.jvmErasure

/**
* Created by vince on Dec 09, 2018.
Expand All @@ -20,7 +21,7 @@ class EntityTest : BaseTest() {
fun testTypeReference() {
println(Employee)
println(Employee.referencedKotlinType)
assert(Employee.referencedKotlinType.classifier == Employee::class)
assert(Employee.referencedKotlinType.jvmErasure == Employee::class)

println(Employees)
println(Employees.entityClass)
Expand Down

0 comments on commit 75ba1f4

Please sign in to comment.