lombok is a library to help your write java cleaner and more efficiently . It’s plugged into the editor and build tool, which works at compile time .
Essentially, it modifies the byte-codes by operating AST (abstract semantic tree) at compile time, which is allowed by javac. This is, in fact, a way to modify java grammar.
Usage To use it,
install lombok plugin in intellij
add package dependency in project (to use its annotations)
1 2 3 4 5 6 <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.16.18</version > <scope > provided</scope > </dependency >
See all the annotations . Give some example here:
@Data @Data
bundles the features of @ToString
, @EqualsAndHashCode
, @Getter
/ @Setter
and @RequiredArgsConstructor
together.
With lombok:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import lombok.AccessLevel;import lombok.Setter;import lombok.Data;import lombok.ToString;@Data public class DataExample { private final String name; @Setter(AccessLevel.PACKAGE) private int age; private double score; private String[] tags; @ToString(includeFieldNames=true) @Data(staticConstructor="of") public static class Exercise <T> { private final String name; private final T value; } }
Vanila java:
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 import java.util.Arrays;public class DataExample { private final String name; private int age; private double score; private String[] tags; public DataExample (String name) { this .name = name; } public String getName () { return this .name; } void setAge (int age) { this .age = age; } public int getAge () { return this .age; } public void setScore (double score) { this .score = score; } public double getScore () { return this .score; } public String[] getTags() { return this .tags; } public void setTags (String[] tags) { this .tags = tags; } @Override public String toString () { return "DataExample(" + this .getName() + ", " + this .getAge() + ", " + this .getScore() + ", " + Arrays.deepToString(this .getTags()) + ")" ; } protected boolean canEqual (Object other) { return other instanceof DataExample; } @Override public boolean equals (Object o) { if (o == this ) return true ; if (!(o instanceof DataExample)) return false ; DataExample other = (DataExample) o; if (!other.canEqual((Object)this )) return false ; if (this .getName() == null ? other.getName() != null : !this .getName().equals(other.getName())) return false ; if (this .getAge() != other.getAge()) return false ; if (Double.compare(this .getScore(), other.getScore()) != 0 ) return false ; if (!Arrays.deepEquals(this .getTags(), other.getTags())) return false ; return true ; } @Override public int hashCode () { final int PRIME = 59 ; int result = 1 ; final long temp1 = Double.doubleToLongBits(this .getScore()); result = (result*PRIME) + (this .getName() == null ? 43 : this .getName().hashCode()); result = (result*PRIME) + this .getAge(); result = (result*PRIME) + (int )(temp1 ^ (temp1 >>> 32 )); result = (result*PRIME) + Arrays.deepHashCode(this .getTags()); return result; } public static class Exercise <T> { private final String name; private final T value; private Exercise (String name, T value) { this .name = name; this .value = value; } public static <T> Exercise<T> of (String name, T value) { return new Exercise <T>(name, value); } public String getName () { return this .name; } public T getValue () { return this .value; } @Override public String toString () { return "Exercise(name=" + this .getName() + ", value=" + this .getValue() + ")" ; } protected boolean canEqual (Object other) { return other instanceof Exercise; } @Override public boolean equals (Object o) { if (o == this ) return true ; if (!(o instanceof Exercise)) return false ; Exercise<?> other = (Exercise<?>) o; if (!other.canEqual((Object)this )) return false ; if (this .getName() == null ? other.getValue() != null : !this .getName().equals(other.getName())) return false ; if (this .getValue() == null ? other.getValue() != null : !this .getValue().equals(other.getValue())) return false ; return true ; } @Override public int hashCode () { final int PRIME = 59 ; int result = 1 ; result = (result*PRIME) + (this .getName() == null ? 43 : this .getName().hashCode()); result = (result*PRIME) + (this .getValue() == null ? 43 : this .getValue().hashCode()); return result; } } }