Saturday, October 3, 2015

Cleaner java code with Lombok

Sometimes, to discover what a class does, you see yourself reading several lines of code. And much of these lines are constructors, getters, setters, toString, equals, hashcode, etc. They simply get on your way.

Wouldn´t be great if all those lines just weren´t there?

Lombok is a library that can help you with that. It's great helping you reduce tedious and boilerplate code.

Your class may go from this:

import java.util.Date;
public class PersonBoilerPlateCode {
private String name;
private String lastName;
private Date dateOfBirth;
private String address;
public PersonBoilerPlateCode() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "PersonBoilerPlateCode [address=" + address + ", dateOfBirth="
+ dateOfBirth + ", lastName=" + lastName + ", name=" + name
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result
+ ((dateOfBirth == null) ? 0 : dateOfBirth.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonBoilerPlateCode other = (PersonBoilerPlateCode) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (dateOfBirth == null) {
if (other.dateOfBirth != null)
return false;
} else if (!dateOfBirth.equals(other.dateOfBirth))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
To this:

import java.util.Date;
import lombok.Data;
public @Data class Person {
private String name;
private String lastName;
private Date dateOfBirth;
private String address;
}
view raw Person.java hosted with ❤ by GitHub
Lombok instruments the code for you. You can check the class´ methods that were automatically generated with the command:

javap Person.class

And this is how it looks like after the instrumentation made by Lombok:

public class br.org.roger.lombok.Person {
public java.lang.String getName();
public java.lang.String getLastName();
public java.util.Date getDateOfBirth();
public java.lang.String getAddress();
public void setName(java.lang.String);
public void setLastName(java.lang.String);
public void setDateOfBirth(java.util.Date);
public void setAddress(java.lang.String);
public boolean equals(java.lang.Object);
protected boolean canEqual(java.lang.Object);
public int hashCode();
public java.lang.String toString();
public br.org.roger.lombok.Person();
}
view raw Person_p.java hosted with ❤ by GitHub
If your class is a Value Object as described in Martin Fowler´s article, you can use more fine grained annotations as seen in the code below:

package br.org.roger.lombok;
import java.util.Date;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class PersonFineGrained {
@Getter private String name;
@Getter private String lastName;
@Getter private Date dateOfBirth;
@Getter private String address;
}

Another great feature is the @Builder feature. I recommend it to all who's going to try Lombok.

To use Lombok, you need to put its library in the classpath during the compile time and only during the compile time. At runtime, your class´ bytecode has already been changed.

If you use Maven, you can declare lombok as a provided dependency in your POM file like this:

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>0.9.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
view raw lombok_provided hosted with ❤ by GitHub

The installation and a description of the basic functions can be found here. There is also a great video in the frontpage. All of these resources and references are very clear and simple to follow. You´ll be using Lombok in your project in no time.

Just a little warning for Sonar users: it does not like Lombok! It complains that the bytecode lacks getters, setters and such. To help with this, there is a command called delombok that I think that can help making Sonar happy.

Any comment about this post? Please drop a line! I´ll be glad to hear from you!

No comments:

Post a Comment