无法通过reflection设置布尔值

我无法使用Javareflection为字段设置Boolean值。 字段数据类型是java.lang.Boolean 。 但是,如果数据类型是基本类型,即boolean值,我可以设置值。

这是一个带有Boolean类型和基本类型的简单VO:

 public class TestVO { private Boolean bigBoolean; private boolean smallBoolean; } 

这是我的javareflection代码:

 public class TestClass { public static void main(String args[]) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { TestVO testVO1 = new TestVO(); Class testVO = testVO1.getClass(); Field smallBooleanField = TestVO.class.getDeclaredField("smallBoolean"); Field bigBooleanField = TestVO.class.getDeclaredField("bigBoolean"); String name1 = smallBooleanField.getName(); System.out.println("SmallBoolean Fieldname is: " + name1); smallBooleanField.setAccessible(true); // get the value of this private field Boolean fieldValue = (Boolean) smallBooleanField.get(testVO1); System.out.println("fieldValue = " + fieldValue); smallBooleanField.setAccessible(true); smallBooleanField.setBoolean(testVO1, true); // get the value of this private field fieldValue = (Boolean) smallBooleanField.get(testVO1); System.out.println("fieldValue = " + fieldValue); name1 = bigBooleanField.getName(); System.out.println("bigBooleanField Fieldname is: " + name1); bigBooleanField.setAccessible(true); // get the value of this private field fieldValue = (Boolean) bigBooleanField.get(testVO1); System.out.println("fieldValue = " + fieldValue); bigBooleanField.setAccessible(true); bigBooleanField.setBoolean(testVO1, new Boolean(true)); // get the value of this private field fieldValue = (Boolean) bigBooleanField.get(testVO1); System.out.println("fieldValue = " + fieldValue); } } 

输出是:

 SmallBoolean Fieldname is: smallBoolean fieldValue = false fieldValue = true bigBooleanField Fieldname is: bigBoolean fieldValue = null Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.Boolean field TestVO.bigBoolean to (boolean)true at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:175) at sun.reflect.UnsafeObjectFieldAccessorImpl.setBoolean(UnsafeObjectFieldAccessorImpl.java:90) at java.lang.reflect.Field.setBoolean(Field.java:795) at TestClass.main(TestClass.java:44) 

我试图用new Boolean(true)Boolean.TRUEtrue等设置bigBoolean值。没有任何作用。

根据这个 ,调用bigBoolean.setBoolean()来设置一个具有基本类型值的引用类型为Boolean的字段。 在非reflection等价物Boolean val = true ,编译器会将原始类型“true”转换(或框)为引用类型为new Boolean(True)以便其类型检查将接受该语句。

使用reflection时,类型检查仅在运行时进行,因此无法对值进行装箱。 由于Inconvertible Types,这会强制抛出IllegalArgumentException

改变这一行,它应该适合你:

 bigBooleanField.set(testVO1, Boolean.TRUE); 

我面临同样的问题而且理由不是reflection原因是getter方法类型。 如果您注意到boolean / Boolean,则IDE会创建一个getter方法,如isVariableName()而不是getVariableName()。 如果您更改方法名称以获取类型,它将正常工作。