Why Java language does not offer a way to declare getters and setters of a given "field" through ann

Posted by zim2001 on Stack Overflow See other posts from Stack Overflow or by zim2001
Published on 2009-11-06T11:21:22Z Indexed on 2010/03/27 11:23 UTC
Read the original article Hit count: 444

Filed under:
|

I actually happily design and develop JEE Applications for quite 9 years, but I realized recently that as time goes by, I feel more and more fed up of dragging all these ugly bean classes with their bunch of getters and setters.

Considering a basic bean like this :

public class MyBean {

    // needs getter AND setter
    private int myField1;

    // needs only a getter, no setter
    private int myField2;

    // needs only a setter, no getter
    private int myField3;

    /**
     * Get the field1
     * @return the field1
     */
    public int getField1() {
            return myField1;
    }

    /**
     * Set the field1
     * @param value the value
     */
    public void setField1(int value) {
            myField1 = value;
    }

    /**
     * Get the field2
     * @return the field2
     */
    public int getField2() {
            return myField2;
    }

    /**
     * Set the field3
     * @param value the value
     */
    public void setField3(int value) {
            myField3 = value;
    }

}

I'm dreaming of something like this :

public class MyBean {

    @inout(public,public)
    private int myField1;

    @out(public)
    private int myField2;

    @in(public)
    private int myField3;
}

No more stupid javadoc, just tell the important thing...

It would still be possible to mix annotation and written down getters or setters, to cover cases when it should do non-trivial sets and gets. In other words, annotation would auto-generate the getter / setter code piece except when a literate one is provided.

Moreover, I'm also dreaming of replacing things like that :

MyBean b = new MyBean();
int v = b.getField1();
b.setField3(v+1);

by such :

MyBean b = new MyBean();
int v = b.field1;
b.field3 = v+1;

In fact, writing "b.field1" on the right side of an expression would be semantically identical to write "b.getField1()", I mean as if it has been replaced by some kind of a preprocessor.

It's just an idea but I'm wondering if I'm alone on that topic, and also if it has major flaws.

I'm aware that this question doesn't exactly meet the SO credo (we prefer questions that can be answered, not just discussed) so I flag it community wiki...

© Stack Overflow or respective owner

Related posts about java

Related posts about syntax