abbreviating getters and setters proposal for java 8

Mikhail Grushinskiy mgrushinskiy at gmail.com
Fri May 20 03:44:16 UTC 2011


Hello JDK developers,

I have one proposal for java 8.
Java getters and setters are quite verbose. There was a number of proposals
to address it but none was
ever included into java. So here is another one.

Instead of:

public class Class1 {

    private Long      a1;
    private boolean   b1;
    private Character c1;

    public Long getA1() {
        return a1;
    }

    public void setA1(Long a1) {
        this.a1 = a1;
    }

    public boolean isB1() {
        return b1;
    }

    public void setC1(Character c1) {
        this.c1 = c1;
    }
}

Allow writing it like following (using ><  .> and .< notation)
 where
     >< abbreviates both getter and setter
     .> abbreviation for a getter
     .< abbreviation for a setter

1.

public class Class1 {

    private Long      a1 ><;
    private boolean   b1 .>;
    private Character c1 .<;
}

2. Implementing getters and setters (note that syntax for a setter is
consistent with java 7 lambda expression):

public class Class1 {

    private Long      a1 ><;
    private boolean   b1 .> {true;};
    private Character c1 .< {Character x -> c1 = x;};
}

3. Calling getters and setters

Class1 obj1 = new Class1();

obj1.>a1; // same as obj1.getA1();

obj1.<a1(val1); // same as obj1.setA1(val1);


4. Annotating getter or setter

public class Class2 {

    private String s1 @ValidDateString .<;

}

5. Possible alternatives to .> and .<

 instead of .> reserve |>  or ->
 instead of .< reserve <|  or <|
 instead of >< reserve <-->


Thanks,
--MG



More information about the discuss mailing list