有没有办法像 isPrimitive() 一样查找 Field 是否为布尔值?

发布于 2024-12-27 04:10:10 字数 324 浏览 6 评论 0原文

有没有一种方法可以像 isPrimitive() 一样在 Java 反射中查找 Field 是否为 boolean

Field fieldlist[] = clazz.getDeclaredFields();
for (int i = 0; fieldlist.length & gt; i; i++) {
 Field fld = fieldlist[i];
 if (fld.getClass().isPrimitive()) {
  fld.setInt(object, 0);
  continue;
 }
}

Is there a way to find if a Field is boolean in Java reflection the same as isPrimitive()?

Field fieldlist[] = clazz.getDeclaredFields();
for (int i = 0; fieldlist.length & gt; i; i++) {
 Field fld = fieldlist[i];
 if (fld.getClass().isPrimitive()) {
  fld.setInt(object, 0);
  continue;
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半枫 2025-01-03 04:10:10
if(fld.getType().equals(boolean.class))

刚刚对此进行了测试,它适用于原始布尔变量。

if(fld.getType().equals(boolean.class))

Just tested this and it works for primitive boolean variables.

白日梦 2025-01-03 04:10:10

我相信 Boolean.class.isAssignableFrom(fld.getClass()) 可用于确定该字段是否为布尔值。不过,我还没有机会测试这是否适用于基元。

I believe Boolean.class.isAssignableFrom(fld.getClass()) can be used to determine if the field is a boolean. I haven't had a chance to test whether this works for primitives though.

别把无礼当个性 2025-01-03 04:10:10

试试这个(

public boolean getBoolean(Object obj)
               throws IllegalArgumentException,
                      IllegalAccessException

Gets the value of a static or instance boolean field.

Parameters:
    obj - the object to extract the boolean value from 
Returns:
    the value of the boolean field 
Throws:
    IllegalAccessException - if the underlying field is inaccessible. 
    IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), **or if the field value cannot be converted to the type boolean** by a widening conversion. 
    NullPointerException - if the specified object is null and the field is an instance field. 
    ExceptionInInitializerError - if the initialization provoked by this method fails.

Try this (reference):

public boolean getBoolean(Object obj)
               throws IllegalArgumentException,
                      IllegalAccessException

Gets the value of a static or instance boolean field.

Parameters:
    obj - the object to extract the boolean value from 
Returns:
    the value of the boolean field 
Throws:
    IllegalAccessException - if the underlying field is inaccessible. 
    IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), **or if the field value cannot be converted to the type boolean** by a widening conversion. 
    NullPointerException - if the specified object is null and the field is an instance field. 
    ExceptionInInitializerError - if the initialization provoked by this method fails.
—━☆沉默づ 2025-01-03 04:10:10

这适用于原语和对象:

boolean isBooleanType = field.getType().equals(boolean.class) || field.getType().equals(Boolean.class);

This is for primitive and object:

boolean isBooleanType = field.getType().equals(boolean.class) || field.getType().equals(Boolean.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文