Java Developers Need To Be Using Lombok


Lombok is a Java library meant to simplify the development of Java code writing. This article deals with writing classes that need getters/setters, override equals, hashCode, toString and/or offer a copy constructor. Way to go Java! You just made writing a simple class cumbersome. Project Lombok will make your day if you haven't seen it yet.



Lombok allows Java developers to do a few things:
  1. Never have to write getters and setters again.
Without Lombok
public class Animal {

    private String name;
    private String gender;
    private String species;

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

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

    public String getGender(){
        return this.gender;
    }

    public void setGender(String gender){
        this.gender = gender;
    }

    public String getSpecies(){
        return this.species;
    }

    public void setSpecies(String species){
        this.species = species;
    }
}

While writing those getter/setters I slowly began to hate Java.

With Lombok
public class Animal {

    @Getter @Setter private String name;
    @Getter @Setter private String gender;
    @Getter @Setter private String species;
}

Life just got a little easier.
Here are a list of other cool feature in the Lombok API.
  1. No more overriding toString
    • We can now annotate our class with a @ToString and lombok will override our toString method and print out the classes fields.
  2. No more overriding equals and hashCode methods.
    • Annotate class with @EqualsAndHashCode for easy generation of equals and hashCode methods at compile time.
  3. Generates constructors based on class annotations.
    • @NoArgsConstructor used for creating a no argument constructor.
    • @RequiredArgsConstructor used for creating constructor that takes one argument per non final/ non-null fields.
    • @AllArgsConstructor used for creating constructor takes in one argument for every field.
  4. Use @Data shortcut for @ToString, @EqualsAndHashCode, @ RequiredArgsConstructor, and @Getter / @Setter (on all non final fields).
These are the common features of Lombok you will use. Other features can be referenced on the project lombok site

Class Example With and Without Lombok

Say we need a class that is serializable and needs a copy constructor. This requires overriding equals, hashCode, toString, provide getter/setters for private fields, and a copy constructor.

With Lombok
@RequiredArgsConstructor
@ToString
@EqualsAndHashCode
public class Animal {
    @Getter @Setter private String name;
    @Getter @Setter private String gender;
    @Getter @Setter private String species;
}
That my friend is modernized java code.

Without Lombok
public class Animal {

    private String name;
    private String gender;
    private String species;

    public Animal(String name, String gender, String species) {
        this.name = name;
        this.gender = gender;
        this.species = species;
    }

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

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

    public String getGender(){
        return this.gender;
    }

    public void setGender(String gender){
        this.gender = gender;
    }

    public String getSpecies(){
        return this.species;
    }

    public void setSpecies(String species){
        this.species = species;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Animal)) return false;

        Animal animal = (Animal) o;

        if (gender != null ? !gender.equals(animal.gender) : animal.gender != null) return false;
        if (name != null ? !name.equals(animal.name) : animal.name != null) return false;
        if (species != null ? !species.equals(animal.species) : animal.species != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (species != null ? species.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this) //Using guava library objects toString
                .add("name", name)
                .add("gender", gender)
                .add("species", species)
                .toString();
    }
}

Many Java developers will argue, "IDE's can generate that stuff as fast as you can annotate the class though!".
To you I will say you are misssing the point. People dislike Java because of the clutter mess of having to own and write all this code. The same code in Ruby, Groovy, Perl, or any scripting language is much simpler. Simple is better, ergo Lombok is better for Java.
Check them out on lombok.org

Comments

  1. Great article, I used AndroidAnnotations on one project and it was very similar, behind the scenes code generation. Thanks for the info, I've bookmarked it for when I write some regular POJ again :-)

    ReplyDelete
    Replies
    1. Glad to hear! Lombok is a coding style that I feel like Java developers should prefer over default Java. It simplifies and almost modernizes the look of Java. Plus, it likely will save you time. You gotta be appreciative for the APT as well. Allows code style to work with Java instead of against it.

      Delete
  2. Thanks for the post.
    Just liked to add that usage of @Data is more simple than adding @Getter and @Setter for each field.

    ReplyDelete
    Replies
    1. Thanks for your comment! I think the use of @Data instead of @Getter @Setter is going to be the coders preference. And more so the person who decides the standards within the application.

      Delete
  3. They should be putting these on main Java API. It's so basic and useful.

    ReplyDelete

Post a Comment

Popular posts from this blog

Atmosphere Websockets & Comet with Spring MVC

Microservices Tech Stack with Spring and Vert.X