为什么Java语言没有提供通过注释声明给定"字段"的getter和setter的方法?

Mic*_*ann 4 java syntax

我实际上很高兴地设计和开发Java EE应用程序已经有9年了,但我最近意识到,随着时间的推移,我越来越厌倦了将所有这些丑陋的bean类与他们的getter和setter一起拖延.

考虑像这样的基本bean:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

我梦想着这样的事情:

public class MyBean {

    @inout(public,public)
    private int myField1;

    @out(public)
    private int myField2;

    @in(public)
    private int myField3;
}
Run Code Online (Sandbox Code Playgroud)

没有更愚蠢的javadoc,只是告诉重要的事情......

仍然可以混合注释和写下来的getter或setter,以涵盖它应该执行非平凡的集合和获取的情况.换句话说,注释将自动生成getter/setter代码片段,除非提供有文化的代码片段.

而且,我也梦想取代这样的东西:

MyBean b = new MyBean();
int v = b.getField1();
b.setField3(v+1);
Run Code Online (Sandbox Code Playgroud)

通过这样的:

MyBean b = new MyBean();
int v = b.field1;
b.field3 = v+1;
Run Code Online (Sandbox Code Playgroud)

事实上,在表达式的右侧写"b.field1"在语义上与写"b.getField1()"相同,我的意思是好像它已被某种预处理器替换.

这只是一个想法,但我想知道我是否独自在这个话题上,以及它是否有重大缺陷.

我知道这个问题并不完全符合SO信条(我们更喜欢可以回答的问题,而不仅仅是讨论过)所以我将它标记为社区维基...

sfu*_*ger 11

你可能梦想着龙目岛计划

  • 整齐.我之前没见过 (3认同)