You are tired of declaring the Getter, Setter, toString(), equals() and hashCode() method for each class that we want to use these methods. Using @Data annotation in Project Lombok can help us avoid this problem.
Let’s create a Maven project for example:
With Project Lombok dependency as below:
1 2 3 4 5 6 |
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency> |
Remember that, in order for the IDE can understand the code, we need to install Project Lombok plugin into our IDE. See construction for IntelliJ IDEA at here.
Now, for example, I have a Student class with some information like firstName, middleName, lastName and country. Without using Project Lombok library, I must declare Student bean as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
package com.huongdanjava.lombok; public class Student { private String firstName; private String middleName; private String lastName; private String country; 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 getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (!firstName.equals(student.firstName)) return false; if (!middleName.equals(student.middleName)) return false; if (!lastName.equals(student.lastName)) return false; return country.equals(student.country); } @Override public int hashCode() { int result = firstName.hashCode(); result = 31 * result + middleName.hashCode(); result = 31 * result + lastName.hashCode(); result = 31 * result + country.hashCode(); return result; } @Override public String toString() { return "Student{" + "firstName='" + firstName + '\'' + ", middleName='" + middleName + '\'' + ", lastName='" + lastName + '\'' + ", country='" + country + '\'' + '}'; } } |
With Project Lombok, we can remove all of these methods here. Just with @Data annotation in Project Lombok.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.lombok; import lombok.Data; @Data public class Student { private String firstName; private String middleName; private String lastName; private String country; } |
Very simple and clean.
If you check the Student.class file in /target/classes/com/huongdanjava/lombok, you will see the content as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.huongdanjava.lombok; public class Student { private String firstName; private String middleName; private String lastName; private String country; public Student() { } public String getFirstName() { return this.firstName; } public String getMiddleName() { return this.middleName; } public String getLastName() { return this.lastName; } public String getCountry() { return this.country; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setCountry(String country) { this.country = country; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof Student)) { return false; } else { Student other = (Student)o; if(!other.canEqual(this)) { return false; } else { label59: { Object this$firstName = this.getFirstName(); Object other$firstName = other.getFirstName(); if(this$firstName == null) { if(other$firstName == null) { break label59; } } else if(this$firstName.equals(other$firstName)) { break label59; } return false; } Object this$middleName = this.getMiddleName(); Object other$middleName = other.getMiddleName(); if(this$middleName == null) { if(other$middleName != null) { return false; } } else if(!this$middleName.equals(other$middleName)) { return false; } Object this$lastName = this.getLastName(); Object other$lastName = other.getLastName(); if(this$lastName == null) { if(other$lastName != null) { return false; } } else if(!this$lastName.equals(other$lastName)) { return false; } Object this$country = this.getCountry(); Object other$country = other.getCountry(); if(this$country == null) { if(other$country != null) { return false; } } else if(!this$country.equals(other$country)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Student; } public int hashCode() { int PRIME = true; int result = 1; Object $firstName = this.getFirstName(); int result = result * 59 + ($firstName == null?43:$firstName.hashCode()); Object $middleName = this.getMiddleName(); result = result * 59 + ($middleName == null?43:$middleName.hashCode()); Object $lastName = this.getLastName(); result = result * 59 + ($lastName == null?43:$lastName.hashCode()); Object $country = this.getCountry(); result = result * 59 + ($country == null?43:$country.hashCode()); return result; } public String toString() { return "Student(firstName=" + this.getFirstName() + ", middleName=" + this.getMiddleName() + ", lastName=" + this.getLastName() + ", country=" + this.getCountry() + ")"; } } |