保存Firestore对象会引发InvocationTargetException错误 [英] Saving a Firestore object throws InvocationTargetException error

查看:78
本文介绍了保存Firestore对象会引发InvocationTargetException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试这个问题花了我大约一个小时的时间,所以我想知道是否有人能够解释这里发生的事情.

It's taken me around an hour to debug this issue so I was wondering whether someone would be able to explain what's going on here.

我正在尝试在Android上(在Kotlin中)保存Firestore自定义对象.这是简化版-

I'm trying to save a Firestore custom object on Android (in Kotlin). Here's a simplified version -

class MockTransaction {

    val endpoint: String = "mockTransactions"

    var fromAddress: String = ""
    var toAddress: String = ""
    var amount: Double = 0.0

    constructor() {}

    constructor(from: String, to: String, amount: Double) {
        this.fromAddress = from
        this.toAddress = to
        this.amount = amount
    }

    fun isValid(): Boolean {
        return true
    }

    fun save() {
        val db = FirebaseFirestore.getInstance()
        db.collection(endpoint).add(this).addOnCompleteListener {
            Log.d("TX", it.isSuccessful.toString() )
            Log.d("TX", it.exception.toString())
        }
    }

}

val tx = MockTransaction("Alice", "Bob", 100.0)
tx.save()

现在,如果我更改isValid函数以引发类似这样的异常

Now if I change the isValid function to throw an exception like so

fun isValid(): Boolean {
    throw TransactionException("No signature found")
}

抛出错误的保存方法

java.lang.reflect.InvocationTargetException

java.lang.reflect.InvocationTargetException

如果我将函数 name 更改为简单的fun valid(),则保存有效.

If I change the function name to simply fun valid() then the save works.

我只是想知道为什么函数名称很重要?我也尝试将其更改为fun isARadioactiveSpider()之类的内容,但您遇到相同的错误.

I'm just curious as to why the name of the function matters? I've also tried changing it to something like fun isARadioactiveSpider() and you get the same error.

推荐答案

firebase类CustomClassMapper具有以下方法:

A firebase class CustomClassMapper has a method:

private static boolean shouldIncludeGetter(Method method) {
    if (!method.getName().startsWith("get") && !method.getName().startsWith("is")) {
        return false;
    } else if (method.getDeclaringClass().equals(Object.class)) {
        return false;
    } else if (!Modifier.isPublic(method.getModifiers())) {
        return false;
    } else if (Modifier.isStatic(method.getModifiers())) {
        return false;
    } else if (method.getReturnType().equals(Void.TYPE)) {
        return false;
    } else if (method.getParameterTypes().length != 0) {
        return false;
    } else {
        return !method.isAnnotationPresent(Exclude.class);
    }
}

因此,反射用于序列化类似于getter的方法.稍后,将使用反射调用这些方法.并且此调用引发了一些异常,该异常隐藏在InvocationTargetException

So reflection is used to serialize methods that look like getters. Later these methods will be invoked using reflection. And this invocation throws some exception, which is hidden in InvocationTargetException

因此,要避免使用InvocationTargetException,只需检查方法是否抛出异常,因为它们是在序列化期间调用的

So, to avoid the InvocationTargetException, just check that methods don't throw exceptions, because they are invoked during the serialization

这篇关于保存Firestore对象会引发InvocationTargetException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆