Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropertyUtils failed to convert boolean if the getter uses "is" rather than "get"

I am trying to use PropertyUtils class's copyProperties method to copy a bean.

The problem is that it fails to copy boolean if the getter of the boolean is written as "isXXX". It only works if the getter of the boolean is "getXXX". For example,

class MyBean {
....
    public boolean isEnabled() {
        return enabled;
    }
....
}

PropertyUtils.copyProperties does not work for this class. But it works for this:

class MyBean {
....
    public boolean getEnabled() {
        return enabled;
    }
....
}

Is there any way to fix this?

Many thanks

like image 735
Kevin Avatar asked Sep 15 '13 12:09

Kevin


1 Answers

It depends on the enabled type:

If it is boolean, then the getter must be in this form:

public boolean isEnabled() {
    return enabled;
}

If it Boolean, then the getter must be in this form:

// The return type of the function doesn't matter Boolean or boolean
public Boolean getEnabled() {
    return enabled;
}
like image 141
fujy Avatar answered Nov 03 '22 05:11

fujy