Uncategorized

Project Lombok

I saw this by mistake(surely this is not new for some of you). I was looking in some repo, I think it was spring-data-rest or something similar and I was like: What the hell is this? You don’t need to write getters and setters for your class members! Suddenly it’s enough to have

@Getter @Setter private int number;

How can it be? Well it’s similar as the IDE does it, but instead of putting the methods in the java file, it puts them in the class file by using bytecode instrumentation.

Hmmm. Is it something that is known, but somehow I never heard of it? Let me check the repo. Wow! Almost 5K likes and 1K forks! That means it’s pretty known, and stable.

What else is there?

and a few more. Note that there are also some experimental features out here. Very nice. Now the million dollar question. Should this be used?

Well, here is a comprehended experience with it. I would say that it can be used but with care. Don’t go too far with it. The above annotations are enough. If things get too complex you’re better off. Of course this needs to be a team decision, as every member needs to understand what’s going on there, and can slow down a bit the ramp-up of a new one (depends on his experience). It will go far with unit testing where you need data input for your tests. There @Builder will work like a charm. It will also save a bunch of code when you are using mappers and transfer objects. Code will be more succinct and easy to read. More it will with with ORMs such as Hibernate.

Example time. Considering this class

public class Car2 {

   private String brand;
   private String type;
   private int doors;
   private int power;
   private int torque;
   private int gears;
   private String color;
   private String emissionClass;
   private int mileage;

   private Car2() {
   }

   public static class Builder {

      private String brand;
      private String type;
      private int doors;
      private int power;
      private int torque;
      private int gears;
      private String color;
      private String emissionClass;
      private int mileage;

      public Builder withBrand(String brand) {
         this.brand = brand;
         return this;
      }

      public Builder withType(String type) {
         this.type = type;
         return this;
      }

      public Builder withDoors(int doors) {
         this.doors = doors;
         return this;
      }

      public Builder withPower(int power) {
         this.power = power;
         return this;
      }

      public Builder withTorque(int torque) {
         this.torque = torque;
         return this;
      }

      public Builder withGears(int gears) {
         this.gears = gears;
         return this;
      }

      public Builder withColor(String color) {
         this.color = color;
         return this;
      }

      public Builder withEmissionClass(String emissionClass) {
         this.emissionClass = emissionClass;
         return this;
      }

      public Builder withMileage(int mileage) {
         this.mileage = mileage;
         return this;
      }

      public Car2 build() {
         Car2 car = new Car2();
         car.brand = this.brand;
         car.type = this.type;
         car.doors = this.doors;
         car.power = this.power;
         car.torque = this.torque;
         car.gears = this.gears;
         car.color = this.color;
         car.emissionClass = this.emissionClass;
         car.mileage = this.mileage;
         return car;
      }
   }

   public String getBrand() {
      return brand;
   }

   public String getType() {
      return type;
   }

   public int getDoors() {
      return doors;
   }

   public int getPower() {
      return power;
   }

   public int getTorque() {
      return torque;
   }

   public int getGears() {
      return gears;
   }

   public String getColor() {
      return color;
   }

   public String getEmissionClass() {
      return emissionClass;
   }

   public int getMileage() {
      return mileage;
   }

   @Override
   public boolean equals(Object o) {
      if (this == o)
         return true;
      if (o == null || getClass() != o.getClass())
         return false;

      Car2 car2 = (Car2)o;

      if (doors != car2.doors)
         return false;
      if (power != car2.power)
         return false;
      if (torque != car2.torque)
         return false;
      if (gears != car2.gears)
         return false;
      if (mileage != car2.mileage)
         return false;
      if (brand != null ? !brand.equals(car2.brand) : car2.brand != null)
         return false;
      if (type != null ? !type.equals(car2.type) : car2.type != null)
         return false;
      if (color != null ? !color.equals(car2.color) : car2.color != null)
         return false;
      return emissionClass != null ? emissionClass.equals(car2.emissionClass) : car2.emissionClass == null;
   }

   @Override
   public int hashCode() {
      int result = brand != null ? brand.hashCode() : 0;
      result = 31 * result + (type != null ? type.hashCode() : 0);
      result = 31 * result + doors;
      result = 31 * result + power;
      result = 31 * result + torque;
      result = 31 * result + gears;
      result = 31 * result + (color != null ? color.hashCode() : 0);
      result = 31 * result + (emissionClass != null ? emissionClass.hashCode() : 0);
      result = 31 * result + mileage;
      return result;
   }
}

you can see the builder pattern in there. With lombok this will look

@Data
@Builder
public class CarLombok2 {

   private String brand;
   private String type;
   private int doors;
   private int power;
   private int torque;
   private int gears;
   private String color;
   private String emissionClass;
   private int mileage;
}

Done. No boilerplate code. Class is short and concise.

You have also delombok when you want to go back to explicit methods in your java file. It’s easy to use, just a:

java -jar lombok.jar lombokSrc -d delombokSrc

There is also a maven plugin for this and also IDE support. That’s it folks.

SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s