: Lombok It 's a very old project, and it's also very practical, but it's odd that over the years it seems to have been tepid. Many Lombok features can be referred to its official website, ha. Brother even education here is just a brief introduction to its basic functions.
Lombok is a Java library designed to reduce code development efforts . Brother Lian In this article describes how to use it to complete the Getter/setter method, construct the method, and rewrite equals (),hashcode () and the toString () method. In Java , a very simple class is often very complex to write. If you have not heard of Lombok , it will certainly make you satisfied.
Lombok can help Java Developers do these things:
No more writing the Setter/getter method.
Without Lombok , we usually have to write this:
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;
}
}
After writing more of these methods, I slowly started to hate Java a little bit.
After having the Lombok :
public class Animal {
@Getter @Setter private String name;
@Getter @Setter private String gender;
@Getter @Setter private String species;
}
The days began to get more moist. Here are Some of the other great features of Lombok.
You no longer have to rewrite the toString method. You can add @ToString annotations to the class,andLombok automatically overrides the ToString method and prints out all the fields of the class.
You can automatically generate these methods without rewriting the Equals and Hashcode methods @EqualsAndHashCode annotations
The construction method is generated by class annotations @NoArgsConstructor A default construction method is generated @RequiredArgsConstructor for all Final and non-empty (note: with @NonNull annotations ) fields generate a construction method @AllArgsConstructor A construction method is generated for all fields
@Data is @ToString, @EqualsAndHashCode, @RequiredArgsConstructor, and @Getter/@ Setter A shortcut to these annotations.
These are the most commonly used Lombok features. Some of the other features can be learned on its project homepage.
Example
Suppose we have a class that needs to support serialization and requires a constructor method. This requires rewriting equals,hashcode,tostring, providing a Setter/getter method for private member variables , and implementing a constructor method.
Using the Lombok
@RequiredArgsConstructor
@ToString
@EqualsAndHashCode
public class Animal {
@Getter @Setter private String name;
@Getter @Setter private String gender;
@Getter @Setter private String species;
}
No Lombokused:
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 = * result + (gender! = null? Gender.hashcode (): 0);
result = * 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 that" with annotated work,theIDE has already generated this code!" "
What I'm trying to say is that you're not talking about the idea. People don't like Java because it has to have these messy things in their code. Like Ruby, Groovy, Perl , or any other scripting language, it's a lot easier than that. Simple is the United States, the brothers even small Lombok think that the Java very well-matched .
Share the tool-lombok Java development