Jinq在Kotlin – 如何将lambda转换为java SerializedLambda?
我可以在Kotlin有可序列化的lambda吗? 我正在尝试使用Kotlin的Jinq库,但它需要可序列化的lambda表达式。 有没有可能的语法?
更新:
我的代码:
var temp=anyDao.streamAll(Task::class.java) .where<Exception,Task> { t->t.taskStatus== TaskStatus.accepted } .collect(Collectors.toList<Task>());
我得到这个错误:
Caused by: java.lang.IllegalArgumentException: Could not extract code from lambda. This error sometimes occurs because your lambda references objects that aren't Serializable.
在lambda中引用的所有对象都是可序列化的(代码在java中没有错误)。
更新2
调试后,似乎kotlin lambda没有被转换成Jinq所需的java.lang.invoke.SerializedLambda来获取信息。 所以问题是如何将其转换为SerializedLambda。
我对Jinq没有经验,但根据GitHub的实现和我在Kotlin中使用Java库的经验。
ref: https : //github.com/my2iu/Jinq/blob/master/api/src/org/jinq/orm/stream/JinqStream.java
你可以回头使用Kotlin中的原生Java接口。
var temp = anyDao.streamAll(Task::class.java) .where( JinqStream.Where<Task,Exception> { t -> t.taskStatus == TaskStatus.accepted } ) .collect(Collectors.toList<Task>()); // Alternatively, You you can import the interface first import org.jinq.orm.stream.JinqStream.* ... // then you can use Where instead of JinqStream.Where var temp = anyDao.streamAll(Task::class.java) .where(Where<Task,Exception> { t -> t.taskStatus == TaskStatus.accepted } ) .collect(Collectors.toList<Task>());
或者做一个自定义的扩展来包装实现
fun JinqStream<T>.where(f: (T) -> Boolean): JinqStream<T> { return this.where(JinqStream.Where<T,Exception> { f(it) }) }
免责声明:以上代码尚未经过测试。