android - 如何引用泛型类型的变量?
问题描述
我正在尝试为 Room 库实现“基本 DAO”接口,以避免样板代码:
BaseEntity.kt
interface BaseEntity {
val entityName: String
}
笔记.kt
@Entity
class Note : BaseEntity {
override val entityName: String = "note"
...
}
BaseDao.kt
interface BaseDao<T : BaseEntity> {
@Query("SELECT * FROM ${T.entityName}")
fun selectAll(): List<T>
...
}
NoteDao.kt
@Dao
interface NoteDao : BaseDao<Note> {
...
}
但是,表达式${T.entityName}
无效。有没有办法做到这一点?
解决方案
我不相信您可以实现“基本 DAO”接口。原因是Room 在编译时创建了每个 DAO 实现。因此,为什么您会收到消息An annotation argument must be a compile time-constant。
Room 需要从注解中(例如)知道哪些表列映射到哪些变量以及用于执行映射的方法,以便可以生成底层代码。
例如,如果实体和道是:-
@Entity
class Note {
@PrimaryKey
var entityName: String = ""
}
和
@Dao
interface BaseDao {
@Query("SELECT * FROM Note")
fun selectAll(): List<Note>
}
那么底层生成的java将是: -
public final class BaseDao_Impl implements BaseDao {
private final RoomDatabase __db;
public BaseDao_Impl(RoomDatabase __db) {
this.__db = __db;
}
@Override
public List<Note> selectAll() {
final String _sql = "SELECT * FROM Note";
final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
__db.assertNotSuspendingTransaction();
final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
try {
final int _cursorIndexOfEntityName = CursorUtil.getColumnIndexOrThrow(_cursor, "entityName");
final List<Note> _result = new ArrayList<Note>(_cursor.getCount());
while(_cursor.moveToNext()) {
final Note _item;
_item = new Note();
final String _tmpEntityName;
_tmpEntityName = _cursor.getString(_cursorIndexOfEntityName);
_item.setEntityName(_tmpEntityName);
_result.add(_item);
}
return _result;
} finally {
_cursor.close();
_statement.release();
}
}
}
推荐阅读
- dynamics-crm - 您可以在另一个中使用来自一个 Dynamics CRM 365 实体的字段吗?
- javascript - 如何隐藏两组不同同位素元素的标题
- python - 不使用任何 Numpy 或 Sklearn 库手动计算 AUC
- office-ui-fabric - 使用 office-ui-fabric-react 组件将所有样式导出到 css 文件
- micropython - 为什么我的 microbit 会显示这个动画?
- xml - boost property_tree:遍历重复子元素中重复子元素的属性
- javascript - 云函数 - 像对象值一样访问 json
- python-3.x - 使用 Python3 选择性地处理 ANTLR4 中的空格
- apache-spark - 如何使用 Spark 修复 parquet 文件分区中的不一致模式
- sql - 在 sql server 中存储链接到客户端的大列表的最佳方法