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.
Nullable boolean can be null, or having a value “true” or “false”. Before accessing the value, we should verify if the variable is null or not. This can be done with the classical check : if … else …
to store booleans in a collection (List, Map, etc.) to represent a nullable boolean (coming from a nullable boolean column in a database, for example). The null value might mean "we don't know if it's true or false" in this context. each time a method needs an Object as argument, and you need to pass a boolean value.
To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.
To display Boolean type, firstly take two variables and declare them as boolean. val1 = true; Now, use if statement to check and display the Boolean true value. if(val1) System.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With