Skip to main content

Why hasn't something like Project Lombok (@Getter/@Setter/@ToString/@HashCode/@Equals/etc) been integrated into the next spec?

No replies
engfer
Offline
Joined: 2011-06-17
Points: 0

I'm sure that everyone here is tired of creating accessors and mutators just to satisfy the JavaBean pattern, and I'm sure that everyone is tired of creating and maintaining hashCode/toString/equals by having your editor regenerate these things.
Ugly way...

public class PojoBean {
    private String foo;
    private String bar;

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getFoo() {
        return foo;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

    public String getBar() {
        return bar;
    }

    // Eclipse-generated examp
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((bar == null) ? 0 : bar.hashCode());
        result = prime * result + ((foo == null) ? 0 : foo.hashCode());
        return result;
    }

    // Eclipse-generated example
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        PojoBean other = (PojoBean) obj;
        if (bar == null) {
            if (other.bar != null)
                return false;
        }
        else if (!bar.equals(other.bar))
            return false;
        if (foo == null) {
            if (other.foo != null)
                return false;
        }
        else if (!foo.equals(other.foo))
            return false;
        return true;
    }

    // Eclipse-generated example
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("PojoBean [foo=").append(foo).append(", bar=").append(bar).append("]");
        return builder.toString();
    }
}

Project Lombok pretty way...
http://projectlombok.org/features/index.html
@ToString
@EqualsAndHashCode
public class PojoBean {
    @Getter
    @Setter
    private String foo;

    @Getter
    @Setter
    private String bar;
}

Why can't this just be a compiler preprocessor, so that it ends up as syntatical sugar (doesn't matter at runtime)?
If it was part of the core SDK, editors would just have to support it instead of the Lombok way of writing code for all of the editors.