Any way to do Android binding with Lombok accessor

2019-07-01 16:58发布

问题:

I'm starting to play with Android binding. The standard (1-way) binding is to the point of being good enough for the people I hang out with.

However, I find that I can't use Lombok accessors without a Could not find accessor error. Have you found a way around this, shy of manually writing getters and setters like some kind of Lombok-ignorant cavebeast?

@Bindable
@Getter @Setter
private String stringField;

//Must uncomment hand-coded accessors to compile!
//public String getStringField() { return stringField;}
//public void setStringField(String s) { stringField = s;}

For posterity, my original sample code was using a boolean, which clouds the issue a little:

    @Bindable 
    @Getter @Setter private boolean showpassword = false;

/*  This only compiles if the handcoded accessors are uncommented.

    public boolean getShowpassword() {
        return showpassword;
    }
    public void setShowpassword(boolean b) {
        showpassword = b;
    }
*/

回答1:

For boolean, by default the generated "getter" is isShowpassword, following the beanspec. The generated "setter" is setShowPassword. The error message suggests it is the "getter" that cannot be found.

You can use a configuration key to change this behavior. According to the documentation, if you include the following in lombok.config your program should work without the hand written getter and setter:

lombok.getter.noIsPrefix = true