Java Developers Need To Be Using Lombok
Lombok allows Java developers to do a few things:
- Never have to write getters and setters again.
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;
}
}
With Lombok
public class Animal {
@Getter @Setter private String name;
@Getter @Setter private String gender;
@Getter @Setter private String species;
}
Here are a list of other cool feature in the Lombok API.
- 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.
- We can now annotate our class with a
- No more overriding equals and hashCode methods.
- Annotate class with
@EqualsAndHashCode
for easy generation of equals and hashCode methods at compile time.
- Annotate class with
- 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.
-
- Use @Data shortcut for @ToString, @EqualsAndHashCode, @ RequiredArgsConstructor, and
@Getter / @Setter (on all non final fields) .
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;
}
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();
}
}
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
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 :-)
ReplyDeleteGlad 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.
DeleteThanks for the post.
ReplyDeleteJust liked to add that usage of @Data is more simple than adding @Getter and @Setter for each field.
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.
DeleteThey should be putting these on main Java API. It's so basic and useful.
ReplyDelete