如何测试通过反射获得的Java字段的值是否为null? [英] How to test whether the value of a Java field gotten by reflection is null?
本文介绍了如何测试通过反射获得的Java字段的值是否为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
Field f = this.getClass().getFields()[0];
我需要知道 this
中 f
的值是否为 null
.有很多方法,例如 getInt()
和 getDouble()
,但是我还没有找到类似 Object getData()
或的方法.isNull()
.有这种方法吗?
I need to know if f
's value in this
is null
or not. There are many methods like getInt()
and getDouble()
, but I have not found a method like Object getData()
or isNull()
. Is there such a method?
推荐答案
field.get(target)
returns Object
. So you can checkif (field.get(this) == null) {..}
如果该字段是原始字段,它将被包装. int
-> 整数
, char
-> 字符
等
If the field is primitive, it will get wrapped. int
-> Integer
, char
-> Character
, etc.
这篇关于如何测试通过反射获得的Java字段的值是否为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文