Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for accessors without JavaBeans-conventional prefix. #173

Open
marcus-talbot42 opened this issue Nov 8, 2022 · 0 comments
Open
Assignees

Comments

@marcus-talbot42
Copy link
Contributor

marcus-talbot42 commented Nov 8, 2022

With the introduction of record-classes, accessors without the JavaBeans-conventional prefixes (is, get, set, etc.) have become mainstream.

Furthermore, methods with the new format are already supported by popular libraries and frameworks, notably, Lombok, the Spring Framework. Some libraries comparable to BeanMapper also support the new format of accessor, or are working on support.

While support for these new types of accessors seems like a rather fundamental change, the actual work involved should be fairly trivial. I would, however, recommend making the recognition of such accessors optional, and disable it by default. This is certainly a useful feature, however, the unrestricted addition of this feature would lead to breaking changes if anyone uses a combination of the JavaBeans accessors, and the new record-style accessors.

Concretely I propose the following:

Add support for getters and setters as used in the following snippet:

public class Person {

    private String name;
    private LocalDate birthday;

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

    public String name() {
        return this.name;
    }

    public void birthday(LocalDate birthday) {
        this.birthday = birthday;
    }

    public LocalDate birthday() {
        return this.birthday;
    }
}

Add support for "fluent" setters, of which the return-type is equal to the type of the declaring class:

public class Person {

    private String name;
    private LocalDate birthday;

    public Person name(String name) {
        this.name = name;
        return this;
    }

    public String name() {
        return this.name;
    }

    public Person birthday(LocalDate birthday) {
        this.birthday = birthday;
        return this;
    }

    public LocalDate birthday() {
        return this.birthday;
    }
}

Add an annotation @BeanPropertyAccessor, to support setters that take an argument that is not of the type of the field, and allow for the use of accessors that do not share the name of the field. This annotation does not enable the use of record-style accessors. To do so, an option, along the lines of boolean enableRecordStyleAccessors, would need to be set to true in the (Core)Configuration. Note that this annotation shares some intended functionality with the @BeanProperty-annotation. I propose to deprecate the use of @BeanProperty on methods, however, this should be carefully reviewed:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BeanPropertyAccessor {
    String value();
}

Which would be used as follows:

public class Person {

    private String name;
    private String birthday;

    @BeanPropertyAccessor("name")
    public Person name(String name) {
        this.name = name;
        return this;
    }

    public String name() {
        return this.name;
    }

    @BeanPropertyAccessor("birthday")
    public Person dateOfBirth(LocalDate birthday) {
        this.dateOfBirth = birthday.toString();
        return this;
    }

    public LocalDate birthday() {
        return this.birthday;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant