想写一个数据库Dao接口的通用类BaseDao,就不用每个数据表都写很多一样的方法了。然后发现无法区分传入的 $tableName ,在网上找了一圈发现以下代码,我运行的时候发生错误,获取的object转换后为null。想问问大家是怎么获取的?
interface BaseDao<T> {
fun loadAll(): List<T> {
val query = SimpleSQLiteQuery("select * from $tableName")
return doFindAll(query)
}
val tableName: String
get() {
val clazz = (javaClass.superclass?.genericSuperclass as ParameterizedType)
.actualTypeArguments[0] as Class<*>
val tableName = clazz.simpleName
Log.d("ez", "getTableName: -->$tableName")
return tableName
}
}
运行时发现是这里获取了null值。是获取错了还是没有获取到?
val clazz = (javaClass.superclass?.genericSuperclass as ParameterizedType)
.actualTypeArguments[0] as Class<*>