Great Idea: Property Annotations alternative to getter/setters
Currently, a huge amount of Java objects use property fields with getter/setter accessors:
class MyClass
{
protectedint xyz;
publicint getXyz(){
return xyz;
}
publicint setXyz(int xyz){
this.xyz = xyz;
}
}
.NET uses a slightly simpler syntax which internally generates getter/setter methods:
class MyClass
{
protectedint xyz;
publicint Xyz{
get{
return xyz;
}
set{
xyz = value;
}
}
}
Most of the time (90%+), the getters and setters just set/return. Of course, there are times where it's necessary to implement additional logic without changing the interface presented to other classes. However, there are lots of cases where there is huge amounts of trivial getter/setter code. Also, it takes effort to maintain the getters/setters, when fields are added, deleted, or renamed.
Wouldn't it be much nicer to have trivial getters/setters implied via annotations? Instead of the above examples, do:
class MyClass
{
// defaults to providing both getter and setter.
@PublicProperty
protectedint xyz;
}
You could have variants for protected access, private access, and parameters for getter only or setter only. And, you would continue to use the traditional syntax when you want to do non-trivial getter/setter logic.
The big advantage is that this syntax would be much easier to maintain. Much less code to scroll through. Much easier to rename fields. Easier to delete fields. Easier to add new fields without relying on IDE "generate getter/setter" features.
What does everyone think?

