Share the tool-lombok Java development

Source: Internet
Author: User
Tags lombok java

Lombok is a very old project, also very practical, but strangely so for years seems to have been tepid. Many Lombok features can be referred to its official website, ha . brother Lian www.lampbrother.net here is just a brief introduction to its basic functions.

Lombok is a Java library designed to reduce code development efforts . 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.

No more rewriting the Equals and hashcode methods @EqualsAndHashCode Annotations can automatically generate these methods

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 will generate a construction method 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.