无法通过反射设置布尔值 [英] Cannot set Boolean value through reflection
问题描述
我无法使用Java反射为字段设置 Boolean
值。
字段数据类型是 java.lang.Boolean
。但是,如果数据类型是基本类型,我能够设置值,即 boolean
。
这是一个简单的VO与布尔
类型和基元类型:
public class TestVO {
private Boolean bigBoolean;
private boolean smallBoolean;
}
这是我的java反射代码:
'pre>
公共类识别TestClass {
公共静态无效主要(字符串ARGS [])
抛出:IllegalArgumentException - ,IllegalAccessException,NoSuchFieldException,抛出:SecurityException {
TestVO testVO1 = new TestVO();
类testVO = testVO1.getClass();
字段smallBooleanField = TestVO.class.getDeclaredField(smallBoolean);
字段bigBooleanField = TestVO.class.getDeclaredField(bigBoolean);
String name1 = smallBooleanField.getName();
System.out.println(SmallBoolean Fieldname is:+ name1);
smallBooleanField.setAccessible(true);
//获取此私有字段的值
Boolean fieldValue =(Boolean)smallBooleanField.get(testVO1);
System.out.println(fieldValue =+ fieldValue);
smallBooleanField.setAccessible(true);
smallBooleanField.setBoolean(testVO1,true);
//获取此私有字段的值
fieldValue =(Boolean)smallBooleanField.get(testVO1);
System.out.println(fieldValue =+ fieldValue);
name1 = bigBooleanField.getName();
System.out.println(bigBooleanField Fieldname is:+ name1);
bigBooleanField.setAccessible(true);
//获取此私有字段的值
fieldValue =(Boolean)bigBooleanField.get(testVO1);
System.out.println(fieldValue =+ fieldValue);
bigBooleanField.setAccessible(true);
bigBooleanField.setBoolean(testVO1,new Boolean(true));
//获取此私有字段的值
fieldValue =(Boolean)bigBooleanField.get(testVO1);
System.out.println(fieldValue =+ fieldValue);
}
}
输出为:
SmallBoolean Fieldname是:smallBoolean
fieldValue = false
fieldValue = true
bigBooleanField字段名是:螺纹bigBoolean
fieldValue方法= NULL
异常 主 java.lang.IllegalArgumentException异常:无法设置一个java.lang.Boolean场TestVO.bigBoolean到(布尔型)
。在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)
我试图用<设置 bigBoolean
值code> new Boolean(true), Boolean.TRUE
, true
等无效。
根据这个, bigBoolean.setBoolean()
被调用来设置一个具有基本类型值的引用类型为Boolean的字段。在非反射等价物布尔值val = true
中,编译器会将原始类型true转换(或框)为引用类型 new Boolean(True)
,以便其类型检查将接受该语句。
使用反射时,类型检查仅在运行时进行,因此没有机会设置值。由于不可转换类型,这会强制抛出 IllegalArgumentException
I am not able to set a Boolean
value to a field using Java reflection.
The field data type is java.lang.Boolean
. however, I am able to set the value if the data type is primitive type i.e boolean
.
Here is a simple VO with Boolean
type and primitive type:
public class TestVO {
private Boolean bigBoolean;
private boolean smallBoolean;
}
Here is my java reflection code:
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);
}
}
Output is:
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)
I tried to set the bigBoolean
value with new Boolean(true)
, Boolean.TRUE
, true
etc. nothing works.
According to this, bigBoolean.setBoolean()
is invoked to set a field that is of the reference type Boolean with a value of primitive type. In the non-reflection equivalent Boolean val = true
, the compiler would convert (or box) the primitive type 'true ' to a reference type as new Boolean(True)
so that its type checking will accept the statement.
When using reflection, type checking only occurs at runtime so there is no opportunity to box the value. This forces to throw IllegalArgumentException
due to Inconvertible Types
这篇关于无法通过反射设置布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!