List<String>
当使用 Kotlin 处理 Firebase 数据库时,如果我使用GenericTypeIndicator
如下a ,我似乎无法检索类型的值:
snap.getValue(object : GenericTypeIndicator<List<String>>() {})
它会从 Firebase SDK 产生一个异常,如下所示:
com.google.firebase.database.DatabaseException: Generic wildcard types are not supported
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
但是,如果我从 Java 调用它,如下所示,它可以工作:
snap.getValue(new GenericTypeIndicator<List<String>>() {})
我猜测它与类型具体化有关,所以我这样做了:
inline fun <reified T> genericType() = object: GenericTypeIndicator<T>() {}
val stringListIndicator = genericType<List<String>>()
snap.getValue(stringListIndicator)
但同样的例外发生了。
为什么会这样?
编辑:我尝试使用 jadx-0.6.1 反编译 Java 和 Kotlin 版本。
Java源码:
public class Randommmm {
private static final GenericTypeIndicator<List<String>> ti = new GenericTypeIndicator<List<String>>() {
};
public static List<String> x(DataSnapshot snap) {
return snap.getValue(ti);
}
}
反编译:
public class Randommmm {
private static final GenericTypeIndicator<List<String>> ti = new C12761();
static class C12761 extends GenericTypeIndicator<List<String>> {
C12761() {
}
}
public static List<String> m48x(DataSnapshot snap) {
return (List) snap.getValue(ti);
}
}
Kotlin 源码(一):
object Randommmm {
private val ti = object : GenericTypeIndicator<List<String>>() {
}
fun x(snap: DataSnapshot): List<String> {
return snap.getValue(ti)
}
}
反编译:
public final class Randommmm {
public static final Randommmm INSTANCE = null;
private static final Randommmm$ti$1 ti = null;
static class C12761 extends GenericTypeIndicator<List<String>> {
C12761() {
}
}
static {
Randommmm randommmm = new Randommmm();
}
private Randommmm() {
INSTANCE = this;
ti = new Randommmm$ti$1();
}
@NotNull
public final List<String> m48x(@NotNull DataSnapshot snap) {
Intrinsics.checkParameterIsNotNull(snap, "snap");
Object value = snap.getValue(ti);
Intrinsics.checkExpressionValueIsNotNull(value, "snap.getValue(ti)");
return (List) value;
}
}
public final class Randommmm$ti$1 extends GenericTypeIndicator<List<? extends String>> {
Randommmm$ti$1() {
}
}
ArrayList
按照 Doug 的建议使用 Kotlin 源代码 (2) :
object Randommmm {
private val ti = object : GenericTypeIndicator<ArrayList<String>>() {
}
fun x(snap: DataSnapshot): List<String> {
return snap.getValue(ti)
}
}
反编译:
public final class Randommmm {
public static final Randommmm INSTANCE = null;
private static final Randommmm$ti$1 ti = null;
static class C12761 extends GenericTypeIndicator<List<String>> {
C12761() {
}
}
static {
Randommmm randommmm = new Randommmm();
}
private Randommmm() {
INSTANCE = this;
ti = new Randommmm$ti$1();
}
@NotNull
public final List<String> m48x(@NotNull DataSnapshot snap) {
Intrinsics.checkParameterIsNotNull(snap, "snap");
Object value = snap.getValue(ti);
Intrinsics.checkExpressionValueIsNotNull(value, "snap.getValue(ti)");
return (List) value;
}
}
public final class Randommmm$ti$1 extends GenericTypeIndicator<ArrayList<String>> {
Randommmm$ti$1() {
}
}