bean properties suck


I’m pretty fed up with bean properties. Here’s the low down, “JavaBeans” was this attempt to make VB style visual development in Java easy – the idea was to define a way to make components with properties, and this was back in the late nineties. Fast forward almost ten years, and bean properties are the foundational technology on which much is built. Hibernate, Spring, various MVC frameworks etc. But, the technology hasn’t changed in the meantime. So, we’re left with a simple Person class that looks like this:

public class Person {

   private String name;
   private int age;

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

   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      return age;
   }
}

Oh, you ask, but wait, I haven’t had to code getters and setters for years I just right click on Eclipse and …..blah blah blah. I say, good for you, but your code still ends up having 12 lines of meaningless encapsulation just to make you feel better. And, that’s the thing, bean property encapsulation is really just there to make you feel better, it isn’t really encapsulation because encapsulation implies that it is shielding you from some detail……and, i’ve got news for you 99.999% of the time it isn’t. In fact, if you put logic into your getters and setters you are violating one of the unspoken rules of bean properties – “getters and setters don’t have embedded logic…..it isn’t clean”.

Well….screw all of this nonsense….seriously, do you go home at night and rest that much easier because, “Phew, thank God all of our bean properties are properly encapsulated”. I’ve only got a few million more keystrokes left in these wrists and I’m absolutely tired of the wasted time right clicking on some code generator. I propose an annotation that would make the Person class.

public class Person {
   @Property private String name;
   @Property private int age;
}

If you really, really, really wanted to have some logic in a get or set method, go ahead and write one yourself, but save yourself the typing time.

alright who wants to do this, or better yet, anyone done this yet? I nominate a Java5 version of commons-beanutils to provide this annotation because like it or not it’s got to play nice with existing systems that use beans. Maybe this could be a code generating Maven plugin no?