-
Notifications
You must be signed in to change notification settings - Fork 7
Description
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;
}
}