java boilerplate

Thursday, December 01, 2005

annotations for Getters and Setters

Writing getters and setters that do nothing beyond reading/writing to an internal private field is such a common job that many java IDEs automate for you. However, the getters and setters still clutter up the code. Generating them is still required in order to ensure that any later additions to the getter or setter code can be added without breaking the API.

We propose simplifying the process by using annotations.

old code:
     private String field;

public String getField() {
return field;
}

protected void setField(String field) {
this.field = field;
}


new code:
     private @Get @Set(Access.PROTECTED) String field;


Access is an enum with PUBLIC, PROTECTED, PACKAGE, and PRIVATE as valid options. The @Get and @Set annotation each have a single value of type Access, defaulting to Access.PUBLIC.

0 Comments:

Post a Comment

<< Home