📜 ⬆️ ⬇️

Other islands. Lombok Project for Java

This article is a sort of free translation-retelling.

It turns out that Java is not the only island that is somehow associated with Java development. Here is another one called Lombok . The name of this island is a wonderful project, distributed under the MIT license .

What is Project Lombok?


Project Lombok is two things in one: a code generator at the compilation stage and a code generator at the design stage. Which makes finding a diamond.
In essence, Lombok integrates directly into the Eclipse compilation cycle (by manipulating the abstract code syntax tree as it is typed ) and instantly generates code based on annotations. The generated code is immediately available to all other classes.
What code can Lombok generate from annotations? Most importantly, basic generic things are generated that usually make Java classes look wildly verbose and monstrous, namely:

At the same time, Lombok provides automatic resource management. For example, your threads will always close safely without the need to use try / catch / finally blocks.
')

Install Lombok and support Maven


Lombok is provided as an executable JAR file ( lombok.jar ). True, at present, the development environment mode only works in Eclipse. When you run the JAR file, a simple wizard will appear and ask you to specify where the Eclipse executable files are located (see Figure 1).



Figure 1. Lombok installation wizard.

Simply point to the Eclipse executables and click Install / Update. You will need to do this every time for all new versions of Lombok.
To enable Maven support, simply add the Lombok repository and its dependencies to the pom.xml file, following the instructions on the project website . After which, Lombok will work with the Maven compilation life cycle "right out of the box."

Project Lombok in action


To fully understand how Lombok exterminates a ton of lines of code in a typical POJO class, consider the following typical Person entity:
public class Person { private Long personId; private String salutation; private String firstName; private String middleName; private String lastName; private String phoneNumber; private String email; private String addressLine1; private String addressLine2; private String city; private String state; private String country; private Calendar birthDate; } 


In order for this class to become a true POJO, it is necessary to add the code getters, setters, toString (), equals () and hashCode () for it. If you decided to use the automatic code generation functions in Eclipse, then a Person POJO with the speed of mushrooms will grow to more than 240 lines of code, most of which, although necessary, are actually just garbage that makes reading and understanding difficult. (see Listing 1).

Listing 1. Automatically generated Person POJO

 public class Person { private Long personId; private String salutation; private String firstName; private String middleName; private String lastName; private String phoneNumber; private String email; private String addressLine1; private String addressLine2; private String city; private String state; private String country; private Calendar birthDate; public Long getPersonId() { return personId; } public void setPersonId(Long personId) { this.personId = personId; } public String getSalutation() { return salutation; } public void setSalutation(String salutation) { this.salutation = salutation; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public Calendar getBirthDate() { return birthDate; } public void setBirthDate(Calendar birthDate) { this.birthDate = birthDate; } @Override public String toString() { return "Person [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", birthDate=" + birthDate + ", city=" + city + ", country=" + country + ", email=" + email + ", firstName=" + firstName + ", lastName=" + lastName + ", middleName=" + middleName + ", personId=" + personId + ", phoneNumber=" + phoneNumber + ", salutation=" + salutation + ", state=" + state + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((addressLine1 == null) ? 0 : addressLine1.hashCode()); result = prime * result + ((addressLine2 == null) ? 0 : addressLine2.hashCode()); result = prime * result + ((birthDate == null) ? 0 : birthDate.hashCode()); result = prime * result + ((city == null) ? 0 : city.hashCode()); result = prime * result + ((country == null) ? 0 : country.hashCode()); result = prime * result + ((email == null) ? 0 : email.hashCode()); result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); result = prime * result + ((personId == null) ? 0 : personId.hashCode()); result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); result = prime * result + ((salutation == null) ? 0 : salutation.hashCode()); result = prime * result + ((state == null) ? 0 : state.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; Person other = (Person) obj; if (addressLine1 == null) { if (other.addressLine1 != null) return false; } else if (!addressLine1.equals(other.addressLine1)) return false; if (addressLine2 == null) { if (other.addressLine2 != null) return false; } else if (!addressLine2.equals(other.addressLine2)) return false; if (birthDate == null) { if (other.birthDate != null) return false; } else if (!birthDate.equals(other.birthDate)) return false; if (city == null) { if (other.city != null) return false; } else if (!city.equals(other.city)) return false; if (country == null) { if (other.country != null) return false; } else if (!country.equals(other.country)) return false; if (email == null) { if (other.email != null) return false; } else if (!email.equals(other.email)) return false; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; if (middleName == null) { if (other.middleName != null) return false; } else if (!middleName.equals(other.middleName)) return false; if (personId == null) { if (other.personId != null) return false; } else if (!personId.equals(other.personId)) return false; if (phoneNumber == null) { if (other.phoneNumber != null) return false; } else if (!phoneNumber.equals(other.phoneNumber)) return false; if (salutation == null) { if (other.salutation != null) return false; } else if (!salutation.equals(other.salutation)) return false; if (state == null) { if (other.state != null) return false; } else if (!state.equals(other.state)) return false; return true; } } 


And now, what does the same POJO look like after “Lombokization”:
 @Data public class Person { private Long personId; private String salutation; private String firstName; private String middleName; private String lastName; private String phoneNumber; private String email; private String addressLine1; private String addressLine2; private String city; private String state; private String country; private Calendar birthDate; } 


And that's it! When Lombok is installed in your Eclipse Runtime, the simple Data annotation magically draws all that necessary standard garbage code. What makes the definition of JPA / Hibernate entities incredibly easy and fast.
But that's not all! All getters and setters methods appear in the schema, as if the code actually existed (see Figure 2).



Figure 2. All getters and setters methods are visible in the schema.

They are also visible when completing the code (see Figure 3).



Figure 3. All methods are visible when completing the code.

If more delicate control is needed, Project Lombok offers the following annotations Getter , @Setter, @ToString and @EqualsAndHashCode. And the above means that the Data abstract combines all of them in itself. In our development process, the Data abstract is used in 99% of cases.

Additional Lombok Features


In addition to the main features that you just saw, Lombok comes with several additions:


Future Plans: Adding New Java Features


At the time of writing, the current version of the Project Lombok was 0.8.5. The short-term perspective of the Lombok team was the stabilization and dependability of work in Eclipse. Although there are some minor problems with earlier versions (for example, some unexpected warnings when compiling), but a simple recompilation of the project allows you to quickly get rid of them. In general, the benefits of using Lombok far outweigh the existing small Eclipse crashes (and which are quite rare in my experience). Long-term plans for Lombok are much more ambitious. Both authors of the Lombok project (Reinier Zwitserloot and Roel Spilker) want to intercept the compilation process in Eclipse to the point where they can really add new features in Java, in particular, the actual completion. Learn more about this ambitious goal in this Google Groups discussion .

disadvantages


The main disadvantage of Lombok is obvious: working in the development environment mode is supported only in Eclipse. If Eclipse is not your IDE, then at the moment the Lombok project is not your option. Perhaps such support in the future may appear in NetBeans and IntelliJ environments, but it provides for some pretty heavy IDE hacks for a particular environment. On the other hand, all workshops with Eclipse should consider adding Lombok to their daily toolkit today.

Goodbye verbosity POJO


With the introduction of the Project, Lombok, Reiner and Roel have made Java POJO verbosity a story. When you add Lombok to your daily development practice, you just don’t want to go back. It's very simple and in our opinion, this is the most revolutionary addition to the Java ecosystem after the birth of Eclipse itself.

Links




Source: https://habr.com/ru/post/72555/


All Articles