7

I have this simple Bean class and try to set some values with BeanUtils.setProperty Problem is, it seems that String works just fine, but when I try to set a Boolean value it just does not work. I have tried and define the field as public but still not working. Any help? Why is this not working?

public class TestBean {

protected Boolean someBoolean;
protected String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isSomeBoolean() {
    if (someBoolean == null) {
        return true;
    } else {
        return someBoolean;
    }
}

public void setSomeBoolean(Boolean value) {
    this.someBoolean = value;
}

public static void main(String[] args) {
    TestBean o = new TestBean();
    Boolean b = new Boolean(false);
    BeanUtils.setProperty(o, "someBoolean", b);
    BeanUtils.setProperty(o, "name", "A name");
    System.out.println(((TestBean)o).isSomeBoolean());
    // Output = true WHY?????
    System.out.println(((TestBean)o).getName());
    // Output = A name 

    BeanUtils.setProperty(o, "someBoolean", false);
    BeanUtils.setProperty(o, "name", "Another name");

    System.out.println(((TestBean)o).isSomeBoolean());
    // Output = true WHY????
    System.out.println(((TestBean)o).getName());
    // Output = Another name        

}

}

Ziggurat
  • 257
  • 1
  • 4
  • 8
  • Why are you repeatedly casting `o` to `TestBean` when that's it's declared type? – Jon Skeet Jul 04 '12 at 16:41
  • Sorry, that is just something left from a more complex code where I don't know the exact object class. I am instantiating like this: Object o = class.newInstance(); – Ziggurat Jul 04 '12 at 16:50

1 Answers1

4

You need to change it from

protected Boolean someBoolean;

to

protected boolean someBoolean;

You will get more info from here.

Java Beans, BeanUtils, and the Boolean wrapper class

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66
  • Ok, that works, but still don't understand why. There is no way of setting the value of a Boolean using reflection? – Ziggurat Jul 04 '12 at 16:48
  • 2
    Rename your method `isSomeBoolean` to `getSomeBoolean`. The reflection heuristics don't cover the combination of a reference-typed `is...` method. An alternative is to write your own reflection code. It's not that difficult. – Marko Topolnik Jul 04 '12 at 16:50
  • @MarkoTopolnik can you give me a sample of how to write my own reflection code? I tried with Field bool = object.getClass().getDeclaredField("someBoolean"); and then bool.setBoolean but still having the same problem. – Ziggurat Jul 04 '12 at 18:50
  • Better go through the setter: `object.getClass().getMethod("setSomeBoolean", Boolean.class).invoke(object, b);` BTW your sample is wrong because you call `setBoolean` which works on a primitive `boolean` field. You would also need to use `bool.setAccessible(true)` to break encapsulation. Not recommended, anyway. – Marko Topolnik Jul 04 '12 at 18:56
  • Thanks @MarkoTopolnik!!!, that I have to patch my code and use that for Boolean and BeanUtils for the rest of the fields. I can't believe jaxb generates this kind of getters/setters that no Reflection library can guess. – Ziggurat Jul 04 '12 at 19:15
  • It's a chaos out there regarding the JavaBeans so-called "standard". Even the `java.beans` package contains a bug regarding booleans and special-cased method naming. – Marko Topolnik Jul 04 '12 at 19:17