如果 getter 使用“is”而不是“get”,则 PropertyUtils 无法转换布尔值

Kev*_*vin 5 java apache-commons

我正在尝试使用 PropertyUtils 类的 copyProperties 方法来复制 bean。

问题在于,如果布尔值的 getter 写为“isXXX”,则无法复制布尔值。仅当布尔值的 getter 为“getXXX”时才有效。例如,

class MyBean {
....
    public boolean isEnabled() {
        return enabled;
    }
....
}
Run Code Online (Sandbox Code Playgroud)

PropertyUtils.copyProperties 不适用于此类。但它适用于此:

class MyBean {
....
    public boolean getEnabled() {
        return enabled;
    }
....
}
Run Code Online (Sandbox Code Playgroud)

有没有什么办法解决这一问题?

非常感谢

fuj*_*ujy 6

这取决于enabled类型:

如果是boolean,则 getter 必须采用以下形式:

public boolean isEnabled() {
    return enabled;
}
Run Code Online (Sandbox Code Playgroud)

如果是Boolean,则 getter 必须采用以下形式:

// The return type of the function doesn't matter Boolean or boolean
public Boolean getEnabled() {
    return enabled;
}
Run Code Online (Sandbox Code Playgroud)